loading an empty value into a date object using vb.net

  • Thread starter Thread starter David Lozzi
  • Start date Start date
D

David Lozzi

Howdy,

I am trying to accomplish the following:

dim DateSend as date

if txtDateSend.text = "" then
DateSend = dbnull.value
else
DateSend = txtDateSend.text
end if

however, the script screams about "Value of type 'System.DBNull' cannot be
converted to 'Date'." How do I send a Null back? This variable, DateSend, is
then sent to a stored procedure and written back to the database. I read
other posts on google groups stating users switched the variables from date
to string, then did a cast within the proc. Is this really the best
solution? Come on, Microsoft must have a way of loading an empty value into
a date object.

thanks!!

David Lozzi
 
DateTime is a value type (Date maps to DateTime with no time set), so it
cannot be set to null or dbnull (only reference types can be set to null
reference and even them cannot be set to dbnull.value, except object type
itself).

You can have DateTime.MinValue to indicate that the value has not been set
or is empty, and before setting the value to the parameter, check the
DateTime being MinValue and based on that pass DBNull straight to the proc
(or use default values in procs so you'd need to apss only parameters having
a value).
 
Back
Top