Make A Date Null
Can someone help me fix my code I am trying to put null in sql server through a function that is excepting _dtSWO.
If I set _dtSWO = Nothing it returns #12:00:00 AM# which throws an exception. The field is nullable in sql server I just want it to return a blank
Dim _swoDate As String = udSWOReceivedDateDateEdit.Text
Dim _dtSWO As Date
开发者_如何学C If _swoDate <> "" Then
_dtSWO = Date.Parse(udSWOReceivedDateDateEdit.Text)
Else
'THIS DOESNT WORK
_dtSWO = Nothing
End If
You need to use a Nullable type. By default, Date does not except null (Nothing in VB.Net) as a value. Nullable types do accept null (Nothing).
Dim _dtSWO As Nullable(Of Date)
If String.IsNullOrEmpty(_swoDate) Then
_dtSWO = Date.Parse(udSWOReceivedDateDateEdit.Text)
Else
_dtSWO = Nothing
End If
Edited based on the comment.
Make it Nullable. Same kind of situation as your Nullable Integer question.
Dim _swoDate As String = udSWOReceivedDateDateEdit.Text
Dim _dtSWO As Date?
If _swoDate <> "" Then
_dtSWO = Date.Parse(udSWOReceivedDateDateEdit.Text)
Else
_dtSWO = Nothing
End If
精彩评论