Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have facing some problem to insert multiple table some time that insert only single table not insert into another table and not working rollback function what happen i can't understand please help me

I use this code(Database is online )

C#
using (SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["Connection"].ToString()))
{                       
    connection.Open();
    SqlCommand command = connection.CreateCommand();
    SqlTransaction transaction;                           
    transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
    command.Connection = connection;
    command.Transaction = transaction;
    try
    {
        // First Store Procedure
        command.CommandText = "PrintInvoiceNew";
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@BillNo", SqlDbType.VarChar, 50).Value = addstr;
        command.Parameters.Add("@CustomerID", SqlDbType.Int).Value = customerID;
        command.ExecuteNonQuery();
        command.Parameters.Clear();
                                
        //Second Store Procedure
        command.CommandText = "PrintInvoiceDetailsInsert";
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@BillNo", SqlDbType.VarChar, 50).Value = addstr;
        command.Parameters.Add("@ItemVariantID", SqlDbType.Int).Value = dgvInvoice[0, i].Value;
        command.Parameters.Add("@ItemCode", SqlDbType.NVarChar, 50).Value = dgvInvoice[1, i].Value;
        command.Parameters.Add("@ItemName", SqlDbType.NVarChar, 250).Value = dgvInvoice[2, i].Value;
        command.ExecuteNonQuery();
        command.Parameters.Clear(); 

        // Third store procedure 
        command.CommandText = "PrintStockUpdate";
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@Stock", SqlDbType.Int).Value = dgvInvoice[3, i].Value;
        command.Parameters.Add("@LocationID", SqlDbType.Int).Value = Convert.ToInt32(Variables.LocationID);
        command.Parameters.Add("@ItemVariantID", SqlDbType.Int).Value = dgvInvoice[0, i].Value;                                           
        command.ExecuteNonQuery();
        command.Parameters.Clear();

        ...........
        transaction.Commit();
    }
    catch (Exception exc)
    {
        try
        {
            transaction.Rollback();
        }
        catch (SqlException ex)
        {
            if (transaction.Connection != null)
            {
                MessageBox.Show("An exception of type " + ex.GetType() +
                                 " was encountered while attempting to roll back the transaction.");
            }
        }

        MessageBox.Show("An exception of type " + exc.GetType() +
                        " was encountered while inserting the data. Message :" + exc.Message);
        MessageBox.Show("Neither record was written to database.");
    }
}
Posted
Updated 29-Jul-13 2:46am
v2
Comments
Prasad Khandekar 29-Jul-13 8:51am    
Have You tried to look at your stored proc. May be they have rollback/commit statements inside. Also check whether all FK relations are satisfied with the data you are trying to insert.

Regards,
thams 29-Jul-13 8:55am    
Seems to be no problem with the code. Only left thing is database connection not closed. But definitely thats not the cause of your problem.

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