Monday, May 26, 2008
Today I was happily coding a .Net 3.5/SQL Server 2005 web application when things came to a grinding halt with the error:

A severe error occurred on the current command.  The results, if any, should be discarded.

I googled the error (of course), but found a dozen different answers and many of them required installing service packs or hotfixes.  While I'm not opposed to that, I knew that it shouldn't be necessary.

I'm sure there are many causes for this error, but here was mine:

BEGIN TRANSACTION;

BEGIN TRY

    IF( @UserId < 1 ) BEGIN

        IF( (SELECT count(UserId) FROM [User] WHERE Email = @Email) > 0) BEGIN
            SET @Status = 1;
            RETURN;

        END

...

See the problem? I'm returning without committing or rolling back my transaction.  While this is a stupid mistake on my part, the error message is quite ambiguous, but thankfully the solution is simple.  Before returning, insert cleanup GOTO cleanup such as:

BEGIN TRANSACTION;

BEGIN TRY

    IF( @UserId < 1 ) BEGIN

        IF( (SELECT count(UserId) FROM [User] WHERE Email = @Email) > 0) BEGIN
            SET @Status = 1;
            GOTO NoSuccessReturn;

        END

...


NoSuccessReturn:
    IF( @@TRANCOUNT > 0 )BEGIN   
        ROLLBACK TRANSACTION;
    END

    RETURN;


Hope this helps someone!

--Tim Medora


posted on Monday, May 26, 2008 2:50:51 PM (US Mountain Standard Time, UTC-07:00)  #    Comments [0]