I'm using VisualBasic.Net 2010 Pro, Windows Form Application, Win. 7, Wamp Server & MySQL Database
This CODE is from the Declarations Section of the main form.
Imports MySql.Data.MySqlClient Public Class Login Private mysql_host As String = "Localhost" Private mysql_player_log As String = "root" Private mysql_pass As String = "" Private mysql_db As String = "henderson_games_player_info" Public SQLConnect As String = "Server =" + mysql_host + ";" + "User ID=" + mysql_player_log + ";" + "Password=" + mysql_pass + ";" + "Database =" + mysql_db Private SQLConnection As New MySqlConnection
This is the Sub Code That I'm using.
Private Sub CheckNewUserReturn() Dim con As New MySqlConnection(SQLConnect) Dim cmd As MySqlCommand = con.CreateCommand con.Open() cmd.CommandText = "SELECT COUNT(*) FROM player_log" & " WHERE Last_Name = '" & TxtLastName.Text & "'" & " AND First_Name = '" & TxtFirstName.Text & "'" Dim Result = cmd.ExecuteScalar() If Convert.ToInt32(Result) > 0 Then 'user is valid MsgBox("User Exist") Else 'user is not valid MsgBox("User Does Not Exist") Dim MySQLStatement As String = "INSERT INTO player_log (Last_Name, First_Name) VALUES (' " & TxtLastName.Text & " ' , ' " & TxtFirstName.Text & " ')" MsgBox("User Does Not Exist") With cmd .CommandText = MySQLStatement .CommandType = CommandType.Text .ExecuteNonQuery() End With End If con.Close() End Sub
On the form I have 2 textboxes 1 named TxtLastName and another named TxtFirstName.
In MySQL DataBase I have a Database Named henderson_games_player_info
In that database I have a Table Named player_log.
In that Table I have a Column Last_Name and another First_Name.
I need to check if the record Exist
If record Exists then 'User Exists Else 'User does not Exists End If
Even if record Exists cmd.ExecuteScalar() is 0 'I assume this means false.
What am I doing wrong?