Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm having a bit of trouble trying to get the logic of why my Rollback wont work.

SQL
BEGIN TRAN

UPDATE CustomerBankInfo
SET [Money] = ([Money] - @orderValue)
WHERE CustomerBankInfo.CustomerID = (SELECT CustomerID
FROM Customer
WHERE FirstName = @custFName
AND LastName =@custLName) AND [Money] > @orderValue;

IF @@ERROR <> 0
BEGIN
ROLLBACK TRAN
RETURN 10
END


The thing is, even if orderValue is less than Money the rest of the procedure still runs. Am I totally missing the point? I feel like I am, can someone help please!


--- New Issue
SQL
BEGIN TRAN

SELECT @currentBalance = [Money] 
FROM CustomerBankInfo 
WHERE CustomerBankInfo.CustomerID = (SELECT CustomerID
FROM Customer
WHERE FirstName = @custFName
AND LastName =@custLName)
 
IF ((@currentBalance - @orderValue) < 0)
BEGIN
ROLLBACK TRAN 
RAISERROR ('Insuffienct funds available for customer',
               16, -- Severity.
               1 -- State.
               )
END

UPDATE CustomerBankInfo
SET [Money] = ([Money] - @orderValue)
WHERE CustomerBankInfo.CustomerID = (SELECT CustomerID
FROM Customer
WHERE FirstName = @custFName
AND LastName =@custLName)


Now it does nothing and just comes up with an unhanded exception. In my head the logic is right maybe im seeing past something though. Any help would be much appreciated.
Posted
Updated 15-Dec-11 7:01am
v4

1 solution

You're only rolling back your transaction if an error has occurred and the @@ERROR variable is set,

Your SQL statement will run OK, it won't generate an error (unless you had some sort of logic built in to table triggers or along those lines!)

What you are talking about is business logic, which is completely different to an SQL Error. You need to define your logic and raise your own error in these cases...

SQL
SELECT @currentBalance = [Money] FROM CustomerBankInfo 
WHERE FirstName = @custFName
AND LastName =@custLName

IF ((@currentBalance - @orderValue) < 0)
BEGIN
    ROLLBACK TRAN 
    RAISERROR ('Insuffienct funds available for customer'
               16, -- Severity.
               1 -- State.
               )
END


--Edit - additional info

If you calling this from c#, you need to handle the exception there. Flow is like this...

1) You call the SQL procedure from c#, passing in parameters
2) SQL procedure runs and encounters a business logic error (insufficent funds). Error is raised
3) This results in an SQLException, which your c# needs to handle
4) In your c# catch block, look at the exception object and extract error messages
5) Display error message to the user

e.g

C#
try
{
    using (SqlConnection conn = new SqlConnection("SomeConnectionString"))
    {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("SomeStoredProcedure", conn))
        {
            cmd.ExecuteNonQuery();
        }
    }
}
catch (SqlException ex)
{
    // Your exception details here. Use your debugger, look at the exception object. Determine how to display exception details to the client
}
 
Share this answer
 
v2
Comments
WurmInfinity 15-Dec-11 11:17am    
Ok thats cool, thanks for that! :D
WurmInfinity 15-Dec-11 11:29am    
I have another question if you'd be so kind to answer. All this is connected to a windows form in C#. Can I return the RAISERROR message to the program as a parameter?
Dylan Morley 15-Dec-11 11:36am    
Yep you can. If this is an SQL procedure, then you should be executing it using SqlConnection and SqlCommand objects.

If so, you can try\catch in your c# code and catch an SqlException object. You can then get at the various error information

Have a look here

http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/881efedb-330f-480c-a8f3-77ba298347df
WurmInfinity 15-Dec-11 11:45am    
Your too awesome! Gunna throw it coz I don't want it popping up haha :D Good call THANKS!
WurmInfinity 15-Dec-11 12:59pm    
I dunno if your still about, but I've been playing with it and it now won't work again. If you could take another look I'd be very grateful, I'll put it up in the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900