ASP.net Session Variables aspnetdb login control

187 Views Asked by At

Hi people I am trying to carry a session variable for my login control which i am using to pass to pass to another page and display their details. I am using the aspNetDB. My username has been set to an integer as is connected to another database table displaying the users details below is my code:

    Protected Sub Login1_LoggingIn(sender As Object, e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles Login1.LoggingIn

            Session("StaffNo") = Login1.UserName()

        End Sub

The code is then being sent to my other page to display their details when loaded on a data grid but the session does not seem to be carrying:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try
            Dim Staff As String = CType(Session.Item("StaffNo"), String)
            Dim conn As SqlConnection
            Dim cmd As SqlCommand

            Dim cmdstring As String = "SELECT * FROM [User] WHERE studentnum = @StaffNo"
            conn = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True")
            cmd = New SqlCommand(cmdstring, conn)

            cmd.Parameters.Add("@StaffNo", SqlDbType.Char).Value = Staff
            conn.Open()

            Dim myReader As SqlDataReader

            myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)


            GridView1.DataSource = myReader
            GridView1.DataBind()

            conn.Close()
        Catch ex As Exception
            Response.Write("An error has occurred")
        End Try

    End Sub
1

There are 1 best solutions below

0
user3420973 On
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Try  
Dim cmdstring As String = "SELECT * FROM [User] WHERE studentnum = '"& Session("StaffNo").ToString & "' "

End Sub