无法执行if语句
你好我正在创建一个登录窗口 (usingms vb 2008),它检查来自sql server(2014)的数据我的else块被执行时我给出了任何错误的值,但是当我给它正确的价值它没有被执行。无法执行if语句
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ((RadioButton1.Checked = False And RadioButton2.Checked = False) Or (TextBox1.Text = "" Or TextBox2.Text = "")) Then
MessageBox.Show("enter user name/password then select login as user/admin", "error", MessageBoxButtons.OK, MessageBoxIcon.Information)
TextBox1.Clear()
TextBox2.Clear()
RadioButton1.Checked = False
RadioButton2.Checked = False
TextBox1.Focus()
Else
If ((RadioButton1.Checked = True And RadioButton2.Checked = False) And (TextBox1.Text <> "" And TextBox2.Text <> "")) Then
Try
ob.connection()
Dim sql As String = "select * from password where loger='user0'" + "and (name=" + "'" + TextBox1.Text + "'" + "and password=" + "'" + TextBox2.Text + "')"
ob.Mydata(sql)
If ob.dr.Read() Then
Dim t, t1, t2 As New TextBox
t.Text = ob.dr.Item("name").ToString
t1.Text = ob.dr.Item("password").ToString
t2.Text = ob.dr.Item("loger").ToString
If (TextBox1.Text = t.Text And TextBox2.Text = t1.Text) Then
MessageBox.Show("successfully login", "login", MessageBoxButtons.OK, MessageBoxIcon.Information)
' TextBox1.Text = ""
TextBox2.Text = ""
main.Show()
Me.Hide()
End If
Else
MessageBox.Show(" invalid userid/password.........", "log in error", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox1.Clear()
TextBox2.Clear()
RadioButton2.Checked = False
TextBox1.Focus()
End If
Catch ex As Exception '' getting Sql exception
MessageBox.Show(ex.Message.ToString())
Finally
ob.connection_close()
End Try
Else
If ((RadioButton2.Checked = True And RadioButton1.Checked = False) And (TextBox1.Text <> "" And TextBox2.Text <> "")) Then
Try
ob.connection()
Dim sql As String = "select * from password where loger='admin'" + "and (name=" + "'" + TextBox1.Text + "'" + "and password=" + "'" + TextBox2.Text + "')"
ob.Mydata(sql)
If ob.dr.Read() Then
Dim t As New TextBox
Dim t1, t2 As New TextBox
t.Text = ob.dr.Item("name").ToString
t1.Text = ob.dr.Item("password").ToString
t2.Text = ob.dr.Item("loger").ToString
If (TextBox1.Text = t.Text And TextBox2.Text = t1.Text) And RadioButton2.Text = t2.Text Then
MessageBox.Show("successfully login", "login", MessageBoxButtons.OK, MessageBoxIcon.Information)
'TextBox1.Text = ""
TextBox2.Text = ""
main.Show()
Me.Hide()
End If
Else
MessageBox.Show(" invalid userid/password......", "login error", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox1.Clear()
TextBox2.Clear()
RadioButton2.Checked = False
TextBox1.Focus()
End If
Catch ex As Exception '' getting Sql exception
MessageBox.Show(ex.Message.ToString())
Finally
ob.connection_close()
End Try
End If
End If
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
末级
解决的第一件事情是这样的:
Dim t As New TextBox
Dim t1, t2 As New TextBox
t.Text = ob.dr.Item("name").ToString
t1.Text = ob.dr.Item("password").ToString
t2.Text = ob.dr.Item("loger").ToString
虽然可以工作,它是凌乱和容易引起你奇怪的错误。而是使用字符串来存储您的值。
Dim t As String
Dim t1, t2 As String
t = ob.dr.Item("name").ToString
t1 = ob.dr.Item("password").ToString
t2 = ob.dr.Item("loger").ToString
由u建议当我声明像上面,但问题没有解决 – 2014-12-08 10:49:30
这是行,当执行正确的条目不被执行如果(TextBox1.Text = t.Text和TextBox2.Text = t1.Text)然后 MessageBox .Show( “成功登录”, “登录”,MessageBoxButtons.OK,MessageBoxIcon.Information) 'TextBox1.Text = “” TextBox2.Text = “” main.Show() Me.Hide() End If – 2014-12-08 10:51:50
您是否知道如何设置断点并逐步完成代码? – 2014-12-08 19:34:55
究竟哪一行没有被执行? – 2014-12-08 07:40:20
不要连接你的sql查询,使用参数。如果密码包含'你将会有错误。另外,如果您的查询使用特定密码检查特定用户(而不是散列?!),那么我在您的if语句中看不到需要再次检查的内容。 – 2014-12-08 13:54:55
在您的IF语句中放置一个断点,通过它验证变量的值。 – 2014-12-08 14:32:27