7

I am populating tables using a stored procedure. The table allows a 'null' for the middle initial of the name, but I'm getting the following error message:

Procedure or function 'uspInsertPersonalAccountApplication' expects parameter '@MiddleInitial', which was not supplied.

Thanks in advance!

   public void ProcessForm(string[] InputData)
    {
        string ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["AssociatedBankConnectionString"].ToString();

        SqlConnection conn = new SqlConnection(ConnString);
        SqlCommand cmd = new SqlCommand("uspInsertPersonalAccountApplication", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@AccountType", "Savings");
        cmd.Parameters.AddWithValue("@AccountSubType", "Savings");
        cmd.Parameters.AddWithValue("@ExistingCustomer","No");
        conn.Open();
        cmd.ExecuteNonQuery();

        conn.Close();
    }

7 Answers 7

5

You can add to project and use following extension method:

public static SqlParameter AddWithValueSafe(this SqlParameterCollection parameters, string parameterName, object value)
{
    return parameters.AddWithValue(parameterName, value ?? DBNull.Value);
}
Sign up to request clarification or add additional context in comments.

Comments

4

There are two options here:

Modify you stored procedure and make @MiddleInitial param optional (which is currently not optional that's why error is thrown)

@MiddleInitial nvarchar(10) = NULL

Or add following line to your code:

cmd.Parameters.AddWithValue("@MiddleInitial", null);

4 Comments

I'm passing in 53 paramaters, many of which can be null. How can I dynamically set the DBNull.Value without wrapping each assignment in an IF statement?
If there is possible to modify stored procedure, then modify paramateres that can be null, and make them optional in stored procedure declaration. That is most flexible solution. If this is not possible you can use SqlCommandBuilder.DeriveParameters(command); and set DBNull.Value for each of them in loop. And then assign the ones you want.
See comments to stackoverflow.com/a/9518215/8479: use cmd.ParametersAddWithValue("@MiddleInitial", (object)yourVal ?? DBNull.Value); or add an extension method like AddParamWithValueOrNull()
I tried the AddWithValue("@param", null); which did not work. setting the defaults in the sproc (i.e. @param datetime = null) fixed it.
3

Try to pass in DBNull.Value instead of null.

6 Comments

I'm passing in 53 paramaters, many of which can be null. How can I dynamically set the DBNull.Value without wrapping each assignment in an IF statement?
Add ?? DBNull.Value after the value, or create a function like AddValueWithNonNullParameter that does it for you
Like this? Intellisense is giving me an error and Intellisense doesn't recognize AddValueWithNullParameter ------- cmd.Parameters.AddWithValue("@MiddleInitial", InputData[11] ?? DBNull.Value);
Yeah ?? example should work. If the value before ?? is null, it returns the value after ?? instead. The function AddValueWithNonNullParameter does not exist, you'd have to implement it yourself :)
Try ((object) InputData[11]) ?? DBNull.Value ?
|
1

You need to declare on everything - even if it's null.

Use DBNull.Value for MiddleInitial.

cmd.Parameters.AddWithValue("@MiddleInitial",DBNull.Value);

Comments

1

I created an extension method to battle this problem. Marcin's suggestion is also worth considering if you can update the stored procedure.

cmd.Parameters.AddString("@MyParamName", myValue);


public static class SQLExtension
{
    public static void AddString(this SqlParameterCollection collection, string parameterName, string value)
    {
        collection.AddWithValue(parameterName, ((object)value) ?? DBNull.Value);
    }
}

Comments

0

add the parameter for MiddleInitial also with Null value

public void ProcessForm(string[] InputData)
    {
        string ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["AssociatedBankConnectionString"].ToString();

        SqlConnection conn = new SqlConnection(ConnString);
        SqlCommand cmd = new SqlCommand("uspInsertPersonalAccountApplication", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@AccountType", "Savings");
        cmd.Parameters.AddWithValue("@AccountSubType", "Savings");
        cmd.Parameters.AddWithValue("@ExistingCustomer","No");
        cmd.Parameters.AddWithValue("@MiddleInitial",DBNull.Value);
        conn.Open();
        cmd.ExecuteNonQuery();

        conn.Close();
    }

Comments

0

hey you have to set with store procedure

@MiddleInitial varhcar(8) = null

1 Comment

+1 This would actually work since the parameter becomes optional

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.