2
votes

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?

3
Can you please show the definition of AddParameterString method? Why not just use ParameterCollection instead? And don't store your DateTime values as a string for your DateMovedIn column. Change it to datetime2 type instead. - Soner Gönül
Pass in null when not supplied (assuming the underlying database has a column that supports null: which it should do if it is optional). - Richard
try address.AddressLine_2 ?? DBNull.Value, or something like this - Gene R
you may use ternary operator to solve this problem.. link - Smit Patel

3 Answers

2
votes
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();
}
1
votes

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;

}
0
votes

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

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