Thursday, March 22, 2012

Newbie in ASP(not advanced by any means)

Hey everyone, I'm trying to use Session Variables for the first time. I just have a couple of questions, when I debub(Step Thru) the code--is there a way to tell what value they hold?

Secondly, without makin a class is the following code going to do it's purpose and function correctly. I'm taking the userid and password and I want to check against a SQL table called User--All my information in three tables I want to store in Session variables. I'm drawing everything through stored procedures. I would prefer you resond to my actual email address until I'm back at work on Friday(if not dont' worry about it i'll eventually see what you posted back):


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Session("User") = ""
Session("empID") = ""
Session("SuperEmpID") = ""
Session("deptID") = ""
Session("currPeriod") = ""
Session("companyID") = ""
Session("facilityID") = ""
Session("Shift") = 0
Session("userName") = ""
Session("Role") = ""

End Sub

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim dataReader As SqlDataReader
Dim strPass, strUser As String

'Session("strUser") = txtUserID.Text
strUser = txtUserID.Text
strPass = txtPassword.Text

If strUser = "" Or strPass = "" Then
Response.Redirect("LogIn.aspx")
lblError.Text = "Your UserID or Password were incorrect. Please Try Again!"
End If

SqlCommand1.Parameters.Add(New SqlParameter("@dotnet.itags.org.strUser", strUser))
SqlCommand1.Parameters.Add(New SqlParameter("@dotnet.itags.org.strPass", strPass))

'Opens the connection to the DB
SqlConnection2.Open()

'Reads the data entered against SQL DB records
dataReader = SqlCommand1.ExecuteReader()

While dataReader.Read()
Session("User") = strUser
Session("SuperEmpID") = dataReader.GetString(1).ToString
Session("userName") = dataReader.GetString(2).ToString
Session("Role") = dataReader.GetString(3).ToString
Session("deptID") = dataReader.GetString(4).ToString
End While

dataReader.Close()

SqlCommand2.Parameters.Add(New SqlParameter("@dotnet.itags.org.deptID", Session("deptID")))
dataReader = SqlCommand2.ExecuteReader

While dataReader.Read
Session("companyID") = dataReader.GetString(1).ToString
Session("facilityID") = dataReader.GetString(2).ToString
Session("empID") = dataReader.GetString(4).ToString
Session("deptID") = dataReader.GetString(6).ToString
Session("Shift") = dataReader.GetString(8).ToString
End While

Response.Redirect("ReviewInterPersonal.aspx")
dataReader.Close()

'Closes Connection to DB
SqlConnection2.Close()
End Sub

much thanks,
jasonThe common method is to use Debug.Write to write out any messages you want. They'll show up in the output below when you run the application in the IDE.

The other thing I noticed is that you are using While loops. If you are only retrieving one row of data, you Simply use


DataReader.Read
Session("companyID) = dataReader.GetString(1).ToString

etc...

and then close it out like you are.

If you are retrieving multiple rows in your dataset, you need to be aware that each time you loop you are overwriting the prior values.

If you really need to store large amounts of data, you should consider using a DataSet with three datatable objects and then storing the DataSet in a Session Variable.

0 comments:

Post a Comment