2014-12-08 158 views
2

你好我正在創建一個登錄窗口 (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 

末級

+0

究竟哪一行沒有被執行? – 2014-12-08 07:40:20

+0

不要連接你的sql查詢,使用參數。如果密碼包含'你將會有錯誤。另外,如果您的查詢使用特定密碼檢查特定用戶(而不是散列?!),那麼我在您的if語句中看不到需要再次檢查的內容。 – 2014-12-08 13:54:55

+0

在您的IF語句中放置一個斷點,通過它驗證變量的值。 – 2014-12-08 14:32:27

回答

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 

雖然可以工作,它是凌亂和容易引起你奇怪的錯誤。而是使用字符串來存儲您的值。

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 
+0

由u建議當我聲明像上面,但問題沒有解決 – 2014-12-08 10:49:30

+0

這是行,當執行正確的條目不被執行如果(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

+0

您是否知道如何設置斷點並逐步完成代碼? – 2014-12-08 19:34:55