4

My application allows the user to add a new player to the database. Before adding a new player, I'd like for a stored procedure to check whether or not a player with the first name and last name exists. I want the stored procedure to return a bit value, 0 or 1. My C# method will then return this value and the program can decide whether or not to proceed with the creation.

Note that I've cut some of the general validation out of the code below, i.e. if the fields are empty, or the balance TextBox is invalid etc..

I'm also aware that I may be using the wrong datatype when handling the returned value. i.e. int instead of bool.

When I run this, I get an error saying that my SP requires an input parameter @ReturnedValue.

Cheers

 Player newPlayer = new Player();
 newPlayer.PlayerID = Guid.NewGuid();
 newPlayer.FirstName = TextBoxFirstName.Text;
 newPlayer.LastName = TextBoxLastName.Text;
 newPlayer.Balance = Convert.ToDouble(TextBoxInitialCredit.Text);

var exists = newPlayer.CheckExists();

if (exists == 1)
{
     newPlayer.AddPlayer();
}

and here's the method:

 public int CheckExists()
 {
     SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Badminton"].ConnectionString);

     myConnection.Open();

     SqlCommand SqlCmd = new SqlCommand("CheckPlayerExists", myConnection);
     SqlCmd.CommandType = CommandType.StoredProcedure;

     SqlCmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50).Value = FirstName;
     SqlCmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 50).Value = LastName;
     SqlCmd.Parameters.Add("@ReturnValue", SqlDbType.Int, 2).Direction = ParameterDirection.Output;

     SqlDataAdapter myDataAdapter = new SqlDataAdapter(SqlCmd);
     DataSet myDataSet = new DataSet();
     myDataAdapter.Fill(myDataSet);

     int exists = Convert.ToInt32(SqlCmd.Parameters["@ReturnValue"].Value);

     myConnection.Close();

     return exists;
 }

and now my stored procedure:

CREATE PROCEDURE dbo.CheckPlayerExists
   @firstName nvarchar(50),
   @lastName nvarchar(50),
   @ReturnValue bit output
AS 
BEGIN
IF EXISTS (SELECT * FROM Players WHERE FirstName = @firstName AND LastName = @lastName)
        SET @ReturnValue = 1
ELSE
        SET @ReturnValue = 0

RETURN @ReturnValue
END
3
  • you should use FirstName = @firstName OR LastName = @lastName Commented Feb 27, 2014 at 15:45
  • A DataTable would be simpler than a DataSet. Commented Feb 27, 2014 at 15:46
  • 1
    You don't need a table, set or adapter as there are no rows in the output, ExecuteNonQuery would be simplest, see stackoverflow.com/questions/5630002/… Commented Feb 27, 2014 at 15:48

3 Answers 3

3

First, there's some inconsistency in your posted information. Is the parameter @ReturnValue or @ReturnedValue? That alone could be the problem.

Second, either change your stored procedures declaration of the @ReturnedValue parameter to

@ReturnValue bit = 0 output

or change the C# code that adds the output parameter:

SqlParameter p = new SqlParameter();
p.ParameterName = "@ReturnedValue";
p.SqlDbType = SqlDbType.Int;
p.Value = 0;
p.Direction = ParameterDirection.InputOutput;
SqlCmd.Parameters.Add(p);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks RBarry. I had only changed it from atReturnValue to atReturnedValue following an earlier answer. My original code was fine. The problem was your second suggestion. I needed to add '= - output' to the parameter declaration within the SP. Thanks
0

Problem : You are assigning the ParameterDirection Enum value as Output.

Solution : Change your ParameterDirection Enum value from Output to ReturnValue

ReturnValue: ParameterDirection.ReturnValue

The parameter represents a return value from an operation such as a stored procedure, built-in function, or user-defined function.

Replace This:

SqlCmd.Parameters.Add("@ReturnValue", SqlDbType.Int, 2).Direction
   = ParameterDirection.Output;

With This:

SqlCmd.Parameters.Add("@ReturnValue", SqlDbType.Int, 2).Direction
   = ParameterDirection.ReturnValue;

2 Comments

Thanks, i've changed that as well as the SP to @ReturnedValue, however I receive this error: Procedure or function 'CheckPlayerExists' expects parameter '@ReturnedValue', which was not supplied
@ManxJason: did you change the direction parameter?
0

I think I see your issues. In the line:

SqlCmd.Parameters.Add("@ReturnValue", SqlDbType.Int, 2).Direction = ParameterDirection.Output;

change name and direction type to:

SqlCmd.Parameters.Add("@ReturnedValue", SqlDbType.Int, 2).Direction = ParameterDirection.ReturnValue;

2 Comments

Thanks, i've changed that as well as the SP to @ReturnedValue, however I receive this error: Procedure or function 'CheckPlayerExists' expects parameter '@ReturnedValue', which was not supplied.
Did you change the ParameterDirection from .Output to .ReturnValue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.