I have a simple GridView bound to an objectdatasource. Everything works fine, except when try to do an update I get the following error:
Exception has been thrown by the target of an invocation.
I know from this post that this error might be caused by my stored procedure not returning a value, but my SP returns the Id of the updated row...
I have a trigger on the table I'm trying to update. If disable it, then the update works fine, so I guess the error is related to the fact that I have this trigger on this table. If I try to execute directly my update SP from SQL, with the trigger enabled, it works just fine.
Any ideas? Thanks!
Here's the code of the trigger, it updates some fields based on other fields new values... simple enough
ALTER TRIGGER [dbo].[ChallengeRideInsertUpdate]
ON [dbo].[DNN4_ESORV_ChallengeRide]
AFTER INSERT,UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Id int
IF (SELECT COUNT(*) FROM INSERTED) > 0
SELECT @Id=Id FROM INSERTED
ELSE
SELECT @Id=Id FROM UPDATED
-- CO2
UPDATE dbo.DNN4_ESORV_ChallengeRide
SET CO2 = (
SELECT (UTM.AvgCO2PerKm - CTM.AvgCO2PerKm) * CR.Distance
FROM dbo.DNN4_ESORV_ChallengeRide CR
INNER JOIN dbo.DNN4_ESORV_ChallengeTransportationMode AS UTM
ON CR.UsualTM = UTM.Id
INNER JOIN dbo.DNN4_ESORV_ChallengeTransportationMode AS CTM
ON CR.ChallengeTM = CTM.Id
WHERE CR.Id = @Id
)
WHERE Id = @Id
-- MONEY
UPDATE dbo.DNN4_ESORV_ChallengeRide
SET Money = (
SELECT (UTM.AvgCostPerKm - CTM.AvgCostPerKm) * CR.Distance
FROM dbo.DNN4_ESORV_ChallengeRide CR
INNER JOIN dbo.DNN4_ESORV_ChallengeTransportationMode AS UTM
ON CR.UsualTM = UTM.Id
INNER JOIN dbo.DNN4_ESORV_ChallengeTransportationMode AS CTM
ON CR.ChallengeTM = CTM.Id
WHERE CR.Id = @Id
)
WHERE Id = @Id
-- CALORIES
UPDATE dbo.DNN4_ESORV_ChallengeRide
SET Calories = (
SELECT (CTM.AvgCaloriesPerKm - UTM.AvgCaloriesPerKm) * CR.Distance
FROM dbo.DNN4_ESORV_ChallengeRide CR
INNER JOIN dbo.DNN4_ESORV_ChallengeTransportationMode AS UTM
ON CR.UsualTM = UTM.Id
INNER JOIN dbo.DNN4_ESORV_ChallengeTransportationMode AS CTM
ON CR.ChallengeTM = CTM.Id
WHERE CR.Id = @Id
)
WHERE Id = @Id
END