Asp.NET Unit-5
Asp.NET Unit-5
Sem - 5
Introduction
XML stands for eXtensible Markup Language.
A text file that contains information organized in a structure that meets XML standards.
XML is a markup language much like HTML but both have many differences.
Features of XML
Using XML you can create your own tags.
So by creating your own tags you can specify your own set of rules to enter data between
tags.
Consider following example ,
<Age>35</Age>
You can see here that <Age> tag have a data i.e. 35. Now while creating <Age>tag, you can
specify rules for age, so that these tags wont allow you to enter invalid <Age> value.
XML can have a file called DTD (Document Type Definition or XSD (XML Schema
Definition- Both of these files are used to specify rules for XML data. Out of these two,
XSD gives, much more facilities then DTD.
For example, in above example we discussed, about <Age> tag (Element) which should
accept AGE only between 1 to 100.
In which file you will write these rules? That is XSD File.
XML is platform independent. So today it is used widely across web applications to transfer
data.
XML can separate Data from HTML. The reason behind this is that HTML is mainly used
for presentation purpose where as XML focuses on data part.
In XML, you can create DTD (Data Type Definition) and XSD (XML Schema Definition)
files to specify your own set of rules for user defined tags.
Page 1 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
In XML, you can create XSLT (XML StyleSheet Transformation) files to show your XML
data in particular format.
XML is called eXtensible Markup Language because you can create your own tags. Further
more you can again create new tags with already created tags.
Which means you can extend your previously created tags.
Page 2 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
WriteXML( ) method :
This method is used to read data from DataSet and write data as XML data.
DataSetName is Treated as Root Element
All Tables of DataSets are treated as Main Elements.
All the Column of the particular Table are treated as Sub element
All Rows data is the actual data of the xml file
WriteXMLSchema( ) method :
We have discussed a small bit of Xml Schema Defination file in XML Basics part.
XMLSchema is a file which contains rules for Xml Elements and their data. You may
also have some Schema information along with you DataSet.
If you use only WriteXml() method, only data. is written as XML. But along with data if
you want to write schema information, you can use WriteXmlSchema() method.
Reading DataSets with XML
This is the procedure to store the XML file data into a particular DataSet.
ReadXml ( ) method :
This method is used to read Xml data from any Xml file to DataSet.
It treats all main Elements under root element as different tables, if you are using
DataSet to read the data.
It automatically stores data in Xml way, because you are already reading a file which is
in Xml format.
Reading XmlSchema( ) method :
XmlSchema is a file which contains rules for Xml Element and their data. You may also
have some Schema information along with your DataSet.
If you use only ReiadXmlQ method. It just reads entire d.ata from Xml and stores it into
DataSet or DataTable.
But before reading Xml data into DataSet, if you want to specify some schema rules
(data rules) before writing xml data to DataSet/DataTable, you can use
ReadXmlSchema() which allows you to specify, XSD file and it restricts the data being
copied into Dataset / DataTable.
Web services
Introduction
o Web services are one of the strongest feature that is exposed by Microsoft since
ASP.NET launched.
o As you are new to Web Services, you might have first question that what are web
services??
o Web Services are group of web methods where each web method gives you some
particular functionality. Web methods are just same as those methods, but all web
methods are combined under a single Web Service and Web Service is uploaded / kept
on Web Server so that you can use its functionality through various Web methods.
o You can use this web services method for your windows application as well as in web
application.
o Web services can be used by different applications regardless of the programming
language, Operating System and Hardware platform.
o The net result is reduced complexity and more time for a developer to concentrate on
business logic rather than plumbing.
o A web service is a distributed computing technology that enables the exposure and reuse
of logical business entities over the Internet.
o Web services provide a platform-independent means of exposing business logic over the
Internet.
Page 3 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
o Creating ASP.NET web services is easy and abstracts much of the complexity associated
with Internet communications. This allows developers to concentrate on business logic.
o web services has no. of web methods that provide different functionality which can be
accessed by different types of application.
o An application which uses Web Service is called as Web Service client as it is used
particular web service.
o today there are so many clients, each of them don’t use similar configuration. Some
client use different hardware, some use different operating system and programming
languages.
o Now question is how to share something among all of them. Web services are the
components which are shareable among all types of hardware, OS and programming
language.
o The reason behind these are web services are
o Platform independent.
o Web services can be shared among any platform. Now the question arises is how it is
possible.
o The answer is various standard and protocols work behind web services.
o Standard / Protocols for Web Services
XML (eXtensible Markup Language)
SOAP(Simple Object Access Protocol)
WSDL (Web Services Description Language)
UDDI(Universal Description Discovery Integration)
Page 4 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
o After this appropriate web method is invoked, execute and again answer value is
returned back using SOAP packet in reverse.
Page 5 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
Design view:-
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class _Default : System.Web.UI.Page
{
static string str = WebConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
Page 6 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
Page 7 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public bool login(String username, string password)
{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=E:\ASP_NET\webServiceDemo\App_Data\Lo
gin.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from login", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr[0].ToString() == username && dr[1].ToString() == password)
{
return true;
}
}
con.Close();
return false;
}
}
Run .asmx file and invoke your web method to test the service
Make one “dll” folder in your folder
If successfully runs now write click on your application and select option “Publish Web
Site”
Now again work in your Website which you have added database
Page 8 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
From website menu-> Add References -> and select Browse tab form dialog box and
browse your App_code.dll from you’re “ dll” folder -> bin
Page 9 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
Page 10 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
Each and Every ASP.NET application has its own copy of configuration settings stored in a
file called Web.config. This file is generated from Machine.Config file. Web Config file is
Stored in
C:\<Windows>\Microsoft.NET\Framework\<version>\Config\
If the web application spans multiple folders, each sub folder has its own Web.config file
that inherits or overrides the parent's file settings.
Configuration File Format
Both Machine.config and Web.config share the same XML schema. Configuration files are
divided into multiple sections, with each section being a top-level XML element. The root
level element in a configuration file is always <configuration>. Configuration file is
organized as hierarchy of section handlers, with each section provides a unique
functionality.
<?xml version=“1.0” ?>
<configuration>
<!-- All other Configuration sections are place here inside configuration
section -->
</configuration>
<Configuration> Sections
<configSection> : This allows you to configure different sections of web application like
javaScript, Web Services, Resources etc.
<appSetting> :Configures custom settings for an application. The settings in this section
can be compared to application variables.
<connectionString> : Specifies a collection of database connection strings, as name/value
pairs, for ASP.NET applications and features.
<System.web> :This is important element used to configure your web application.
<compilation> <authentication>
<caching> <customErrors>
<deployment> <roleManager>
<trace> <sessionState>
<webServices>
https://www.youtube.com/watch?v=l3Sqdk88gH4<system.codedom> : This section
stores compiler specific information used for your web application under <compilers>
element.
<system.webServer> : This section stores information and configuration setting for web
servers which will be used for your deploying and running your web application
<runtime> : This section is used to specify some runtime settings for your web application.
Run Time setting can include no .of setting like connection string, tracing, data provider etc.
Application Settings
The application settings allow storing application-wide name-value pairs for read-only access.
For example, you can define a custom application setting as:
<configuration>
<appSettings>
<add key="Application Name" value="MyApplication" />
</appSettings>
</configuration>
Page 11 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
For example, you can also store the name of a book and its ISBN number:
<configuration>
<appSettings>
<add key="appISBN" value="0-273-68726-3" />
<add key="appBook" value="Corporate Finance" />
</appSettings>
</configuration>
Page 12 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
In the Admin side pages are generally made for useful web site related information or
setting. So that pages cannot be accessed by the common user otherwise it can be harmful to
your website.
So to those pages you have to give some specific rights or security. This thing can be
handling by Role Manager.
.
Session Configuration :
You can configure session related settings under this. Under Session configuration, you can
specify session settings like, Timeout Period, Session Mode, Cookie based Session or
Cookieless session, SqlCommandTimeout,NetworkTimeout, etc.
Many of these sessions based configurations can be done at different levels of session
handling.
Trust Levels
This thing is useful at the time of web developing. Web site can be developed by many
person so different person have different rights.
Web master (Project Leader) can access all the pages of the web site. Web developer
(Designer, Programmer or Tester) have some limited access in the web site. Administrator
(DBA) has access for only back end pages. So this facility is provided by Trust Levels.
Caching
Sometimes caching is a very important thing for the web site. In your web server many
website or many web pages are available.
So when you request for that page web server has to find the page or data. So at that time
page or data which is used recently that you can store in the cache memory. So it can not be
search each and every time for that you can increase the speed of your web server.
Tracing
To trace means to monitor.
Tracing is a process of finding the problem from the application. When you executes the
web site or particular page within the web sites you cannot detect the value of variable,
property or methods or any code.
Sometimes, you can’t find the actual problem at that time by the using of the trace
functionality you can know the flow of the program and find the bug or error by the help of
the value of source code without affect the actual output.
Tracing is important to test your website in order to give perfect output as per developer’s
requirement which fulfills user’s need.
Tracing can be done at two levels. They are
Page Level Tracing
Application Level Tracing
Page 13 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
Explains how to enable tracing for a page, as well as how to include trace
statements in page output and how to interpret trace messages for a page.
The page level tracing can be enabled using page directive called Trace.
<%@ Page Language="vb" AutoEventWireup="False" Trace="True"
Codebehind="TraceTest.aspx.vb" Inherits="TracingExample.Test"%>
Also, we have inbuilt Trace object to enable and disable the tracing dynamically.
In the page, on Load event if you write the below line, the tracing will be
enabled.
Trace.IsEnabled = True;
Trace :-
o This is the attribute of the page directive. It has a boolean value. Default
value is false. If you want
o to trace a particular page you can set the value true of Trace attribute.
Trace Mode :-
o You can also use the TraceMode attribute of the <%@Page ...%>
directive to specify how you want
o your trace messages to be sorted when they are displayed on the page.
You can set this attribute to
o either SortByTime, which is the default, or SortByCategory.
o SortByTime displays trace messages in the order that they were
processed on the page.
o SortByCategory displays messages alphabetically by the category that
you assigned them when you wrote the code.
You can write trace information by using Trace.Write() method or by using
Trace.Warn() method.
so, if you have specified “SortByCategory”, all warned traced messages and
written traced messages are displayed.
Page 14 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
PageOutput :- If this attribute value is set to true then the trace output will be displayed on
the page. If value of this attribute is set to false trace, then the trace output can be retrieved
using browser.
RequestLimit :- This attribute is active only when the PageOutput attribute value is set to
false. This attribute defines the number of page requests that are to be traced.
TraceMode :- To manage the order of the trace output, this attribute is used. For Example
SortByTime, SortByCategory etc.
localOnly :- Set this attribute to "false" if you want access the trace log from any client.
Otherwise the log will be displayed on local machine only.
Key Points:
If you have enabled Application Level Tracing, no need to go for Page Level Tracing.
Tracing is not enabled automatically. You need to enable tracing whether its page level or
Application Level.
Tracing does not affect your output. It just give some additional information along with your
code.
Server.GetLastError() Method :
Page 15 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
This is the method is used to get the Exception or Error which is currently generated.
According that Exception or Error you can take proper decision to continue the
application.
Server.ClearError Method :
This Error is used to clear the current error after give the proper output to the user. If you
will not clear error. Error is generated and execution of the program is not continuing.
Page 16 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
<system.web>
</configuration>
Status Code Error Message
Page 17 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
After a user is authenticated, the user’s credentials are stored in a cookie for use in
that session. When the user has not logged in and requests for a page that is insecure,
he or she is redirected to the login page of the application. Forms authentication
supports both session and persistent cookies.
Example :
<authentication mode="Forms">
<forms name="Login" loginUrl="Login.aspx">
<credentials passwordFormat="Clear">
<user name="prashant" password="doshi"/>
<user name="BBB" password="bbb"/>
</credentials>
</forms>
</authentication>
<authorization>
<allow users=”prashant,BBB"/>
<deny users="?"/>
</authorization>
The statement <deny users="?"> indicates other users are not allowed. “?” indicates
all other users where as if you specify “*”, it indicates all the users.
How to Check Username and Password ?
if (FormsAuthentication.Authenticate(TextBox1.Text, TextBox2.Text))
{
FormsAuthentication.RedirectFromLoginPage (TextBox1.Text, false);
}
else
{
Response.Write("Invalid username or Password");
}
Page 18 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
<configuration>
<system.web>
<authenticationmode="Passport">
<passportredirectUrl="login.aspx" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</configuration>
Type of Authorization :
o URL Authorization
o File Authorization
o Customized Authorization
URL Authorization
We authorized you to particular URL. This authorization method is useful for
splitting Administrations and Normal Users. Where all the Administrative level Web
Pages are stored in different folder.
Then only particular URL is authorized for administrative level users. So they can
access certain group of web pages for web site Administration.
File Authorization
File Authorization is almost similar way to authorize you for particular file. This
method is also used to split admin pages and normal pages. If you have limited no. of
files to authorize, you can use this method, but for large number of users URL
authorization is best.
Customized Authorization
You can also customize your Authorization. This can be done by using the location
element inside the configuration element of web.config file.
Example :
<configuration>
<location path="OnlyAdmin.aspx" >
<system.web>
<authorization>
<allow users="Admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
</configuration>
As we have learn we done many settings in the web.config file like set the application
variable, connection strings, custom errors etc. So you need to get the value of that setting.
Most important thing how to read that value from the web.config or machine.config file and
that value you can use in your web site.
Page 19 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
So reading that setting from the configuration file you need to use namespace
System.Web.Configuration. By using this namespace you can read the setting of the
web.config or machine.config file.
Methods :
(1) GetSection() :- This method return the particular section according to path you have
given from the web.config file.
(2) OpenMachineConfiguration() :- This method is used to read the setting of the
machine.config file.
(3) OpenWebConfiguration() :- This method is used to read the setting from the
web.config file.
(4) GetWebApplicationSection() :- This method is used to read the section from the
Current web sites root folder web.config file.
ASPX Page:-
Page 20 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
ASPX.CS page:-
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Diagnostics;
Page 21 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string target = Request.QueryString["ReturnUrl"];
if (target == "/AuthenticationAndAuthorizationDemo/AdminPage.aspx")
{
Response.Write("<font color='red'>");
Response.Write("You must log in as 'Admin' in order to use AdminPage.aspx page");
Response.Write("</font> <hr>");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FormsAuthentication.Authenticate(TextBox1.Text, TextBox2.Text))
{
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
}
else
{
Response.Write("Invalid username or Password");
}
}
}
Add AdminPage.aspx page and design as follows.
Page 22 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
- At Design Time:-
ASPX Page:-
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Configuration;
using System.Web.Configuration;
public partial class GetSettings : System.Web.UI.Page
{
protected void btnConStr_Click(object sender, EventArgs e)
{ string x =
WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
Page 23 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
Local deployment : In this case, the entire application is contained within a virtual
directory and all the contents and assemblies are contained within it and available to the
application.
Global deployment : In this case, assemblies are available to every application running
on the server.
There are different techniques used for deployment, however, we will discuss the following
most common and easiest ways of deployment:
XCOPY deployment
Copying a Website
Creating a set up project
XCOPY Deployment
Page 24 of 25
Sub.: ASP.NET UNIT - 5 B.C.A. Sem - 5
XCOPY deployment means making recursive copies of all the files to the target folder on the
target machine. You can use any of the commonly used techniques:
FTP transfer
Using Server management tools that provide replication on a remote site
MSI installer application
XCOPY deployment simply copies the application file to the production server and sets a virtual
directory there. You need to set a virtual directory using the Internet Information Manager
Microsoft Management Console (MMC snap-in).
Copying a Website
The Copy Web Site option is available in Visual Studio. It is available from the Website ->
Copy Web Site menu option. This menu item allows copying the current web site to another
local or remote location. It is a sort of integrated FTP tool.
Using this option, you connect to the target destination, select the desired copy mode:
Overwrite
Source to Target Files
Sync UP Source And Target Projects
Then proceed with copying the files physically. Unike the XCOPY deployment, this process of
deployment is done from Visual Studio environment. However, there are following problems
with both the above deployment methods:
Page 25 of 25