Click here to Skip to main content
15,895,471 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can you guys please help me to solve this? When I'm going to insert data to the data base I got a "
fatal error encountered during command execution
" Error. So Can You please help me on my code whether there is any mistake in it?

What I have tried:

private void btn_save_Click(object sender, EventArgs e)
{
    try
    {
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            MySqlCommand cmd3 = new MySqlCommand();
            cmd3.Connection = con;

            cmd3.CommandText = "insert into sales-product(sales-id, product-name, qty, price, gross-total)values(@sales-id, @product-name, @qty, @price, @gross-total)";
            cmd3.Parameters.AddWithValue("@sales-id", lbl_invoice.Text);
            cmd3.Parameters.AddWithValue("@product-name", dataGridView1.Rows[i].Cells[2].Value);
            cmd3.Parameters.AddWithValue("@qty", dataGridView1.Rows[i].Cells[3].Value);
            cmd3.Parameters.AddWithValue("@price", dataGridView1.Rows[i].Cells[4].Value);
            cmd3.Parameters.AddWithValue("@gross-total", dataGridView1.Rows[i].Cells[5].Value);

            MySqlCommand cmd4 = new MySqlCommand();
            cmd4.Connection = con;

            cmd4.CommandText = "insert into sales(id, date, time, qty, gross-total)values(@id, @date, @time, @qty, @gross-total)";
            cmd4.Parameters.AddWithValue("@id", lbl_invoice.Text);
            cmd4.Parameters.AddWithValue("@date", lbl_date.Text);
            cmd4.Parameters.AddWithValue("@time", lbl_time.Text);
            cmd4.Parameters.AddWithValue("@qty", lbl_items.Text);
            cmd4.Parameters.AddWithValue("@gross-total", lbl_total.Text);

            con.Open();
            cmd3.ExecuteNonQuery();
            cmd4.ExecuteNonQuery();
            MessageBox.Show("Record Successfully Inserted !!");
            con.Close();
        }
    }

    catch (Exception ee)
    {
        MessageBox.Show(ee.Message, "Record Not Successfully Inserted !!");
    }
}
Posted
Updated 29-May-19 5:28am
v2
Comments
W Balboos, GHB 27-May-19 14:36pm    
Debugger . . . . ???

Maybe you should do one command at a time ... and check the result of the first before doing the second ... and maybe confirm whether or not there are any "referential integrity" rules being violated by not following proper procedures.

Maybe consider a "transaction".
 
Share this answer
 
C#
cmd3.ExecuteNonQuery();
cmd4.ExecuteNonQuery();
MessageBox.Show("Record Successfully Inserted !!");

Do not do this. You have ignored the return values of both commands so you have no idea whether your records were successfully inserted. If this code is part of a real business application then your company is heading for the rocks.
 
Share this answer
 
If you are using an auto increment id field, it must not be included in the insert query.
 
Share this answer
 
Quote:
C#
cmd3.CommandText = "insert into sales-product(sales-id, product-name, qty, price, gross-total)values(@sales-id, @product-name, @qty, @price, @gross-total)";
...
cmd4.CommandText = "insert into sales(id, date, time, qty, gross-total)values(@id, @date, @time, @qty, @gross-total)";

Unquoted table, column, and parameter names may not contain a minus sign (-). They can only contain letters, numbers, underscores (_), or the dollar sign ($).

MySQL :: MySQL 8.0 Reference Manual :: 9.2 Schema Object Names[^]

If at all possible, you should rename your columns to follow these rules. If not, you will need to quote them in any query.
C#
cmd3.CommandText = "insert into `sales-product` (`sales-id`, `product-name`, qty, price, `gross-total`) values (@SalesId, @ProductName, @qty, @price, @GrossTotal)";
...
cmd4.CommandText = "insert into sales (id, date, time, qty, `gross-total`) values (@id, @date, @time, @qty, @GrossTotal)";
 
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