Wednesday 26 September 2012

What is an application object?



Hi friend , here i have explained about Application object.

Introduction:

Application object is used to store the information and access variables from any page in the application. Application object is same as the Session  object but only difference is that session object is used only for particular user.Apllication object  is same for all users once application object is created that application is used through out  all the pages in application (like database connection) and we change the application object  one  place those will changes in all the pages.
You can create the application object in Global.asax file and access those variable in all the pages. 
Add  the following code in Global.asax file :
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["UserID"] = "SureshDasari";
}
</script>
After this write the following code in aspx page:
<html> 
<head> 
<title>Application Object Sample</title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<asp:Label id="lblText" runat="server"></asp:Label> 
</form> 
</body> 
</html> 

After that write the following code in .cs file:
public void Page_Load(object sender, EventArgs e)
{
this.lblText.Text =  "UserName: " + Application["UserID"] + "<br>";
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.