我在VB.NET中實現了用戶驗證的代碼。當我在表單的文本框中輸入用戶名和密碼並單擊提交按鈕時,即使我已爲其編寫代碼,也不會顯示任何消息框。 try-catch塊中是否存在一些問題,或者我缺少一些代碼行?VB.NET中的用戶驗證
有人可以指出這段代碼有什麼問題嗎?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = " " Then
MsgBox("Enter a user id and password")
End If
TextBox1.Text = userid
TextBox2.Text = password
Try
myconnection = New SqlConnection("server=PARTH-PC\SQLEXPRESS;uid=sa;pwd=demo;database=fc")
'you need to provide password for sql server
myconnection.Open()
mycommand = New SqlCommand("select * from student where user id='" & TextBox1.Text & "' and password='" & TextBox2.Text & "')", myconnection)
dr = mycommand.ExecuteReader()
Catch ex As Exception
Finally
If (dr IsNot Nothing) Then
If (dr.Read()) Then
MsgBox("User is authenticated")
Form2.Show()
Else
MsgBox("Please enter correct username and password")
End If
End If
End Try
myconnection.Close()
End Sub
End Class
您是否收到任何錯誤?您的Form2.Show()是否按預期工作? – Tariqulazam
@Tariqulazam:沒有控制是不會在那如果阻止..i dnt knw爲什麼 –