I have this stored procedure called from my context, and it's not working. Then I have other stored procedures that are working... it's not sending any, error.
It's just not making the changes.
var id = facturavm.Id;
if (facturavm.EstadoAnterior == 0)
{
var fecha = new DateTime(Convert.ToInt32(facturavm.Fecha[6].ToString() + facturavm.Fecha[7].ToString() + facturavm.Fecha[8].ToString() + facturavm.Fecha[9].ToString()), Convert.ToInt32(facturavm.Fecha[3].ToString() + facturavm.Fecha[4].ToString()), Convert.ToInt32(facturavm.Fecha[0].ToString() + facturavm.Fecha[1].ToString()));
var x = DBManager.Context.ModificarFactura(facturavm.IdCliente, facturavm.IdProveedor, fecha, facturavm.MonedaDescripcion, facturavm.MonedaCambio, facturavm.Estado, facturavm.Total, id);
DBManager.Context.SubmitChanges();
DBManager.Context.ModificarFactura_Eliminar(id);
DBManager.Context.SubmitChanges();
...
And my stored procedures are:
(1) The one isn't working:
CREATE PROCEDURE [dbo].[ModificarFactura]
@idCleinte int,
@idProveedor int,
@fecha date,
@monedaDescripcion varchar(50),
@monedaCambio float,
@estado int,
@total float,
@id int
AS
BEGIN TRY
BEGIN TRAN
UPDATE Factura
SET IdCliente = @idCleinte,
IdProveedor = @idProveedor,
Fecha = @fecha,
MonedaDescripcion = @monedaDescripcion,
MonedaCambio = @monedaCambio,
Estado = @estado,
Total = @total
WHERE Id = @id
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
RETURN 0
(2) And the one that works:
CREATE PROCEDURE [dbo].[ModificarFactura_Eliminar]
@idFactura int
AS
BEGIN TRY
BEGIN TRAN
DELETE FROM DetalleFactura
WHERE IdFactura = @idFactura;
DELETE FROM ImpuestoProyectoFactura
WHERE IdFactura = @idFactura;
DELETE FROM ProyectoFactura
WHERE IdFactura = @idFactura;
DELETE FROM Impuesto
WHERE IdFactura = @idFactura;
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
RETURN 0
Thank you everybody!
You can't nilly-willy swallow all errors and ignore them. And is not valid to always ROLLBACK in catch blocks, w/o first consulting XACT_STATE()
. Here is a correct stored procedure error handling pattern:
create procedure [usp_my_procedure_name]
as
begin
set nocount on;
declare @trancount int;
set @trancount = @@trancount;
begin try
if @trancount = 0
begin transaction
else
save transaction usp_my_procedure_name;
-- Do the actual work here
lbexit:
if @trancount = 0
commit;
end try
begin catch
declare @error int, @message varchar(4000), @xstate int;
select @error = ERROR_NUMBER(), @message = ERROR_MESSAGE(), @xstate = XACT_STATE();
if @xstate = -1
rollback;
if @xstate = 1 and @trancount = 0
rollback
if @xstate = 1 and @trancount > 0
rollback transaction usp_my_procedure_name;
raiserror ('usp_my_procedure_name: %d: %s', 16, 1, @error, @message) ;
end catch
end
Try This
CREATE PROCEDURE [dbo].[ModificarFactura]
@idCleinte int,
@idProveedor int,
@fecha date,
@monedaDescripcion varchar(50),
@monedaCambio float,
@estado int,
@total float,
@id int,
@sts int output, --RETURN STATUS COMMIT OR NOT FROM SQL
@error nvarchar(400) output --RETURN ERROR MESSAGE SQL
AS
BEGIN
SET NOCOUNT ON;
SET @sts = 1 ;
SET @error = '';
BEGIN TRY
BEGIN TRANSACTION
UPDATE Factura
SET IdCliente = @idCleinte,
IdProveedor = @idProveedor,
Fecha = @fecha,
MonedaDescripcion = @monedaDescripcion,
MonedaCambio = @monedaCambio,
Estado = @estado,
Total = @total
WHERE Id = @id
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF @@ERROR > 0
BEGIN
SET @sts = 0 ;
SET @error = ERROR_MESSAGE();
ROLLBACK TRANSACTION
END
END CATCH
END
GO
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.