如何使用asp.net vb.net在我的代码中使用会话

时间:2015-08-19 17:01:35

标签: asp.net vb.net session

我想开会,但我不知道从哪里开始。我自己登录,但没有使用会话,我必须进行会话。以下是我到目前为止所做的代码。

Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click

    If txt_username.Text = "" Or txt_password.Text = "" Then
        error_usr_invalid.Visible = False
        If txt_username.Text = "" Then
            error_usr_blank.Visible = True
        ElseIf txt_username.Text <> "" Then
            error_usr_blank.Visible = False
        End If
        If txt_password.Text = "" Then
            error_pwd_blank.Visible = True
        ElseIf txt_username.Text <> "" Then
            error_pwd_blank.Visible = False
        End If
    ElseIf txt_username.Text <> "" And txt_password.Text <> "" Then
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        con.Open()
        Dim cmd As New SqlCommand("select * from users where username = '" + txt_username.Text + "' and Password = '" + txt_password.Text + "'", con)
        cmd.Parameters.AddWithValue("@username", txt_username.Text)
        cmd.Parameters.AddWithValue("@password", txt_password.Text)
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        da.Fill(dt)
        If dt.Rows.Count > 0 Then
            error_usr_invalid.Visible = False
            error_usr_blank.Visible = False
            error_pwd_blank.Visible = False

            Response.Redirect("main.aspx")
        Else
            error_usr_invalid.Visible = True
            error_usr_blank.Visible = False
            error_pwd_blank.Visible = False
        End If
    End If

请在此代码中提出建议,以帮助我。

2 个答案:

答案 0 :(得分:1)

您可以在此处阅读有关会话的内容:ASP.NET Session State Overview

现在,只有几个例子:

为会话添加值:Session.Add("username", txt_username.Text)

从会话中获取价值:Dim username As String = Session("username")

例如:您可以将用户名存储到Session("username")并在其他页面上使用(例如在main.aspx中),直到您将其删除。

又一个例子:

Default.aspx的

Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
  Session.Add("username", txt_username.Text)
  Response.Redirect("main.aspx")
End Sub

main.aspx

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  Label1.Text = Session("username")
End Sub

会话可用于存储数组和其他类型的数据。例如:

Session.Add("list", List(Of String))
Session("list").Add("record #1")
Session("list").Add("record #2")
Session("list").Add("record #3")

然后在其他地方使用它:

For x = 0 to Session("list").Count - 1
  Label1.Text = Session("list")(x) + ", "
Next

删除会话:Session.Remove("username")(此会话项目将被删除)

当您完成工作后(例如您注销),您可以删除,清除,放弃会话。

Session.RemoveAll() : Session.Clear() : Session.Abandon()

您可以将一些数据存储到会话中,并在您的网络应用中的任何位置使用它。

顺便说一句。在您的代码中,更好的方法是:

Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
If con.State = ConnectionState.Open Then con.Close()
con.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ToString
con.Open()
Dim cmd As New SqlCommand("select * from users where username = @username and Password = @password;", con)
cmd.Parameters.AddWithValue("@username", txt_username.Text)
cmd.Parameters.AddWithValue("@password", txt_password.Text)
Dim tds As SqlDataReader = cmd.ExecuteReader
Session.Add("isexist", tds.HasRows)   'there is example how to store result into session (in this case result value will be True or False)
tds.Close(): cmd.Dispose(): con.Close()  'avoid connection stay opened
If Session("isexist") = True Then
  'user successful log in, session("isexist") is True
  Session.Remove("isexist")
  Response.Redirect("main.aspx")
Else
  'username or password doesn't exist in database, session("isexist") is False
  Session.Remove("isexist") 
 'do what You want
End If

当然,您必须注意session timeout ....您可以通过本帖子开头提供的链接阅读更多内容。

答案 1 :(得分:0)

这很简单......

    Session("Username") = txt_username.Text

之后,您可以在每个页面上获得session的值。