Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
How to call stored procedure with required number of parameters in bool method
I have created stored procedure with parameters like
SQL
CREATE PROCEDURE [dbo].[sp_UPerson]
(
@PersonID INT,
@PersonEmailID varchar(50),
@PhoneNumber varchar(50)=null,
@Source varchar(50)=null
)

in bool method
C#
public bool GetPerson(int PersonID,string PersonEmailID )
      {
          try
          {
              conn.Open();
              cmd = new SqlCommand();
              cmd.Connection = conn;
              cmd.CommandText = "sp_UPerson";
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.Parameters.AddWithValue("@PersonID", PersonID);
              cmd.Parameters.AddWithValue("@PersonEmailID ", PersonEmailID );
              cmd.ExecuteNonQuery()
          }
          catch
          {

          }
          finally
          {
              conn.Close();
          }
      }

it showing error does not get all path value.
Posted
Updated 13-Jun-12 1:47am
v2
Comments
RDBurmon 13-Jun-12 9:25am    
Thanks Everyone who replied to this thread , So Prasannala, I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.

Hi Prasannala,

You should change you statement as like below,
C#
public bool GetPerson(int PersonID,string PersonEmailID )
      {
          try
          {
              conn.Open();
              cmd = new SqlCommand();
              cmd.Connection = conn;
              cmd.CommandText = "sp_UPerson";
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.Parameters.AddWithValue("@PersonID", PersonID);
              cmd.Parameters.AddWithValue("@PersonEmailID ", PersonEmailID );
              cmd.ExecuteNonQuery();
              return true;           }
          catch
          {
return false;          }
          finally
          {
              conn.Close();
          }
      }
 
Share this answer
 
v2
Comments
Prasannala 14-Jun-12 3:20am    
In this executeNonQuery() gives number of rows effected by which query in stored procedure.
stored procedure have the many queries.which query effected by executenonQuery()
Hi,

cmd.ExecuteNonQuery() method returns integer which its affected by number of rows.
So you should be return true, if count is greater than 0 otherwise returns false.


it showing error does not get all path value
For avoid this above error, you should return false in catch block.
 
Share this answer
 
You can try

C#
conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "sp_UPerson";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PersonID", PersonID);
            cmd.Parameters.AddWithValue("@PersonEmailID ", PersonEmailID );
          if(  cmd.ExecuteNonQuery() > 0)
          {
             return true;
          } 
          else
            {
                 return false;
            }
 
Share this answer
 
Comments
Prasannala 14-Jun-12 3:17am    
In this executeNonQuery() gives number of rows effected by which query in stored procedure.
stored procedure have the many queries.which row effected by executenonQuery()
The answer to this question has already been posted, but no one has really addressed the "why" of it so in the interest of teaching a man to fish, I am going to add this answer.

The reason you are getting the error about not all paths returning a value is because not all paths return a value. Sorry, but it means exactly what it says. You have a method that says it will return a boolean value. That is a contract that you have agreed to honor, yet your code does not honor that contract. It doesn't return a value. Even if you put a return inside your try statement, that won't be enough because there might be a time when your code throws an error before they try statement finishes. In that case, the return statement would be skipped and that path would not have a return value.

You could put multiple return statements in (two, in this case) to cover all the code paths, but that gets messy in a larger application. Instead, what I like to do is create a variable called output that is the return type. In your case, this would mean that output would be a boolean. At the very top of the application, I initialize the output variable to have a value that conveys an error. In this case, that would mean that it would be false. Then, at the very end of my method, outside of any limiting code block like try/catch statements, I return the output value. This way, if something goes wrong, no matter where it happens, the error value will be returned. I then set the value to true if, and only if, the criteria has been met to make the method a success. Here is a demo, with your code, on how that would work:

C#
public bool GetPerson(int PersonID,string PersonEmailID )
{
    bool output = false;
    try
    {
        conn.Open();
        cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "sp_UPerson";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@PersonID", PersonID);
        cmd.Parameters.AddWithValue("@PersonEmailID ", PersonEmailID );
        int recordCount = cmd.ExecuteNonQuery()
        if (recordCount > 0)
        {
            output = true;
        }
    }
    catch
    {

    }
    finally
    {
        conn.Close();
    }

    return output;
}


Now we have one variable that holds the state of our method. We also only have one place where our code exits. Finally, we know that no matter what, our return value will be valid. We will never tell the calling method that we were successful when we were not.

The one final thing I want to cover is the boolean return value. I don't think that this is a good idea. If you are writing records to SQL, you should get back the number of records affected. This allows you to test situations where you don't want any rows affected. Right now, that would mean that your method would say you had an error but that isn't true. Unless you have a strong business case why you must have a boolean return value, I would recommend changing it to be an int. If you cannot do that, I would recommend taking out the if statement and returning true if the application didn't crash.
 
Share this answer
 
Comments
Prasannala 14-Jun-12 3:16am    
In this executeNonQuery() gives number of rows effected by which query in stored procedure.
stored procedure have the many queries.which row effected by executenonQuery()
Tim Corey 14-Jun-12 7:52am    
The number of rows affected is cummulative. If you have three queries, each of which deletes five rows, you will get a returned value of 15 (3x5).
Prasannala 18-Jun-12 8:48am    
Thank you.
Hi,


Your method is
public bool GetPerson(int PersonID,string PersonEmailID )

which shows that method returns bool value. in your code , function doesn't returns any thing. to remove this error ,
cmd.ExecuteNonQuery() method should returns integer which its affected by number of rows.

or simply add return 0 or 1 on successfull execution.

C#
public bool GetPerson(int PersonID,string PersonEmailID )
      {
          try
          {
              conn.Open();
              cmd = new SqlCommand();
              cmd.Connection = conn;
              cmd.CommandText = "sp_UPerson";
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.Parameters.AddWithValue("@PersonID", PersonID);
              cmd.Parameters.AddWithValue("@PersonEmailID ", PersonEmailID );
              cmd.ExecuteNonQuery(); 
              return 1;
          }
          catch
          {
 
          }
          finally
          {
              conn.Close();
          }
      }
 
Share this answer
 
Comments
Tim Corey 13-Jun-12 22:25pm    
Two things: first, this wouldn't solve his error because not all paths return a value (still). If the code fails in the try section, it will never get to the return line and thus nothing will be returned. Second, you should be returning a boolean value, not an int.
Stucture of Function should be like this

C#
Boolean ChkValidation()
  {
         try
         {

        }
        catch
        {

        }
        finally
        {
            conn.Close();
        }
         return true(or false);
  }
 
Share this answer
 
v2

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