Reputation: 15049
I have the following stored procedure:
CREATE PROCEDURE [dbo].[master_accounting_invoice_change]
(
@accinvoiceuid uniqueidentifier,
@invoicenumber nvarchar(50),
@businessname nvarchar(150),
@taxid nvarchar(20),
@total money,
@subtotal money,
@taxamount money,
@discountamount money,
@invoicedate datetime,
@createddate datetime,
@newfolio int OUTPUT
)
AS
IF NOT EXISTS (SELECT accinvoiceuid FROM dbo.accounting_invoice WHERE accinvoiceuid = @accinvoiceuid )
BEGIN
/* GET NEXT FOLIO FOR INVOICE */
SELECT @newfolio = ISNULL(MAX(foliocurrent),0) + 1
FROM dbo.accounting_sender_folios
WHERE accsenderuid = @accsenderuid
AND isactive = 1;
exec master_accounting_invoice_insert
@accinvoiceuid,
@invoicenumber,
@businessname,
@taxid,
@total,
@subtotal,
@taxamount,
@discountamount,
@comissionamount,
@invoicedate,
@createddate
/* UPDATE NEXT FOLIO FOR INVOICE */
UPDATE dbo.accounting_sender_folios
SET foliocurrent = @newfolio
WHERE accsenderuid = @accsenderuid
AND isactive = 1;
END
ELSE
BEGIN
SET @newfolio = @folio;
exec master_accounting_invoice_update
@accinvoiceuid,
@invoicenumber,
@businessname,
@taxid,
@total,
@subtotal,
@taxamount,
@discountamount,
@comissionamount,
@invoicedate,
@createddate
END
Now, in my C# application I call the stored procedure in order to save changes, but the issue is that the foliocurrent is not being rollback when a exception occurs, and then the incremental variable is updated and saved.
Everything is rolled back except the:
/* UPDATE NEXT FOLIO FOR INVOICE */
UPDATE dbo.accounting_sender_folios
SET foliocurrent = @newfolio
WHERE accsenderuid = @accsenderuid
AND isactive = 1;
This is the code in the C# application. Is working, the rollback transaction is working but it doesn't rollback the incremental folio.
DbConnection conn = db.CreateConnection();
conn.Open();
DbTransaction trans = conn.BeginTransaction();
try{
using (DbCommand cmd1 = db.GetStoredProcCommand("master_accounting_invoice_change"))
{
db.AddInParameter(cmd1, "accinvoiceuid", DbType.Guid, dr["accinvoiceuid"]);
.....
.....
.....
db.ExecuteNonQuery(cmd1);
newFolio = Convert.ToInt32(db.GetParameterValue(cmd1, "newfolio"));
}
}catch(Exception ex){
// roll back transation
trans.Rollback();
}
Any clue on how to solve this or why is happening?
Appreciate any help in advance.
Alejandro
Upvotes: 0
Views: 1268
Reputation: 77657
Well, you may have it somewhere in the code, but you need to make sure that you associate the command with the transaction. You also need to make sure that the command is associated to the same connection that the transaction is. I'm not sure what your db.GetStoredProcCommand is doing.
db.Connection = conn;
db.Transaction = trans;
Upvotes: 3