G
Guest
I want to set a DateTime field to Null before passing it to the DB
//First I check to see if anything is in this datarow column, because
sometimes we have no data.
DateTime dt;
if ( datarow["date"].ToString().Length > 0)
{
//We assume the data is a date
dt = (DateTime)datarow["date"];
}
else
//We have no data in this column
{
dt = DBNull.Value; //this does not work
}
Then I set the param.
cmdUpdate.Parameters.Add("@invoicedate", SqlDbType.NVarChar, 40).Value =
dt.ToShortDateString();
If there is a date in 'datarow["date"]' everything works fine, but if there
is no data it throws an exception. So I set up the if statement to check for
something in that column, if there is something I assume it's a date and
assign it to dt. But if there no data I want to asign nothing to dt.
Thanks
Paul
//First I check to see if anything is in this datarow column, because
sometimes we have no data.
DateTime dt;
if ( datarow["date"].ToString().Length > 0)
{
//We assume the data is a date
dt = (DateTime)datarow["date"];
}
else
//We have no data in this column
{
dt = DBNull.Value; //this does not work
}
Then I set the param.
cmdUpdate.Parameters.Add("@invoicedate", SqlDbType.NVarChar, 40).Value =
dt.ToShortDateString();
If there is a date in 'datarow["date"]' everything works fine, but if there
is no data it throws an exception. So I set up the if statement to check for
something in that column, if there is something I assume it's a date and
assign it to dt. But if there no data I want to asign nothing to dt.
Thanks
Paul