I have facing some problem to insert multiple table some time that insert only single table not insert into another table and rollback is not working what happen i can't understand please help me
I use this code(Database is online )
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.");
}
}


Iftikar HussainRank #423 · 5.9K reputationPosted Jul 29, 2013, 4:44 AM EDT
It is good have one SP instead of two SP, since both are related each other. Create one SP and pass all field value. In that SP use TRANSACTION with TRY-CATCH like this
Regards,
Iftikar
Posted Jul 29, 2013, 5:13 AM EDT
Please follow Iftikar Hussain comment.
krishan kumar singhRank #4,983 · 316 reputationPosted Jul 29, 2013, 9:52 PM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 29, 2013, 9:56 PM EDT
Regards,
Iftikar
krishan kumar singhRank #4,983 · 316 reputationPosted Jul 29, 2013, 10:23 PM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 29, 2013, 10:32 PM EDT
Regards,
Iftikar