Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear attached is my project I wanted to store rows from datagridview to mysql table but I am facing the error in the image kindly help me.
http://ansicollege.net/vbcode/error.png[^]

This the error I get.

Following is the code line I get error in
VB
dgvcc = DirectCast(DataGridView1.Rows(i).Cells(1), DataGridViewComboBoxCell)
.Parameters.AddWithValue("@product", dgvcc.Value)

I store values in combobox of Datagridview, product_id datatype in database is int


Here is my project http://ansicollege.net/vbcode/TsoftPOS.zip[^]

What I have tried:

VB
Public Sub CreateBill()
        Dim sql2 As String
        Try
            ConnDB()


            Dim dgvcc As New DataGridViewComboBoxCell

            For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1

                sqL = "INSERT INTO `dailysale`(`billno`,`customer_id`, `product_id`, `qty`, `price`, `total`, `description`) VALUES(@billno,@custid,@product, @qty, @price, @total, @desc)"
                cmd = New MySqlCommand(sqL, conn)
                'datatype integer
                With cmd
                    '   .Parameters.Clear()
                    .Parameters.AddWithValue("@billno", txtbillno.Text)
                    .Parameters.AddWithValue("@custid", txtCustomer.Tag)
                    dgvcc = DirectCast(DataGridView1.Rows(i).Cells(1), DataGridViewComboBoxCell)
                    .Parameters.AddWithValue("@product", dgvcc.Value)
                    .Parameters.AddWithValue("@qty", Me.DataGridView1.Rows(i).Cells(2).Value)
                    .Parameters.AddWithValue("@price", Me.DataGridView1.Rows(i).Cells(3).Value)
                    .Parameters.AddWithValue("@total", Me.DataGridView1.Rows(i).Cells(4).Value)
                    .Parameters.AddWithValue("@desc", Me.DataGridView1.Rows(i).Cells(5).Value)
                End With
  '              cmd = New MySqlCommand(sqL, conn)
                cmd.ExecuteNonQuery()
            Next
            sql2 = "INSERT INTO customer_payments(customer_id, billno, cdate, bill_amount, paid_amount, discount, ret_amount, balance) VALUES ('" & txtCustomer.Tag & "','" & txtbillno.Text & "','" & DateTimePicker1.Value & "','" & txtbill_amount.Text & "','" & txtpaid_amount.Text & "','" & txtdiscount.Text & "','" & txtret_amount.Text & "','" & txttotalbalance.Text & "')"
            cmd = New MySqlCommand(sqL, conn)
            cmd.ExecuteNonQuery()
            MsgBox("New Bill successfully created.", MsgBoxStyle.Information, "Create Bill")
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            cmd.Dispose()
            conn.Close()
        End Try
Posted
Updated 21-May-17 23:39pm
v3
Comments
[no name] 20-May-17 15:35pm    
And? Which part of the error message is it that you do not understand? No matter how much you try to avoid learning new things, you simply MUST learn how to use the debugger to debug your code.
CHill60 20-May-17 15:39pm    
Why do you correctly use a parameterised query for sql and then expose your database to SQL Injection with sql2?
The error is quite clear - for one of the values of I there is nothing in DataGridView1.Rows(i).Cells(1). It's probably the last row - do you have DataGridView1.AllowUserToAddRows set to false?

1 solution

As the error message says 'product_id' can not be null, depending on how your table is defined you will have to either supply a value for it, or DEFAULT.
See: MySQL :: MySQL 5.7 Reference Manual :: 13.2.5 INSERT Syntax[^]
You probably need another cast to the type of 'product_id' in:
dgvcc.Value
 
Share this answer
 
v2
Comments
nyt1972 21-May-17 2:51am    
Product_id has a value, and in debug it show me the value in variable as well, but even then it gives me that error.
RickZeeland 21-May-17 2:56am    
Then maybe it's time to use another database, may I suggest PostgreSQL in combination with NpgSql :)
nyt1972 22-May-17 5:37am    
Thanks a lot for yours responses, finally I managed to correct the error finding in debug that the error is because of the last empty row in datagridview.

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