Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
reflex2dotnet
Yak Posting Veteran
99 Posts |
Posted - 2007-02-13 : 16:13:01
|
Hi EverybodyI am using ASP.NET 2.0In my application, i want to use session conceptI have a user control, in which i have to set the default values for some controls to be session variables.eg: for a dropdown list i am using -1 as the defualt valuehere i want to set a session variable as session("var1") = -1Now when my page loads, in the page load, user control will display the dropdwonlist with -1 valuesNow the user changes the value in dropdownlist to 2How can i update the session("var1") to the newly selected value from the user control?I also want to mention that i have a button in the user control which i am planning to do the refresh after changing any value in the user controlso in the button click, how can i reset the session varible values?Initially where do i have to set the session("var1") = -1 in usercontrol--- in page init or in page loadHope I am not confusing you allHope someone will help me to figure it outthanks |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-02-13 : 16:20:59
|
Your session variables should not be initialized on a page, they should be initialized when the session itself is created. In your Global.asax file you should see a Session_Start method, and that is where you put your initialization code for the session. Note that this may be different in 2.0, I have 1.1 running here at work so I cannot check easily and I don't recall.Another option is to simply check if a session variable exists before setting it. i.e., in VB you would write:If Session("var1") is nothing Then Session("var1") = DefaultValueEnd IfBut it is much better and easier and cleaner to have everything done in one place in the Session_Start method of your application.- Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
reflex2dotnet
Yak Posting Veteran
99 Posts |
Posted - 2007-02-13 : 17:04:48
|
Thanks for the prompt replyI will go in detail with the global.asax optionIf come acorss any questions, I will definitely seek you all memebrs |
|
|
reflex2dotnet
Yak Posting Veteran
99 Posts |
Posted - 2007-02-14 : 08:59:21
|
I set 3 session variables in global.asax and initialize their value there I removed my initialization of these three variables from the page init and page load of the user control.When i run the application, it says "object reference not set"do i need to give in the page load of user control asdropdownlist1.selectedvalue = session("var1")datecontrol.value = session("var2")Where exactly do i have to give?In page init or in page load?I know these all will be silly questionsPlease forgive, as i am a beginner in industry and not exactly able to apply what all i learnedThanks for all your help |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-02-14 : 11:19:34
|
quote: do i need to give in the page load of user control asdropdownlist1.selectedvalue = session("var1")datecontrol.value = session("var2")Where exactly do i have to give?In page init or in page load?
I don't understand your question. You always need to initialize your controls on your page when the page first loads, so you would need to use code like that on the Page_load() event to initialize your controls with the necessary default values. What block code is throwing the error that you indicate? Also, show me the code where you are setting your session values. It also helps to put some breakpoints in your code and to step through it to verify that your session variables are being set when you expect and that things are happening when you think they are.In addition, as always, I strongly recommend getting a book about asp.net programming and going through it to be sure that you understand all of the core concepts of asp.net -- sessions, page events, and so on. - Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
|
|
|
|
|