0

请原谅我,自从我使用 VB.Net 以来已经有一段时间了,所以这可能很容易。

我有一个带有文本框、组合框和按钮的简单登录表单。组合框绑定到 SQL 数据库并显示用户表中的用户名。这个想法是您选择一个名称,输入密码,然后登录到一个新表单(这部分效果很好!)。如果您输入了错误的密码,表单会弹出一条消息,显示“无效”。在这个阶段出于某种原因,组合框变为空白。下拉列表现在在名称曾经存在的地方有空格。在此处输入带有密码的名称会导致对象引用错误。我已经尝试了一切来重置组合框并搜索网络,但没有任何效果!!!

(我知道这不是安全登录,这只是将数据绑定到组合框并对照 SQL 表检查它的简单示例)。谢谢你的帮助!

enter code here


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BTN_Logon.Click  

    Try  
    Dim STR_User As String = Users_nameComboBox.SelectedValue  
    Dim STR_Pwd As String = TB_Pwd.Text  
    STR_User = STR_User.Trim  

    'MessageBox.Show(STR_User & "|" & STR_Pwd)'Used to verify this stage works  
    Dim numRecords As Int16 = UsersTableAdapter.FillByLogin(Me.Login_testDataSet.users, STR_User, STR_Pwd)  
    'MessageBox.Show(numRecords) 'Used to verify this stage works  

     If (numRecords > 0) Then
     Dim DB_User As String = Me.Login_testDataSet.users(0).users_name.Trim
     Dim DB_Pwd As String = Me.Login_testDataSet.users(0).users_pwd.Trim
     'MessageBox.Show(DB_User & "|" & DB_Pwd)'Used to verify this stage works

     If DB_User.Equals(STR_User) And DB_Pwd.Equals(STR_Pwd) Then
     'User is authenticated for application
     'This section works (removed to cleanup code)

     Else
     MessageBox.Show("ERROR" & STR_User & "|" & STR_Pwd) 'Shows that this stage works
     'Need to figure out why combobox doesn't refill, using restart for now
     'Application.Restart()

     End If
     End If

     MessageBox.Show("ERROR" & STR_User & "|" & STR_Pwd) 'Shows that this stage works
     'Need to figure out why combobox doesn't refill, using restart for now
     'Application.Restart()


     Catch ex As Exception
     MessageBox.Show(ex.Message)
     'Need to figure out why combobox doesn't refill, using restart for now
     'Application.Restart()

     End Try

     End Sub









     InitializeComponent

     'Users_nameComboBox

     Me.Users_nameComboBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", 
     Me.UsersBindingSource, "users_name", True))
     Me.Users_nameComboBox.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.UsersBindingSource, "users_name", True))
     Me.Users_nameComboBox.DataSource = Me.UsersBindingSource
     Me.Users_nameComboBox.DisplayMember = "users_name"
     Me.Users_nameComboBox.Name = "Users_nameComboBox"
     Me.Users_nameComboBox.ValueMember = "users_name"
4

0 回答 0