> You just can't do that. A DateTime can't be null. It can't be
> DBNull.Value, either (which is something different).
they accept a constructor for custom initial values. Right?
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1c693e6aa69333398bcc5@msnews.microsoft.com...
> Paulb1us@newsgroup.nospam <Paulb1us@newsgroup.nospam> wrote:
> > I want to set a DateTime field to Null before passing it to the DB
>
> You just can't do that. A DateTime can't be null. It can't be
> DBNull.Value, either (which is something different).
>
> > //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();
>
> Yikes - is the field in the database really a string rather than a
> date-related type? That's nasty.
>
> > 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.
>
> You can't do that. What you can do is:
>
> object parameterValue;
>
> if (!datarow.IsNull("date"))
> {
> parameterValue = ((DateTime)datarow["date"]).ToShortDateString();
> }
> else
> {
> parameterValue = DBNull.Value;
> }
> cmdUpdate.Parameters.Add ("@invoicedate", SqlDbType.NVarChar,
> parameterValue);
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too