Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Private Sub btnReg_Click(sender As Object, e As EventArgs) Handles btnReg.Click

      Try


          If cn.State.ToString = "Closed" Then
              DbConnection()
          End If

          cm = New MySqlCommand("INSERT INTO tbclientreg (rfidno, lname, fname, mi, gender, bday, age, address, contactno)
          VALUES (@rfidno, @lname, @fname, @mi, @gender, @bday, @age, @address, @contactno)", cn)
              With cm
                  .Parameters.AddWithValue("@rfidno", txtrfid.Text)
                  .Parameters.AddWithValue("@lname", txtlname.Text)
                  .Parameters.AddWithValue("@fname", txtfname.Text)
                  .Parameters.AddWithValue("@mi", txtmi.Text)
                  .Parameters.AddWithValue("@gender", cbgender.SelectedItem)
                  .Parameters.AddWithValue("@bday", dtbday.Value)
                  .Parameters.AddWithValue("@age", txtage.Text)
                  .Parameters.AddWithValue("@address", txtaddress.Text)
                  .Parameters.AddWithValue("@contactno", txtcontact.Text)
                  .ExecuteNonQuery()
              End With
              cn.Close()
              MsgBox("Successfully Registered!")



      Catch ex As Exception
          cn.Close()
          MsgBox(ex.Message, vbCritical)
      End Try



  End Sub


What I have tried:

Private Sub btnReg_Click(sender As Object, e As EventArgs) Handles btnReg.Click

       Try


           If cn.State.ToString = "Closed" Then
               DbConnection()
           End If

           Dim sqlQRY As String = "SELECT COUNT(*) AS numRows FROM tbclientreg WHERE rfidno = '" & txtrfid.Text & "'"
           Dim queryResult As Integer


           cmd = New MySqlCommand(sqlQRY, cn)
           sqlQRY = cmd.ExecuteScalar()

           cn.Close()

           If queryResult > 0 Then
               MsgBox("Already Exist!,MessageBoxButtons.OK, MessageBoxIcon.Information")

           Else
               cm = New MySqlCommand("INSERT INTO tbclientreg (rfidno, lname, fname, mi, gender, bday, age, address, contactno)
           VALUES (@rfidno, @lname, @fname, @mi, @gender, @bday, @age, @address, @contactno)", cn)
               With cm
                   .Parameters.AddWithValue("@rfidno", txtrfid.Text)
                   .Parameters.AddWithValue("@lname", txtlname.Text)
                   .Parameters.AddWithValue("@fname", txtfname.Text)
                   .Parameters.AddWithValue("@mi", txtmi.Text)
                   .Parameters.AddWithValue("@gender", cbgender.SelectedItem)
                   .Parameters.AddWithValue("@bday", dtbday.Value)
                   .Parameters.AddWithValue("@age", txtage.Text)
                   .Parameters.AddWithValue("@address", txtaddress.Text)
                   .Parameters.AddWithValue("@contactno", txtcontact.Text)
                   .ExecuteNonQuery()
               End With
               cn.Close()
               MsgBox("Successfully Registered!")


           End If
       Catch ex As Exception
           cn.Close()
           MsgBox(ex.Message, vbCritical)
       End Try
Posted
Updated 15-Mar-21 3:58am

If you don't understand code, why use it?

And if you do understand it, why suddenly switch from code that isn't prone to SQL Injection to code that is? Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And when you have fixed that throughout your whole app, use the debugger to find out what the SELECT returns, and compare that with your DB content. We can't do that for you!
 
Share this answer
 
Don't rely on checking for duplicates and then inserting the record, unless you wrap both commands in a serializable transaction.

Without a transaction, another request could come in at the same time. Both requests see that the record doesn't exist. Both requests insert the same record. And you now have duplicate records in your database.

The simplest solution is to create a UNIQUE constraint / index on the column which needs to be unique. When you try to insert a record, if the same value has already been inserted, you will get an exception.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900