In this vb.net form, I've created a login form which contains 3 fields:
1. User Name
2. Password
3. Combobox to select which which department the user belongs to.
For combobox, I've created a particular code:
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cn.Open()
'da = New SqlDataAdapter("select user_dept from user_master", cn)
'da.Fill(ds, "user_master")
'ComboBox1.Items.Clear()
da = New SqlDataAdapter("select user_dept from user_master", cn)
da.Fill(ds, "user_master")
ComboBox1.DataSource = ds.Tables("user_master")
ComboBox1.DisplayMember = "user_dept"
MetroComboBox1.DataSource = ds.Tables("user_master")
MetroComboBox1.DisplayMember = "user_dept"
End Sub
On button click, I've done the following code:
da = New SqlDataAdapter("select user_name,user_pass,user_dept from user_master", cn)
da.Fill(ds, "user_master")
dt = ds.Tables("user_master")
For Each dr In ds.Tables(0).Rows
If dr(0) = txtuname.Text And dr(1) = txtpw.Text And dr(2) = MetroComboBox1.Text Then
s = 1
Exit For
Else
s = 2
End If
Next
If s = 1 Then
MsgBox("Login Successful")
ElseIf s = 2 Then
MsgBox("Invalid Username or Password")
End If
End Sub
Still, this Exception Occurs:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Operator '=' is not defined for type 'DBNull' and string "some name or password".