I have a table in which there are some datetime type columns. I use a stored procedure to insert values into the table. In the stored procedure I have variables that accept null
Try using:
if (string.IsNullOrWhiteSpace(InsertDate.Text))
{
cmd1.Parameters["@InsertDate"].Value = getDate;
}
else
{
cmd1.Parameters["@InsertDate"].Value = InsertDate.Text;
}
Try something like this, using Add rather than AddWithValue..
DB.Parameters.Add("@date", SqlDbType.DateTime).Value = DBNull.Value;
Try this logic if DBNull.Value is not worked .. use DateTime? null
Ensure that ur database field should allow null
dtexpiry!=null? dtexpiry :(DateTime?) null
if (//some condition)
{
cmd.Parameters.AddWithValue("@dateofBirth", txtdob.text);
}
else
{
cmd.Parameters.AddWithValue("@dateofBirth", DBNull.Value);
}
In the stored procedure do something like this:
insert into table(date)
values(case when @InsertDate ='' then
null
else
@insertDate
end)