How to Return a success or fail code from a Stored Procedure?

MikeMCSD
MikeMCSD used Ask the Experts™
on
I'm running a stored procedure from code and I want to know if it completed succesfully:

            string connectionString = ConfigurationManager.ConnectionStrings["NopSqlConnection"].ConnectionString;
            using (var conn = new SqlConnection(connectionString))  
            using (var command = new SqlCommand("Import_Update", conn) {CommandType = CommandType.StoredProcedure})
            {
                conn.Open(); command.ExecuteNonQuery();
                conn.Close();

               //  Return a success or fail code

            }


ALTER PROCEDURE  Import_Update
AS
BEGIN
      SET NOCOUNT ON;

                UPDATE  . . .
                ..........

--   Return a success or fail code
 
END



What is the best way to do this?  thanks


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Here is a great example with customized error messages:
http://www.4guysfromrolla.com/webtech/tips/t100901-1.shtml

Author

Commented:
thanks, but that's in VB and is not using the same syntax as I am.
Navneet.Net Full Stack Developer
Commented:
Hi!

Do command.ExecuteScaler();

and from your query Select 'true' or select 'False'

Thanks!
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Navneet.Net Full Stack Developer

Commented:
Like!

 string connectionString = ConfigurationManager.ConnectionStrings["NopSqlConnection"].ConnectionString;
            using (var conn = new SqlConnection(connectionString))   
            using (var command = new SqlCommand("Import_Update", conn) {CommandType = CommandType.StoredProcedure})
            {
                conn.Open(); 
                returnValue = cmd.ExecuteScalar();

                conn.Close();

               //  Return a success or fail code

            }

Open in new window

Make sure your Stored proceudre return vale
liek on Success - Select 'true'
on Failuer   - Select 'false'

Thanks!
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
A proc will return an int value.  There must be a way to get that value in your code.

If that value is 0, all was OK.

For MS errors, If 1 thru 10, a minor error occurred but it still worked.

For 11+, there was an error(s) in the proc.

Author

Commented:
thanks all

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial