How to convert empty string to Null of Datetime data type
Hi,
This is very annoying to me.
I have a datatable which has two fields of DateTime data type. Both of them are nullable. When I do the update to that datatable, in some cases, Null values are going be put through TextBox and DropDownList's selected Item. But the updated value in database is 01/01/1900 and 12:00 AM which is not the real null value, can anyone help me with this?
My update code:
Dim inputDateAsNewDate
Dim inputSessionAsNewDate
If Trim(txtDate.Text) <> ""Then
inputDate = Convert.ToDateTime(txtDate.Text)
Else
inputDate = System.Data.SqlTypes.SqlDateTime.Null -->01/01/1900
EndIf
If ddlSession.SelectedItem.Text <> ""Then
inputSession = Convert.ToDateTime(ddlSession.SelectedItem.Text)
Else
inputSession = System.Data.SqlTypes.SqlDateTime.Null -->12:00 AM
EndIf
UpdateDT(AgendaItemID, inputDate, inputSession)
Just out of interest, why aren't inputSession and input of type Date System.Data.SqlTypes.SqlDateTime?If you use the standard CLR DateTime structure, it will also run back to its default value as that's all it can hold.
Make the variables of the appropriate type and then convert the input into SqlDateTime, not the other way around, and your problem should go away.
Labels: ado, annoying, convert, datatable, datetime, empty, fields, net, null, nullable, string, type, update
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home