2014-12-08 3 views
2

hello all Я создаю окно входа в систему (usingms vb 2008), который проверяет данные с sql-сервера (2014), блок my else выполняется, когда я даю неправильное значение, но когда я давая правильное значение, он не был выполнен.не удалось выполнить оператор if 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 

End Class

+0

Именно эта линия не выполняется? –

+0

Не связывайте свой запрос sql, используйте параметры. Если пароль содержит «у вас будет ошибка. Кроме того, если ваш запрос проверяет конкретный пользователь с определенным паролем (не хешировал ?!), то я не вижу, что вам нужно проверить еще раз в инструкции if. –

+0

положил точку останова на ваш оператор IF, выполнив один шаг, проверив значения ваших переменных. –

ответ

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

как предложено u, когда я объявлен как указано выше, но проблема не решена. –

+0

Это строка, которая не была выполнена, когда сделаны правильные записи. Если (TextBox1.Text = t.Text And TextBox2.Text = t1.Text) Затем MessageBox .Show ("успешно войти в систему", "Вход", MessageBoxButtons.OK, MessageBoxIcon.Information) 'TextBox1.Text = "" TextBox2.Text = "" main.Show() Me.Hide() End If –

+0

Знаете ли вы, как установить точки останова и пройти через код? –

Смежные вопросы