NoviceToProgramming
NoviceToProgramming

Reputation: 103

Transaction scope c#

I have two insert statements which are inserting records in a database. I have used transaction scope so that in case any of the statement fails, none of the records get updated. In the second procedurem the insert is failing but it's inserting record in the database for the first statement. What i am missing in the code?

Connection String

<add name="Transac.Properties.Settings.TransacDbConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=Transac;Integrated Security=True" providerName="System.Data.SqlClient" />

private void button1_Click(object sender, EventArgs e) {
    var scopeOptions = new TransactionOptions();
    scopeOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
    scopeOptions.Timeout = TimeSpan.MaxValue;

    using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required,scopeOptions)) {
        try {

                cmd = new SqlCommand("SalesSave", con);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@BillNo", lblBillNo.Text));
                cmd.Parameters.Add(new SqlParameter("@CustName", cboCust_Name.Text.ToUpper()));
                cmd.Parameters.Add(new SqlParameter("@Mobile", txtMob.Text));
                cmd.ExecuteNonQuery();


                cmd = new SqlCommand("SubSave", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@SubBillNo", lblBillNo.Text));
                cmd.Parameters.Add(new SqlParameter("@ItemName", dataGridView1.Rows[i].Cells[0].Value.ToString().ToUpper()));
                cmd.Parameters.Add(new SqlParameter("@Qty", dataGridView1.Rows[i].Cells[1].Value.ToString()));
                //cmd.Parameters.Add(new SqlParameter("@Price", dataGridView1.Rows[i].Cells[2].Value.ToString()));
                cmd.ExecuteNonQuery();

            transactionScope.Complete();
            transactionScope.Dispose();
        }
        catch (Exception e1) {
            MessageBox.Show(e1.Message);
        }
    }
}

Stored Proc 1:

ALTER PROCEDURE [dbo].[SalesSave]
(  
    @SalesBillNo as varchar(50),
    @CustName as varchar(50),
    @Mobile as nvarchar(50)

)   
AS
    begin
        insert into Sales(

                         SalesBillno,
                         CustName,
                         Mobile

                         )

                values (

                         @SalesBillNo,
                         @CustName,
                         @Mobile

                         )
    end

Stored Proc 2

ALTER PROCEDURE [dbo].[SubSave]
@SubSalesBillNo as varchar(50),
@ItemName as nvarchar(50),
@Qty as money,
@Price as money

AS
    begin
         INSERT INTO SubSales 
         VALUES(
                @SubSalesBillNo,
                @ItemName,
                @Qty,
                @Price   
                )
    end

Upvotes: 0

Views: 608

Answers (1)

I believe you must open a database connection within the transaction scope and use that for this to work.

Upvotes: 1

Related Questions