gpt4 book ai didi

asp.net - VB.NET-ASP.NET-不正确的用户名/密码(验证)

转载 作者:行者123 更新时间:2023-12-03 08:05:43 28 4
gpt4 key购买 nike

谁能从代码中告诉我代码中有什么问题?

如果用户名和密码不匹配,则lbl文本应显示“用户名/密码错误”。

码:

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

Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\PetLandia\App_Data\db.mdb")
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [User] where Username=? and Password=?", conn)

cmd.Parameters.AddWithValue("@Username", txtLogin.Text)
cmd.Parameters.AddWithValue("@Password", txtPassword.Text)




If (String.IsNullOrEmpty(txtLogin.Text)) Or (String.IsNullOrEmpty(txtPassword.Text)) Then

lblLoginError.Text = "One or more fields are empty. Please fill in all the fields"
lblLoginError.Visible = True

Else

conn.Open()
Dim read As OleDbDataReader = cmd.ExecuteReader()

Try

If read.HasRows Then

While read.Read()

If txtLogin.Text = read.Item("username").ToString And txtPassword.Text = read.Item("password").ToString Then


Dim tUsername As String = read.Item("Username").ToString

Session("Username") = tUsername
Response.Redirect("Default.aspx")


End If
End While
End If

read.Close()
Catch ex As Exception
Response.Write(ex.Message())
lblLoginError.Text = "Incorrect Username/Password."
lblLoginError.Visible = True

Finally
conn.Close()
End Try


End If

End Sub

最佳答案

您可以尝试此代码。此代码没有Try Catch块。

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

If (String.IsNullOrEmpty(txtLogin.Text)) Or (String.IsNullOrEmpty(txtPassword.Text)) Then
lblLoginError.Text = "One or more fields are empty. Please fill in all the fields"
lblLoginError.Visible = True

Else
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\PetLandia\App_Data\db.mdb")
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [User] where Username=? and Password=?", conn)
cmd.Parameters.AddWithValue("@Username", txtLogin.Text)
cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
conn.Open()
Dim read As OleDbDataReader = cmd.ExecuteReader()
If read.HasRows Then
read.Read()
Session("Username") = read.Item("Username").ToString
read.Close()
conn.Close() 'Close connection before Redirecting.
Response.Redirect("Default.aspx")
Else
read.Close()
conn.Close()
lblLoginError.Text = "Incorrect Username/Password."
lblLoginError.Visible = True

End If
End If
End Sub

关于asp.net - VB.NET-ASP.NET-不正确的用户名/密码(验证),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10464613/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com