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
?