Mobile Content

Saturday, December 24, 2011

Fetch data store in global.asax file in Asp.net

Here we use array to store data returning from database in global.asax file.

Que: why we are using global.asax file.
        If we use dropdownlist in page, in every postback dropdownlist get filled dynamically.if list of data is long process increases to reduce the process we are storing data into array in Application Start method in global.asax file when page load.so every postback data get filled from array into dropdownlist instead from database.

Example

global.asax

Application_Start 

SqlConnection con=new SqlConnection("Connectionstring");
SqlCommand cmd=new SqlCommand("select name from test",con);
SqlDataAdapter da=new SqlDataAdapter(cmd);
Dataset ds=new Dataset();
da.fill(ds);
int cnt=ds.Tables[0].Rows.Count;
string st=new string[1000];
for( int i=0; i < cnt ; i++ )
{
      st[i]=ds.Table[0].Rows[i].ItemArray[0].toString();
}
this.Application.Add("add",st);

in Default.aspx

<asp:DropDownList Id="DropDownList1" runat="server" />

in Default.aspx.cs

string st=(string[])page.Application["add"];
for(int i=0; i < st.length; i++)
{
     DropDownList1.Item.Add(st[i]);
}

o/p

When page is loaded at that time data from database get filled in array.So in every postback in applcation dropdownlist get filled by using array variable.


No comments:

Post a Comment