Answered by:
Nullable Objects must have value for DateTime

Question
-
User1208695299 posted
Hi friends..
@oned_gk @dotnetzoom
How to pass Null value to database? In my codebehind pageI have tried this code but its not working for me
Dim dateSample As System.Nullable(Of Date) = Date.Now
Dim Datestring As String = dateSample.Value.ToString("d")
dateSample = NothingEx: Classname.Update(Txtbox1.text, dateSample)
When i try to pass this datesample in update function getting error like this "Nullable Objects must have value"
Date data type is DateTime.. m using SQL server 2008
Tuesday, April 29, 2014 1:42 AM
Answers
-
User1401801381 posted
How to pass Null value to database? In my codebehind pageHi
when you want to insert a null value in a database:
- check that your datetime column allows null values
- use DBNull.Value to set the value: http://msdn.microsoft.com/en-us/library/system.dbnull.value(v=vs.110).aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 29, 2014 2:01 AM -
User-1716253493 posted
Try like this
cmd.Parameters("@date").Value = IIf(dateSample.HasValue, dateSample.Value, DBNull.Value)
or try
If dateSample.HasValue Then cmd.Parameters("@date").Value = dateSample.Value Else cmd.Parameters("@date").Value = DBNull.Value End If
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 29, 2014 3:57 AM
All replies
-
User1401801381 posted
How to pass Null value to database? In my codebehind pageHi
when you want to insert a null value in a database:
- check that your datetime column allows null values
- use DBNull.Value to set the value: http://msdn.microsoft.com/en-us/library/system.dbnull.value(v=vs.110).aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 29, 2014 2:01 AM -
User-1716253493 posted
Try like this
cmd.Parameters("@date").Value = IIf(dateSample.HasValue, dateSample.Value, DBNull.Value)
or try
If dateSample.HasValue Then cmd.Parameters("@date").Value = dateSample.Value Else cmd.Parameters("@date").Value = DBNull.Value End If
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 29, 2014 3:57 AM