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?

Recommended Answers

All 4 Replies

Let's look at the statement

Dim MySQLStatement As String = "INSERT INTO player_log (Last_Name, First_Name) VALUES (' " & TxtLastName.Text & " ' , ' " & TxtFirstName.Text & " ')"

If we plug in values "Jim" and "Jones" it evaluetes to

INSERT INTO player_log (Last_Name, First_Name) VALUES (' Jones ' , ' Jim ')

Do you see the problem? You are adding a blank at each end of the names. Try changing it to

Dim MySQLStatement As String = "INSERT INTO player_log (Last_Name, First_Name) VALUES ('" & TxtLastName.Text & "' , '" & TxtFirstName.Text & "')"

If you used parameterized queries as I suggested in your other thread on this topic you wouldn't have had this problem.

I used to use vb4 then moved to vb6. Life happened and I didn't do any programing for a long while. Now I am trying to learn VB.Net 2010 and it is very different than either of those 2. So basicly I'm starting over. I have never used databases before in any programing but I am trying to learn.

Can you walk me through on how to parameterize, or point me in the right direction.

I will apply the changes you have suggested.

Thanks Rev.

Is that all there is to setting parameters. I can do that. I didn't realize I was adding a space in the earlier code. Thanks for pointing that out. After correcting worked perfect.
Thanks so much for you help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.