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.

 All Forums
 Development Tools
 ASP.NET
 Managing sessions

Author  Topic 

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-02-13 : 16:13:01
Hi Everybody

I am using ASP.NET 2.0
In my application, i want to use session concept
I 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 value
here i want to set a session variable as
session("var1") = -1

Now when my page loads, in the page load, user control will display the dropdwonlist with -1 values

Now the user changes the value in dropdownlist to 2

How 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 control

so 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 load

Hope I am not confusing you all

Hope someone will help me to figure it out

thanks

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") = DefaultValue
End If

But it is much better and easier and cleaner to have everything done in one place in the Session_Start method of your application.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-02-13 : 17:04:48
Thanks for the prompt reply
I will go in detail with the global.asax option
If come acorss any questions, I will definitely seek you all memebrs
Go to Top of Page

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 as
dropdownlist1.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 questions
Please forgive, as i am a beginner in industry and not exactly able to apply what all i learned

Thanks for all your help
Go to Top of Page

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 as
dropdownlist1.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.



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -