I need a little help with this.
I don't usually use a DataReader in this way but now have a need for a one way, forward communication this allows.
Anyway I got this code for the reader off the net but what it doesn't seem to handle is the null values in the db.
The repro is:
I do have a matching username and pcname but the user choose not to password protect, so the Password field is null.
any guidance in using a datareader and dbnulls?Code:Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click If txtUsername.Text = "" Then MessageBox.Show("Please complete the required fields..", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Else ' Check if user exist in OneData database ' Connect to DB Dim conn As New System.Data.OleDb.OleDbConnection() conn.ConnectionString = My.Settings.OneDataConnectionString Try 'Select the Staff record that matches the username, pcname and password Dim sql As String = "SELECT * FROM IDStaff WHERE StaffName= '" & Trim(txtUsername.Text) & "'" & _ " AND PCNAME= '" & Trim(txtPcName.Text) & "'" & " AND [Password] = '" & Trim(txtPassword.Text) & "'" Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql) 'Open Database Connection sqlCom.Connection = conn conn.Open() Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader() 'Check if this user is in the database If sqlRead.Read() Then 'Yes, username/password/pcname check has passed UserModule.PCName = txtPcName.Text UserModule.UserName = txtUsername.Text Me.Close() Else ' No username/password/pcname checked failed ' Throw an error message MessageBox.Show("The Username/MachineName combination is either incorrect or not registered in the system..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 'Focus on Username field txtUsername.Focus() End If ' Close and dispose of the connection resource conn.Close() conn.Dispose() Catch ex As Exception MessageBox.Show("Failed to connect to Database..", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If End Sub
I know how to check a data row for dbnull but since I'm not going that route leaves me at a lose.




Reply With Quote