无法执行if块语句

时间:2014-12-08 07:26:55

标签: vb.net

你好,我正在创建一个登录窗口 (usingms vb 2008)检查来自sql server(2014)的数据我的else块已被执行,当我给出任何错误的值但是当我给出正确的值时它没有被执行。

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

结束班

1 个答案:

答案 0 :(得分:0)

要解决的第一件事是:

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

虽然这可行,但它很麻烦并且容易引起奇怪的错误。而是使用String来存储您的值。

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