I have an sql query that uses transactions, and it uses return to indicate failure or success, like so:
BEGIN
DECLARE @intErrorCode INT;
DECLARE @emailAutoIncrement INT;
BEGIN TRAN
INSERT INTO Accounts ( blabla, blabla2 )
VALUES ( somevalue, somevalue2 )
Select @intErrorCode = @@ERROR
if (@intErrorCode <> 0) GOTO PROBLEM
COMMIT TRAN
RETURN 1
PROBLEM:
if (@intErrorCode <> 0) Begin
Rollback Tran
RETURN 0
END
END
From code, I used sqlCommand.ExecuteScalar() thinking that this will give me either 1 or 0, but this always returns false. Also, another problem I have is that when an exception happens in the sql and the program is meant to return 0, it actually returns the exception.
So the RETURN 0 is useless because it is always overridden by the exception?
To add some more information, the sql query is a stored procedure and it is called like so:
myCommand.CommandText = "createAccount";
myCommand.CommandType = CommandType.StoredProcedure;
//Add parameters here
using (myReader)
{
var test = myCommand.ExecuteScalar();
}
try
SELECTrather thanRETURNI also changed
to