0

I am trying to execute on a stored procedure in my database with the following code:

using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myCNN"].ConnectionString))
{
    using (var cmd = new SqlCommand("dbo.myStoredProcedure", connection))
    {
        cmd.Parameters.Add("@PatientId", SqlDbType.VarChar, 40).Value = number;
        cmd.Parameters.Add("@NickName", SqlDbType.VarChar, 20).Value = "stlukeshs";                            
        var reader = cmd.ExecuteReader();
        while(reader.Read())
        {
            //Do Stuff..
        }
        //closer reader
        reader.Close();
    }
}

However, on execution I am getting an error that says:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Procedure or function 'myStoredProcedure' expects parameter '@PatientId', which was not supplied.

Clearly you see I am adding the parameters, and my value for number is 100, not null. So I am a bit confused on why it is not getting my parameter arguments.

3
  • 1
    That's clearly not your real stored procedure name, so presumably other things may not quite be as in the real code... are you 100% sure this isn't a typo? Commented Feb 24, 2017 at 17:38
  • I'm surprised it would not complain about you not opening the connection. Commented Feb 24, 2017 at 17:41
  • Oh it did, I missed that line of code.. sorry! Commented Feb 24, 2017 at 17:54

1 Answer 1

1

You need to specify CommandType which is Text in default. You have to specify StoredProcedure as CommandType i.e.

cmd.CommandType = CommandType.StoredProcedure;

So, your complete code should be :

using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myCNN"].ConnectionString))
{
    using (var cmd = new SqlCommand("dbo.myStoredProcedure", connection))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@PatientId", SqlDbType.VarChar, 40).Value = number;
        cmd.Parameters.Add("@NickName", SqlDbType.VarChar, 20).Value = "stlukeshs";                            
        var reader = cmd.ExecuteReader();
        while(reader.Read())
        {
            //Do Stuff..
        }
        //closer reader
        reader.Close();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This fixed my problem exactly. I thought that would have been implicit, alas I am wrong. Thank you!

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.