Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a empty string to DBNull

Status
Not open for further replies.

mal54

Programmer
Mar 11, 2002
13
AU
Hi All
I'm currently learning VB.Net and have come upon a problem that I'm sure you can help me with.

I have bound a field that holds a date from a MSAccess db DateTime Type. In this case VB.Net renders the date with the time stamp which I didn't need. So I formatted the date to a shortdate using the binding method. This works well. However I sometimes need to remove the date from the form, in this case the form produces an empty string and when you update the form it throws an error "String was not recognised as a valid DateTime" I realise that the program is trying to parse an empty string to a datetime type. But I'm not sure how to fix the problem. I need to be able to update a Null value back into the database.
Can you help
Regards Mal
 
When you want the users to have the ability to not set a date (leave the field blank), you'll need (in your GUI code) to set the DateTime variable to DateTime.minValue.

Later, when you go to write it to the database, you have to detect this, and write a DBNull instead (You can use an IIF() for this, or an If..Then..EndIf).

All this is because (as you've found out) it's not possible to assign an empty value to a DateTime variable. You could use a string to hold your value, but then you have a huge hassle with formatting (is it February 1st or January 2nd??). Using the minValue approach has the least pain.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top