I'm looking for code that will help me check a particular column in MySQL and return its value if it already exists. I'm working on ForgotPassword module, so when the user clicks "Forgot Password", a form appears that will ask the user to input his/her username. Once he's/she's done, it will check the system to see if the entered username exists. I found some code here on Stack Overflow:
Private Sub btnCheckUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckUser.Click
If IsUserExist(userName:=True) Then
MsgBox("user exists")
Else
MsgBox("user does not exists")
End If
End Sub
Private Function IsUserExist(ByVal userName As String) As Boolean
Dim query As String
Dim returnValue As Boolean = False
query = "SELECT username FROM dbase.tblusers WHERE username = @username "
Using conn As New MySqlConnection("server=localhost; userid=root; password=root; database=dbase")
Using cmd As New MySqlCommand()
With cmd
.Connection = conn
.CommandText = query
.CommandType = CommandType.Text
.Parameters.AddWithValue("@username", txtUsername.Text)
End With
Try
conn.Open()
If CInt(cmd.ExecuteScalar()) > 0 Then
returnValue = True
End If
Catch ex As MySqlException
MsgBox(ex.Message)
returnValue = False
Finally
conn.Close()
End Try
End Using
End Using
Return returnValue
End Function
But this code gives me an error Conversion from string "username" to type 'Integer' is not valid
at If CInt(cmd.ExecuteScalar()) > 0 Then
.