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
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
链接地址: http://www.djcxy.com/p/42824.html
上一篇: Petpoco问题与可空SQl服务器smallint和int16在vb.net
下一篇: 使日期为空