Zet
Zet

Reputation: 571

The parameterized query expects the parameter , which was not supplied

in my MVC application there is a method

public void insertAddress(AddressModel address)
{
        var connection = OpenConnection();
        var command = connection.CreateCommand();
        command.CommandText = "insert into Adres (AddressLine_1,AddressLine_2,Postcode,Town,DateMovedIn,Id) values (@AddressLine_1, @AddressLine_2, @Postcode, @Town,@DateMovedIn,@Id)";
        AddParameterString(command, "@AddressLine_1", address.AddressLine_1);
        AddParameterString(command, "@AddressLine_2", address.AddressLine_2);
        AddParameterString(command, "@Postcode", address.Postcode);
        AddParameterString(command, "@Town", address.Town);
        AddParameterString(command, "@DateMovedIn", address.DateMovedIn.ToString("yyyyMMdd"));
        AddParameterInt(command, "@Id", address.Id);

        command.ExecuteNonQuery();
}

AddressLine2 in model is not required. When user is not submitting AddressLine2 I get error:

The parameterized query '(@AddressLine_1 nvarchar(3),@AddressLine_2 nvarchar(4000),@Postc' expects the parameter '@AddressLine_2', which was not supplied.

How can I modify this method to work in both cases - user submitting AddressLine2 and user not submitting AddressLine2?

Upvotes: 2

Views: 10119

Answers (3)

You can simply use Null-coalescing operator (??):

AddParameterString(command, "@AddressLine_2", address.AddressLine_2 ?? Convert.DBNull);

Upvotes: 2

Jesús López
Jesús López

Reputation: 9221

This happens when a parameter value is null. To fix it you need to set parameter value to DbNull when the property is null.

void AddParameterString(SqlCommand command, string parameterName, string parameterValue)
{
     var param = command.Parameters.Add(parameterName, SqlDbType.NVarChar, 4000);
     param.Value = String.IsNullOrEmpty(parameterValue) ? (object) DbNull.Value : (object) parameterValue;

}

Upvotes: 2

Gene R
Gene R

Reputation: 3744

public void insertAddress(AddressModel address)
{
    var connection = OpenConnection();
    var command = connection.CreateCommand();
    command.CommandText = "insert into Adres (AddressLine_1,AddressLine_2,Postcode,Town,DateMovedIn,Id) values (@AddressLine_1, @AddressLine_2, @Postcode, @Town,@DateMovedIn,@Id)";
    command.Parameters.Add(new SqlParameter { ParameterName = "@AddressLine_1", Value = address.AddressLine_1 });
    if (address.AddressLine_2 == null)
    {
        command.Parameters.Add(new SqlParameter { ParameterName = "@AddressLine_2", Value = DBNull.Value });
    }
    else
    {
        command.Parameters.Add(new SqlParameter { ParameterName = "@AddressLine_2", Value = address.AddressLine_2 });
    }
    command.Parameters.Add(new SqlParameter { ParameterName = "@Postcode", Value = address.Postcode });
    command.Parameters.Add(new SqlParameter { ParameterName = "@Town", Value = address.Town });
    command.Parameters.Add(new SqlParameter { ParameterName = "@DateMovedIn", Value = address.DateMovedIn.ToString("yyyyMMdd") });
    command.Parameters.Add(new SqlParameter { ParameterName = "@Id", Value = address.Id });
    command.ExecuteNonQuery();
}

Upvotes: 2

Related Questions