2015-08-19 42 views
0

我想開會,但我不知道從哪裏開始。我登錄自己,但沒有使用會議,我必須進行會議。以下是我迄今爲止所做的代碼。如何在我的代碼中使用會話使用asp.net vb.net

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 

請通過在此代碼中推薦您的建議來幫助我。

回答

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可以用於存儲陣列,以及其它類型的的數據。例如:

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()

您可以將某些數據存儲到會話,並在你的web應用程序的任何地方使用它。

btw。在你的代碼,一點更好的方法是:

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 

當然,你必須要注意的sessiontimeout ....你可以閱讀提供的鏈接更多的對這個職位的開始。

0

簡單,因爲這...

Session("Username") = txt_username.Text 

之後,你可以得到session的每一頁上的價值。