Click here to Skip to main content
15,908,020 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hiii all,

I made a function that return a count of table in (int) and use that returned int to be displayed in a textbox.

I made that code but it always returns (-1). Anyone can tell me where the error is?
C#
public Int64 bulidcount()
    {
        Int64 iQryValue;
        SqlConnection conn = new SqlConnection(connstr);
        SqlCommand cmd_save = new SqlCommand();
        cmd_save.Connection = conn;
        cmd_save.CommandType = CommandType.StoredProcedure;
        cmd_save.CommandText = "usp_test";
        try
        {
             conn.Open();
            iQryValue = cmd_save.ExecuteNonQuery();
            return iQryValue;
        }
        catch (Exception ex)
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
            return 1;
        }  
 
    }

C#
Int64 iResult = bulidcount();
 txt_MobileCount.Text = iResult.ToString();
Posted
Updated 23-Jan-11 18:12pm
v4
Comments
Sergey Alexandrovich Kryukov 24-Jan-11 0:12am    
Fixed typo in the title. Watch this: http://www.youtube.com/watch?v=u9_kahA_wQo
--SA

Disclaimer: this is not intended to answer a question.

Instead, this is a warning about a much more serious mistake.

Why the code returns 1 when exception is thrown? Does 1 indicate an error? Does it mean that when iQryValue == 1 it should be considers as an error?!

More importantly, this is a very bad and unsafe practice to block an exception from propagation as shown in the code sample (except pretty rare cases when there is no other options).
In the code, the line "return 1;" should be replaced with "throw;". It will re-throw the same exception.
 
Share this answer
 
v2
Comments
fjdiewornncalwe 23-Jan-11 22:36pm    
100% agreement from me. +5.
moon2011 26-Jan-11 5:35am    
i didn't mean that returning one refers to an exception , but it was reference for me only and i know that it shouldn't be like that.
thanks alot for your advice.
Sergey Alexandrovich Kryukov 26-Jan-11 12:46pm    
You're very welcome. No matter what you meant to do, do yourself a favor: always re-throw or throw, don't block propagation of exception. You also never need to return any error condition from any function: exceptions made this technique gravely obsolete.
Good luck.
--SA
Espen Harlinn 6-Feb-11 15:01pm    
Good advice, a 5
Try:

cmd_save.ExecuteScalar();


ExecuteNonQuery only returns the number of rows affected (after carrying out whatever your stored procedure does), ExecuteScalar will return the result of your stored procedure (whatever that might be).
 
Share this answer
 
v2
Comments
moon2011 23-Jan-11 3:34am    
Thanks digital man for your help
I guess, your logical error lies here:
cmd_save.ExecuteNonQuery(); & StoredProcedure called.

Adding to that, probably you must have enabled 'no count' in SP. Remove it.

ExecuteNonQuery: Executes insert/update/delete & returns number of rows affected. Details here[^].

Just to add on, a similar discussion here[^].
 
Share this answer
 
Comments
moon2011 23-Jan-11 3:33am    
Thanks Sandeep Mewara for your help
Hi Monamoona,

Change :
C++
iQryValue = cmd_save.ExecuteNonQuery();



with :
C++
iQryValue = cmd_save.ExecuteScalar();



I hope this help ,
Good luck :)
 
Share this answer
 
Comments
moon2011 23-Jan-11 3:32am    
Thanksss Michael Waguih for your help
Michael Waguih 23-Jan-11 3:37am    
You are welcome :)
It may be related to your stored procedure, which we can't see because you haven't given us the code.

But, it is worth noting that MSDN[^] on ExecuteNonQuery says:
Although the ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are populated with data.
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.


It may be worth your reading up on returning values from stored procedures. There is a good explanation here.[^]
 
Share this answer
 
Comments
moon2011 23-Jan-11 3:33am    
Thanks OriginalGriff for your help

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