Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have been getting the above error and I can't figure it out but this is the code I tried:

What I have tried:

C#
string constring = "Data Source=AnyStore.db;Version=3;New=True;Compress=True;";
            SQLiteConnection con = new SQLiteConnection(constring);
            SQLiteTransaction transaction = con.BeginTransaction();
            try
            {
                con.Open();
                foreach(DataGridViewRow row in dataGridView1.Rows)
                {
                    using (SQLiteCommand com = new SQLiteCommand("insert into tbl_Details ([Date],[Serial_Number],[ItemId],[Item_Name],[Quantity],[Unitt_Price],[Total_Price],[Amount],[Cashier_Name]) values(@Date,@Serial_Number,@ItemId,@Item_Name,@Quantity,@Unitt_Price,@Total_Price,@Amount,@Cashier_Name)", con))
                    {
                        com.Parameters.AddWithValue("@Date", dtTrans.Text);
                        com.Parameters.AddWithValue("@Serial_Number", txtSerial.Text);
                        com.Parameters.AddWithValue("@Amount", txtTotalToPay.Text);
                        com.Parameters.AddWithValue("@ItemId", row.Cells["ItemId"].Value);
                        com.Parameters.AddWithValue("@Quantity", row.Cells["Quantity"].Value);
                        com.Parameters.AddWithValue("@Unitt_Price", row.Cells["Unit Price"].Value);
                        com.Parameters.AddWithValue("@Total_Price", row.Cells["Total Price"].Value);
                        com.Parameters.AddWithValue("@Amount", txtTotalToPay.Text);
                        com.Parameters.AddWithValue("@Cashier_Name", txtCashier.Text);
                        com.Transaction = transaction;
                        com.ExecuteNonQuery();
                    }
                }
                transaction.Commit();
                con.Close();
            }
            catch(Exception ex)
            {
                transaction.Rollback();
                con.Close();
                MessageBox.Show(ex.Message);
            }
Posted
Updated 13-Jul-22 0:33am
v2
Comments
Richard MacCutchan 13-Jul-22 6:03am    
I do not think you should use ExecuteNonQuery inside a Transaction.

1 solution

Try opening the connection before you begin the transaction.

SqlConnection.BeginTransaction Method (System.Data.SqlClient) | Microsoft Docs[^]
 
Share this answer
 
v2

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