Reputation: 37
How to enable Groupbox if the confirmation is equal to the database?
I have txtpass.text which will be input for confirmation, if the the value in txtpass is equal to my database the groupbox will now enable. Is there something wrong in my code?
This is my code:
Private Sub txtpass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtpass.KeyPress
disconnect()
connect()
cmd = New Odbc.OdbcCommand("SELECT pass FROM secure", con)
dr = cmd.ExecuteReader
If dr.Read AndAlso dr("pass").ToString = txtpass.Text Then
gbscanpnum.Enabled = False
gbscanpnum.Focus()
End If
End Sub
Upvotes: 1
Views: 47
Reputation: 81675
Use parameters to avoid SQL Injection.
cmd = New Odbc.OdbcCommand("SELECT * FROM secure WHERE pass=@pass", con)
cmd.Parameters.AddWithValue("@pass", txtpass.Text.Trim)
For your code, Read just sets the cursor position of the reader, you still have to read the individual field.
If dr.Read AndAlso dr("pass").ToString = txtpass.Text Then
gbscanpnum.Enabled = True
End If
or if receiving one or more rows means it worked, then just:
gbscanpnum.Enabled = dr.HasRows
If expecting one row and one column, ExecuteScalar would work, too.
Your method is called txtfeedernum_Enter
but it is being handled by the txtpass.VisibleChanged
event. That's kind-of messed up. I'm guessing you should have a button and its click event handle this authentication.
Upvotes: 1