Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

I have one DOB field in my database and i have one text box in my form with out enter date am click on button, but i got the error like
"Nullable object must have a value." my code like
C#
if (txtdob.Text == "")
           {

                DateTime? value = null;
                record.DOB = value.Value;
           }
           else
           {
               record.DOB = Convert.ToDateTime(txtdob.Text);
           }


how to insert null value into date time field.
please tel me the ans..
Posted
Updated 29-Oct-12 1:32am
v2

Hi,

Please try this,

record.DOB = System.DBNull.Value;


May be help you....
 
Share this answer
 
You can use Nullable Types to achieve that.
XML
Nullable<DateTime> _myDateTime;


Check for nulls:
C#
if (object.myDateTime != null)

or
C#
if (object.myDateTime.HasValue)


Hope that helps.

Don't forget to mark answers that helped you! :)
 
Share this answer
 
Comments
Santhosh23 29-Oct-12 7:57am    
a tried your code i got the this type of error
Nullable object must have a value.
Bitla Phanindra 29-Oct-12 8:02am    
Does the database column allow nulls?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900