You will really find storing the ASP application settings in the global asax file to be more than helpful. It really makes the use of application start method as well as the global property application. The application property for the HTTPcontextclass returns the httpapplicationstateobject when the user request for the ASP page through the HTTP request. The HTTPappplicationstateobject allows sharing of the global parameters during multiple sessions and request for the asp.net application.
It is really very hectic to define again and again the web application parameters like data source, initial catalog etc. hence most of the programmers find it really easy to define them once inside the global.asax file and use it again and again during various sessions.
I would like to provide you the code which will explain the above.
Global.asax.cs
Protected void Application_start( object sender, eventArgs e)
{
Application["user"]="user";
Application["pass"]= "pass";
Application ["name"] ="cooldb";
Application ["server"] = "myoffice.db.server";
String myConnString= "Data Source=" + Application ["server"] +"; initial catalog=" + application ["name"] +"; UserId=" + Application ["name"] +"; Password="+ Application ["pass"] +";
Application [ConnString] =myConnString;
}
It is clear for the above code that when the web application starts then the application object will contain the value of the parameter and you can access them on the page within your application.
The above code is written in CSharp. However you cannot say that this is any new technique. If you have done even a little bit of programming, even if you are familiar with the windows application then you will know that it is the common practice to assign the values to these parameters once and then use the connection string throughout the project. The word Global has been used and it is quiet similar to the global variable. It just makes it sure that you can use it again and again. For example here once you initialize these global parameters you will not need to do it again and again.
You can see how it has been used in the code below.
SomePage.asax.cs
Private void page_load (object sender, System.EventArgs e)
{
SqlConnection myCNN;
SqlCommand myCMD;
SqlDataReader myReader;
myCNN= new Sqlconnection ((string) Application ["connsrting"]);
myCMD=new Sqlcommand ("select * from company",myCNN);
mycNN.open();
// set of commands
mycNN.close ();
}
You would really want to know the advantage and disadvantage of the global.asax in storing the global parameters. I must say that the real advantage lies in the pact that these complex parameters are assigned the value once inside the global.asax file and used again and again without much change. If you want to change the value of some parameter then it will require the recompilation and you will have to change the value inside the global.asx file. This is really a good asset and you will really find it very helpful during coding.
No comments:
Post a Comment