简体   繁体   中英

Session ASP.NET doesnt seem to work

I have the following code on my default.aspx

Label1.Text = Session("valueName").ToString()

And the following code on my login.aspx

Dim strCon As String = ConfigurationManager.ConnectionStrings("Goed").ConnectionString

    'Create Connection String And SQL Statement
    Dim strSelect As String = "SELECT COUNT(*) FROM tbl_LogIn WHERE Gebruiker = @Gebruiker AND Wachtwoord = @Wachtwoord"

    Dim con As New SqlConnection(strCon)
    Dim cmd As New SqlCommand()
    cmd.Connection = con
    cmd.CommandType = CommandType.Text
    cmd.CommandText = strSelect

    Dim Gebruiker As New SqlParameter("@Gebruiker", _
                                      SqlDbType.VarChar)
    Gebruiker.Value = TxtUs.Text.Trim().ToString()
    cmd.Parameters.Add(Gebruiker)

    Dim Wachtwoord As New SqlParameter("@Wachtwoord", _
                                       SqlDbType.VarChar)
    Wachtwoord.Value = TxtPw.Text.Trim().ToString()
    cmd.Parameters.Add(Wachtwoord)


    con.Open()

    Dim result As Integer = DirectCast(cmd.ExecuteScalar(), Int32)
    con.Close()

    If result >= 1 Then
        Response.Redirect("default.aspx")

        Session("valueName") = TxtUs.Text.ToString()


    Else
        lblMsg.Text = "Gebruikers naam en of wachtwoord kloppen niet"
    End If

End Sub

But it doesnt seem to help. I don't get any error or whatso ever, any idea?

The Redirect method ends the execution, so you have to set the session variable before redirecting:

Session("valueName") = TxtUs.Text.ToString()
Response.Redirect("default.aspx")

you are redirecting before setting the session. you should first set the session and then redirect the page.

Or

Your result variable is not 1 or bigger than 1. you should check that too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM