Session Not Maintaing State

R

rn5a

A ASPX Form has a TextBox & a Button. Users enter their username in
the TextBox & submit the Form after which records pertaining to the
username get retrieved from a MS-Access database table & displayed in
a DataGrid. The DataGrid also provides pagination.

Sub Page_Load(......)
If Not(Page.IsPostBack) Then
'do something
Else
Dim strSQL As String
Dim strUserName As String

strUserName = txtUserName.Text
Session("UserName") = strUserName
Response.Write("UserName: " & Session("UserName"))

strSQL = "SELECT............WHERE UserName = '" &
Session("UserName") & "'"

'some more code comes here
End If
End Sub

Assume that I enter the username Ronnie in the TextBox & submit the
Form. The DataGrid correctly displays the 1st set of records (note
that the records are paginated) but strangely when I click any of the
paging links (or for that matter, any link that re-posts the Form),
the variable Session("UserName") becomes empty! It no longer holds the
username; it gets lost! I have turned on EnableSessionState in the
Page directive as well but that doesn't make any difference.

Can someone please give me some clues on what could be causing this? I
also tried using ViewState("UserName") instead of Session("UserName")
but that doesn't resolve the issue.
 
J

Joey

A ASPX Form has a TextBox & a Button. Users enter their username in
the TextBox & submit the Form after which records pertaining to the
username get retrieved from a MS-Access database table & displayed in
a DataGrid. The DataGrid also provides pagination.

Sub Page_Load(......)
If Not(Page.IsPostBack) Then
'do something
Else
Dim strSQL As String
Dim strUserName As String

strUserName = txtUserName.Text
Session("UserName") = strUserName
Response.Write("UserName: " & Session("UserName"))

strSQL = "SELECT............WHERE UserName = '" &
Session("UserName") & "'"

'some more code comes here
End If
End Sub

Assume that I enter the username Ronnie in the TextBox & submit the
Form. The DataGrid correctly displays the 1st set of records (note
that the records are paginated) but strangely when I click any of the
paging links (or for that matter, any link that re-posts the Form),
the variable Session("UserName") becomes empty! It no longer holds the
username; it gets lost! I have turned on EnableSessionState in the
Page directive as well but that doesn't make any difference.

Can someone please give me some clues on what could be causing this? I
also tried using ViewState("UserName") instead of Session("UserName")
but that doesn't resolve the issue.

In asp.net 2.0 if you are modifying/saving the contents of a file, and
it is outside of the App_Data folder, I think it will cause an
AppDomain restart. This will kill your Session data. Do a search for
AppDomain restart, and you'll find more info.

JP
 
R

rn5a

In asp.net 2.0 if you are modifying/saving the contents of a file, and
it is outside of the App_Data folder, I think it will cause an
AppDomain restart. This will kill your Session data. Do a search for
AppDomain restart, and you'll find more info.

JP- Hide quoted text -

- Show quoted text -

Joey, when a user comes to the page for the first time, only the
TextBox & the Button are visible to users whereas the DataGrid remains
hidden. Assuming that the username entered by a user gets validated
successfully against the MS-Access database, the TextBox & the Button
becomes invisible & the DataGrid becomes visible..When any link is
clicked under such circumstances so that the page posts back to
itself, the Session variable loses the value it was holding prior to
clicking the link.

After some experiments, I concluded that if I keep the TextBox visible
when the DataGrid is displayed & then I click any link to post the
page, then the Session variable doesn't lose the value it had stored
when the page was posted for the first time but if the TextBox is made
invisible when the DataGrid is visible, then only the Session variable
loses the value.

Your opinion on this??

Though I am not 100% sure, I don't think this has got to do anything
with AppDomain or App_Data; it's the visibility of the TextBox that's
causing the Session variable to lose/retain its value.

I never anticipated this to be such a tough question to answer!
 
J

Joey

Joey, when a user comes to the page for the first time, only the
TextBox & the Button are visible to users whereas the DataGrid remains
hidden. Assuming that the username entered by a user gets validated
successfully against the MS-Access database, the TextBox & the Button
becomes invisible & the DataGrid becomes visible..When any link is
clicked under such circumstances so that the page posts back to
itself, the Session variable loses the value it was holding prior to
clicking the link.

After some experiments, I concluded that if I keep the TextBox visible
when the DataGrid is displayed & then I click any link to post the
page, then the Session variable doesn't lose the value it had stored
when the page was posted for the first time but if the TextBox is made
invisible when the DataGrid is visible, then only the Session variable
loses the value.

Your opinion on this??

Though I am not 100% sure, I don't think this has got to do anything
with AppDomain or App_Data; it's the visibility of the TextBox that's
causing the Session variable to lose/retain its value.

I never anticipated this to be such a tough question to answer!- Hide quoted text -

- Show quoted text -

You must have a statement that stores the value into the Session
variable. Now, where is that statement? Should it be running each time
the page loads (or whatever event handler fires), or should it only be
running under certain circumstances (i.e. first page load, all loads
other than first page load, etc..). Check to make sure the assignment
is occuring under the right conditions. For example, if you wanted
this to occur only AFTER the first page load, you would set it up like
this...

void Page_Load()
{
if(this.Page.IsPostBack)
{
// this code block runs only if the page is NOT being loaded
for the first time
[your code to do something]
}
}

If you already are way beyond this then I apologize! I remember it
took me a while to understand stuff like this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top