Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a SQL stored procedure that returns the result of SQL. Executing the SP with SQL it works however I haven't clue to achieve the same in C# winform. Here is what I know to execute a SP but beyond that I need help. Here is my SP:

SQL
PROCEDURE [dbo].[spPeople_AddPerson] 
(  
      @FirstName NVARCHAR(50),
	  @LastName NVARCHAR(50) =''
)  
AS  
DECLARE @ResultValue int  
BEGIN TRAN  
-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
IF EXISTS  
    (  
          SELECT FirstName,LastName FROM People 
          WHERE FirstName = @FirstName AND LastName = @LastName 
	)  
     BEGIN  
         SET  @ResultValue = -5  
     END  
ELSE  
      BEGIN  
           INSERT INTO People  
               (  
                 FirstName,LastName   
               )  
           VALUES  
           (  
                 @FirstName,@LastName 
           )  
           set @ResultValue = @@ERROR  
     END  
IF @ResultValue <> 0  
     BEGIN  
            ROLLBACK TRAN  
      END  
ELSE  
      BEGIN  
            COMMIT TRAN  
      END  
RETURN @ResultValue


What I have tried:

Here is my C# winform code:

C#
using (SqlConnection sqlCon = new SqlConnection(connectionString))
           {
               sqlCon.Open();

               SqlCommand sqlCmd = new SqlCommand("spPeople_AddPerson", sqlCon);
               sqlCmd.CommandType = CommandType.StoredProcedure;
               sqlCmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text.Trim());
               sqlCmd.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim());

               sqlCmd.ExecuteNonQuery();
           }
Posted
Updated 25-May-21 11:08am
v2

1 solution

Instead of ExecuteNonQuery, use ExecuteScalar - it returns a single value to the caller:
C#
using (SqlConnection sqlCon = new SqlConnection(connectionString))
    {
    sqlCon.Open();
    
    using (SqlCommand sqlCmd = new SqlCommand("spPeople_AddPerson", sqlCon))
        {
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text.Trim());
        sqlCmd.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim());
        
        int result = (int) sqlCmd.ExecuteScalar();
        ...
        }
    }
 
Share this answer
 
v2
Comments
Member 13694735 25-May-21 17:07pm    
I get the following error message:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
OriginalGriff 25-May-21 17:30pm    
Where?
When?
What does the debugger show you?
[no name] 25-May-21 17:48pm    
Griff, I could be mistaken, but shouldn't the sqlCmd.ExecuteScalar() be cast as an int? The result from the stored proc is either 0 or a negative number.

int result = (int)sqlCmd.ExecuteScalar();
OriginalGriff 26-May-21 1:22am    
Yes, you are right - I missed that.
In my defense, it was late at night and I was hurrying ... :O

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