Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        openCon()
        MsgBox()
        con.Close()
        loadTable()
    End Sub

    Private Sub Button_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_add.Click
        openCon()

        Try
            cmd.Connection = con
            cmd.CommandText = "INSERT INTO products (`ID`,`NAME`,`PRICE`,`QUANTITY`,`DESCRIPTION`) VALUES (" & TextBox_pID.Text & ",'" & TextBox_pName.Text & "'," & TextBox_pPrice.Text & "," & TextBox_pQty.Text & ",'" & TextBox_pDesc.Text & "')"
            cmd.ExecuteNonQuery()

            MsgBox("Product Successfully Added!")
            con.Close()
            
            TextBox_pID.Clear()
            TextBox_pName.Clear()
            TextBox_pPrice.Clear()
            TextBox_pQty.Clear()
            TextBox_pDesc.Clear()

            loadTable()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub TextBox_pID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox_pID.TextChanged
        openCon()

        Try
            cmd.Connection = con
            cmd.CommandText = "SELECT * FROM products WHERE ID='" & TextBox_pID.Text & "'"
            adapter.SelectCommand = cmd
            data.Clear()
            adapter.Fill(data, "List")

            TextBox_pName.DataBindings.Add("Text", data, "List.NAME")
            TextBox_pName.DataBindings.Clear()

            If TextBox_pID.Text = "" Then
                TextBox_pName.Clear()
            End If

            con.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub loadTable()
        openCon()

        Try
            cmd.Connection = con
            cmd.CommandText = "SELECT * FROM products"
            adapter.SelectCommand = cmd
            table.Clear()
            adapter.Fill(table)
            DataGridView1.DataSource = table
            con.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

End Class




I'm Using Visual Basic 2010 Express

Plss Help! Thanks!

What I have tried:

I have tried Changing the Values but nothing happend
Posted
Updated 18-May-22 19:09pm

1 solution

First off, don't do it like that! 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?

When you have fixed that through your whole app, use the debugger to locate exactly where the problem occurs - there are a couple of Fills in thet code, and I can't see anything wrong with either (other than the recycling Connection and Adapters) so you need to find the actual code that causes the problem and look at what is and isn't setup with it - which needs you code running with your DB and we have no access to either!
 
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