Saturday, March 24, 2012

Session variable, need help!

Hi All!

In my login page, I created a session variable to hold the username(studentId). It's not working! It's very frustrating!! Anybody figure out what my problem is?!

My Code:

Login page Code:

Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then

dim user as string = UserName.Text
Session("StudentID") = user

End If
End Sub

Sub LoginBtn_Click(Sender As Object, E As EventArgs)

If Page.IsValid Then
Dim userDS As New System.Data.DataSet

userDS = password(UserName.Text, UserPass.Text)

If userDS.Tables(0).Rows.Count = 1 Then

FormsAuthentication.RedirectFromLoginPage(UserName.Text, true)
Else
Msg.Text = "***Invalid Credentials: Please try again***"
End If
End If

End Sub


Next Page Code:

Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then


Dim id = CType(Session("StudentID"), string)
Label1.Text = id.ToString()


End If
End Sub

make sure that you have session enabled.

Hi!

Don't know how to do that! Can you explain to me how I can do it?

Collette


I think the problem is that you are setting the session variable in thepage load method, and at that time UserName.Text is probablyblank! Try creating the session variable in the click event ofthe login button and see if that solves the problem.

Hi!

I tried this:

Sub LoginBtn_Click(Sender As Object, E As EventArgs)

If Page.IsValid Then
Dim userDS As New System.Data.DataSet

userDS = password(UserName.Text, UserPass.Text)

Session("UserName") = user

If userDS.Tables(0).Rows.Count = 1 Then

FormsAuthentication.RedirectFromLoginPage(UserName.Text, true)
Else
Msg.Text = "***Invalid Credentials: Please try again***"
End If
End If

End Sub

Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then

dim user as string
user = UserName.Text

End If
End Sub

But Im getting the following error:

Object reference not set to an instance of an object.

Please help!!!



Panic Over!!

Sussed it out, appreciate the help I got, Thanx guys!Wink [;)]


Your variable "User" is not a global variable in your page. It islocal to the Page_Load method and hence not usable in any other method.
Try the following ... note the change in the underlined text.

Sub LoginBtn_Click(Sender As Object, E As EventArgs)

If Page.IsValid Then
Dim userDS As New System.Data.DataSet

userDS = password(UserName.Text, UserPass.Text)

Session("UserName") = UserName.Text

If userDS.Tables(0).Rows.Count = 1 Then

FormsAuthentication.RedirectFromLoginPage(UserName.Text, true)
Else
Msg.Text = "***Invalid Credentials: Please try again***"
End If
End If

End Sub


0 comments:

Post a Comment