Question:
Class in VB.NET and storing data?
csdraska
2006-11-21 12:12:17 UTC
With a mulitple form web page how do I store information that I will need to use threw out my forms. I know I can make a class and have say "public shared userid as string" but wont that variable change all the time when ever someone is on the site. So if two people are loged onto my web page and the userid is stored from the first time they log on so that in later web pages I populate a dataview from a database based on their userid. If user A log on and userid = A and then user B log on and userid= B wont that rewrite userid for A. So if A and B are both on the site and A goes to a new page that will display a dataview based off his userid wont it show B's info since B loged in second and that changed userid=A to userid=B.I would be using userid as my where clause in my SQL statement to pull data for the databse.I know that the web pages are instances of what is on the server but are classes instances too and everytime someone enters the site a class instance is made for just them?
Three answers:
thegooddeal2000
2006-11-21 13:06:37 UTC
That is why you should store the User's ID in a Session variable.



The server maintains a Session for every unique browser that comes to the site. That way User A won't see User B's data and User B won't see User A's data.



But beware! The Session will expire in 20 minutes (default). You can make the session timeout longer, but it will still only store data for so long (and won't persist if IIS is restarted). You need to have any information that you want saved permanently stored in a database.
Doug k
2006-11-21 22:05:22 UTC
It sounds to me like you need to do some reading about this. As it turns out, there are many ways to "persist" information in a .NET web application.



The most global is indeed the session. You can fill a session variable when a person first hits a site. As long as you don't change it, it will stay set until the user closes the browser or goes for more than 20 minutes without posting back (hitting another page on the site). You can configure that in the web.config file.



Here is how to add something to the session. On one page, you can say something like ...

Session("UserID") = txtLogin.Text.Trim()



On another page, you can say something like ...

Dim cmd As New SqlCommand

Dim cn As New SqlConnection(Session("db_connectionstring")

cmd.Connection = cn

cmd.CommandText = "SELECT field1, field2 FROM sometable WHERE User = @UserID

cmd.Parameters.Add("@UserID", SqlDbType.Varchar)

cmd.Parameters("@UserId").Value = Session("UserID")



Dim dr As SqlDataReader = cmd.ExecuteReader()



blah.. blah... blah...
chemicalimbalance000
2006-11-22 04:54:46 UTC
Store the info in a Cookie.

The cookie resides on the user's machine, so the data will stay with the right person.



Alternately, you can embed the data in the web address.

default.aspx?var1=asdf



Then you can do a QueryString to get it back.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...