Clarification On Static Member [modified]
-
Hi friends..plz go through this pseudo code I Included Requirid Namespaces Dataset ds;--------------------------------------1 //static Dataset ds;-----------------------------2 protected void page_load(parameters) { ds = function in Data acesss Layer which retrives records from database and returns dataset. populatedatagrid(); } void populatedatagrid() { if(mydropdownlist.selectedIndex=0) datagrid1.datasource=ds; else { datagrid1.Datasource= here I performed filtering action using Row Filter Property and selected Index Value } } void mydropdownlist_selectedIndexchanged(parameters) { populatedatagrid(); } I am designing a web page where by default all records will displayed.but by selecting from dropdownlist I can filter the records.Later I thought its not right to perform the fuctionalities of the page_load event at each postback(unnecessry making connection to the database)..so i used Page.Ispostback to avoid that..so first time it worked fine but when i selected from dropdwn list it throws an error "object reference not set to an instance of object".. reason for above error i understood(because in page load event i am returning dataset which does not happened at page post back)..so one of my friend commented the line no-1 and uncommented the line no-2 in above code..thus made dataset static..and it works What I am not understanding is why making the dataset static solved the task..though HTTP is static?Is making dataset ststic makes the records to be saved at server? Warm Regards, Rahul Agarwal -- modified at 7:11 Tuesday 12th June, 2007
-
Hi friends..plz go through this pseudo code I Included Requirid Namespaces Dataset ds;--------------------------------------1 //static Dataset ds;-----------------------------2 protected void page_load(parameters) { ds = function in Data acesss Layer which retrives records from database and returns dataset. populatedatagrid(); } void populatedatagrid() { if(mydropdownlist.selectedIndex=0) datagrid1.datasource=ds; else { datagrid1.Datasource= here I performed filtering action using Row Filter Property and selected Index Value } } void mydropdownlist_selectedIndexchanged(parameters) { populatedatagrid(); } I am designing a web page where by default all records will displayed.but by selecting from dropdownlist I can filter the records.Later I thought its not right to perform the fuctionalities of the page_load event at each postback(unnecessry making connection to the database)..so i used Page.Ispostback to avoid that..so first time it worked fine but when i selected from dropdwn list it throws an error "object reference not set to an instance of object".. reason for above error i understood(because in page load event i am returning dataset which does not happened at page post back)..so one of my friend commented the line no-1 and uncommented the line no-2 in above code..thus made dataset static..and it works What I am not understanding is why making the dataset static solved the task..though HTTP is static?Is making dataset ststic makes the records to be saved at server? Warm Regards, Rahul Agarwal -- modified at 7:11 Tuesday 12th June, 2007
-
Hi friends..plz go through this pseudo code I Included Requirid Namespaces Dataset ds;--------------------------------------1 //static Dataset ds;-----------------------------2 protected void page_load(parameters) { ds = function in Data acesss Layer which retrives records from database and returns dataset. populatedatagrid(); } void populatedatagrid() { if(mydropdownlist.selectedIndex=0) datagrid1.datasource=ds; else { datagrid1.Datasource= here I performed filtering action using Row Filter Property and selected Index Value } } void mydropdownlist_selectedIndexchanged(parameters) { populatedatagrid(); } I am designing a web page where by default all records will displayed.but by selecting from dropdownlist I can filter the records.Later I thought its not right to perform the fuctionalities of the page_load event at each postback(unnecessry making connection to the database)..so i used Page.Ispostback to avoid that..so first time it worked fine but when i selected from dropdwn list it throws an error "object reference not set to an instance of object".. reason for above error i understood(because in page load event i am returning dataset which does not happened at page post back)..so one of my friend commented the line no-1 and uncommented the line no-2 in above code..thus made dataset static..and it works What I am not understanding is why making the dataset static solved the task..though HTTP is static?Is making dataset ststic makes the records to be saved at server? Warm Regards, Rahul Agarwal -- modified at 7:11 Tuesday 12th June, 2007
-
Please note that you have an option to modify your message. And what the PostBack property is going to do with the Question?:confused:
Regards, Arun Kumar.A
-
Hi friends..plz go through this pseudo code I Included Requirid Namespaces Dataset ds;--------------------------------------1 //static Dataset ds;-----------------------------2 protected void page_load(parameters) { ds = function in Data acesss Layer which retrives records from database and returns dataset. populatedatagrid(); } void populatedatagrid() { if(mydropdownlist.selectedIndex=0) datagrid1.datasource=ds; else { datagrid1.Datasource= here I performed filtering action using Row Filter Property and selected Index Value } } void mydropdownlist_selectedIndexchanged(parameters) { populatedatagrid(); } I am designing a web page where by default all records will displayed.but by selecting from dropdownlist I can filter the records.Later I thought its not right to perform the fuctionalities of the page_load event at each postback(unnecessry making connection to the database)..so i used Page.Ispostback to avoid that..so first time it worked fine but when i selected from dropdwn list it throws an error "object reference not set to an instance of object".. reason for above error i understood(because in page load event i am returning dataset which does not happened at page post back)..so one of my friend commented the line no-1 and uncommented the line no-2 in above code..thus made dataset static..and it works What I am not understanding is why making the dataset static solved the task..though HTTP is static?Is making dataset ststic makes the records to be saved at server? Warm Regards, Rahul Agarwal -- modified at 7:11 Tuesday 12th June, 2007
Please don't use static unless you are sure that it will not create any side effect. Consider the following program:
static int i; protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { i=i+1; TextBox1.Text=i.ToString(); }
For each Button Click event, the value of "i" is incremented and displayed in the TextBox. I have clicked the button 5 times and the text box shows 5 in it. At this moment,when I opened another browse , copy - paste the same url and I tried to click the button, it shows "6" in it. So, I think that: For the first request, an Object of the page is created and maintained in the server. For subsequent request separate object is created for the page and is rendered to the Client, and the static member is shared by all objects.
Regards, Arun Kumar.A
-
Please don't use static unless you are sure that it will not create any side effect. Consider the following program:
static int i; protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { i=i+1; TextBox1.Text=i.ToString(); }
For each Button Click event, the value of "i" is incremented and displayed in the TextBox. I have clicked the button 5 times and the text box shows 5 in it. At this moment,when I opened another browse , copy - paste the same url and I tried to click the button, it shows "6" in it. So, I think that: For the first request, an Object of the page is created and maintained in the server. For subsequent request separate object is created for the page and is rendered to the Client, and the static member is shared by all objects.
Regards, Arun Kumar.A