Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6168449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:41:27+00:00 2026-05-23T22:41:27+00:00

I am writing stored procedure in following way.. CREATE PROCEDURE spTest @intCompId int, @varCompName

  • 0

I am writing stored procedure in following way..

CREATE PROCEDURE spTest
@intCompId      int,
@varCompName    varchar(50)
AS
BEGIN
BEGIN TRANSACTION
BEGIN TRY 
    INSERT INTO tblCompanyMaster(CompId,CompName)
    VALUES (@intCompId,@varCompName)
    IF(@@ERROR<>0)
        RAISERROR('Error',10,1)
        SELECT ERROR_MESSAGE()

    IF(@@ERROR=0)
    BEGIN
        COMMIT TRANSACTION
        SELECT 0;
    END
    ELSE
        ROLLBACK TRANSACTION
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION
END CATCH
END
GO

I this case if error occurs it will return error message. I am calling this sp from front end with sqlcommand property like oSqlcommand.executeNonQuery(); it will return no of rows affected and zero otherwise. Now what I want is if there is any error arise in stored procedure than the message returned by error_message() should be caught at front end as an exception and because I am writing each exception in text file. How can I catch this message at front end??
My coding style is as below….

try
{

   //Some code

}
catch(exception ex)
{

    //Write exception in text file.

}

I want to handle that message at ex.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:41:29+00:00Added an answer on May 23, 2026 at 10:41 pm

    Your stored procedure contains a fundamental flaw:

    If you execute any statement after examining the value of @@ERROR (or @@ROWCOUNT), it gets reset to zero.

    You should always cache in a local variable before continuing:

    DECLARE @errornum int
    SET @errornum = @@ERROR
    

    If you need rowcount as well, then you must perform in a single statement like so

    SELECT @errornum = @@ERROR, @rowcount = @@ROWCOUNT
    

    In addition, you should not need to mix TRY/CATCH and examining @@ERROR

    You use TRY/CATCH when you want to handle errors in the stored procedure, for example:

    BEGIN TRY
        -- ...
        -- Do Stuff
        -- ...
    END TRY
    BEGIN CATCH
        SELECT 
            ERROR_NUMBER() AS ErrorNumber
            ,ERROR_MESSAGE() AS ErrorMessage;
    END CATCH
    

    In your C# code, catch SqlException

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a stored procedure where i m using try catch block. Now
i am writing stored procedures in MySQL that return values; CREATE PROCEDURE getCustomerById (id
I am writing a stored procedure in MySQL. The following is the code I
Without writing a stored procedure loop, is it possible to insert into a table
I am writing a stored procedure with 3 parameters and my where clause changes
I am writing one stored procedure, i need to pass start and end date
I am writing a stored procedure that when completed will be used to scan
I am writing a stored procedure for displaying month and year. It is working,
I am writing a stored procedure in SQL Server 2008. Here I have a
I am using SQL Server. I'm writing a stored procedure that executes a series

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.