Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        If Namount.Text = Nothing Then
            MsgBox("insert data first")

        Else

            Dim str, str1 As String

            str = "insert into customer values(" & Val(bill.Text) & ",'" & dtp.Value & "','" & cName.Text & "','" & Address.Text & "','" & paymenttype.Text & "'," & Val(phoneno.Text) & "," & Val(Tamount.Text) & "," & Val(Discount.Text) & "," & Val(Namount.Text) & ")"


            cmd = New OleDbCommand(str, cnn)
            dap = New OleDbDataAdapter(cmd)
            ds = New DataSet
            dap.Fill(ds)


            Dim i As Integer
            Dim Barcode, Item_name As String

            Dim Qty, item_rate, i_amt As String


            For i = 0 To DataGridView1.RowCount - 1
                Barcode = DataGridView1.Rows(i).Cells(0).Value
                Item_name = DataGridView1.Rows(i).Cells(1).Value
                Qty = DataGridView1.Rows(i).Cells(2).Value
                item_rate = DataGridView1.Rows(i).Cells(3).Value
                i_amt = DataGridView1.Rows(i).Cells(4).Value




                str1 = "insert into item values(" & bill.Text & ", " & Barcode & ", '" & Item_name & "'," & item_rate & "," & Qty & "," & i_amt & ")"

                cmd = New OleDbCommand(str1, cnn)
                dap = New OleDbDataAdapter(cmd)
                ds = New DataSet
                dap.Fill(ds)

            Next
            MessageBox.Show("Record Added")


            bill.Clear()
            paymenttype.ResetText()
            cName.Clear()
            Address.Clear()
            phoneno.Clear()
            DataGridView1.Rows.Clear()
            Tamount.Text = "00"
            Discount.Clear()
            Namount.Clear()
        End If
Posted

1 solution

You'll have to pass the data correctly to your SQL statement. The way you've created your insert statement is probably missing quotes (can't tell without knowing what the table looks like), so you'll get the error from that.

But instead of constructing a String containing your insert statement, use a prepared statement. This has a couple of advantages: you don't have to worry about data types, escaping quotes in the text, or things such as SQL injection. You can find such an example at MSDN[^].
 
Share this answer
 
v2
Comments
Navi Sandhu 20-Jan-14 8:47am    
I m not familier with prepare statement . may u make correction in my coding of submit button bt putting prepare statement instead of insert.
Maarten Kools 20-Jan-14 8:51am    
The example at MSDN, which I linked in my answer, uses an INSERT statement. The idea is that you have a statement, where the values are passed as parameters. The parameters are specified separately in the command. The prepare statement will then "compile" the actual statement before you execute it.

You can find more info about prepared statements here[^] and here[^]
Navi Sandhu 20-Jan-14 8:59am    
thank u sir i will try

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