.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
satyapriyanayak
Subhransu Sekhar Jena
abhays
SP
DotnetUser

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

how to assign null value to datetime

Posted By: rashmi     Posted Date: June 07, 2011    Points:5   Category :ASP.Net
how to assign null value to datetime

Responses
Author: Sunil Yadav       [Moderator]      
Posted Date: June 07, 2011     Points: 20   

Hi,

You can use Nullable types for assigning null value.

Nullable<DateTime> date1;
date1.Value = null;

OR

DateTime? date1 = null;


for more info http://msdn.microsoft.com/en-us/library/1t3y8s4s(v=vs.80).aspx

----------------------------
My Blog : http://www.sunilyadav.net

Twitter : http://www.twitter.com/yadavsunil
Author: Harsha             
Posted Date: June 07, 2011     Points: 20   

The DateTime struct itself does not provide a null option, but the DateTime? nullable type allows you to assign the null literal to the DateTime type.

E.g.
DateTime? value = null;
Console.WriteLine(value.Value);

-- Output --
0


For more info refer : http://www.dotnetperls.com/nullable-datetime


Author: rashmi             
Posted Date: June 07, 2011     Points: 20   

how to pass null values to datetime fields into database

Author: rashmi             
Posted Date: June 07, 2011     Points: 20   

hi thanks
it works but when i inserted null value into sqldatabase the datetime field get 1/1/1900 date

Author: rashmi             
Posted Date: June 07, 2011     Points: 20   

I'm using TextBox with AJAX CalendarExtendar... There are few other textboxes, dropdowns on the form. So if I leave the CaledarTextbox empty and hit insert button, record is created.

But if I open that form later, the Textbox is displaying "1/1/1900 12:00:00 AM". I am using Datareader to read data from DB.

Author: Sasi Prabhu             
Posted Date: June 07, 2011     Points: 20   

Use If Condition
and display the value

Refer This.

Download Attachment

Thanks & Regards,
Sasi Prabhu.
Author: shiv chandra             
Posted Date: June 07, 2011     Points: 20   

DateTime? dateExample1;
dateExample1 = null;


------
Shiva


Regards
Shiva
Author: Ravi Ranjan Kumar             
Posted Date: June 08, 2011     Points: 20   

You can set the DateTime to Nullable. By default DateTime is not nullable. You can make it nullable in a couple of ways. Using a question mark after the type DateTime? myTime or using the generic style Nullable. I have added a couple of links on msdn.
Like
You can use a nullable DateTime for this.
Nullable<DateTime> myDateTime;
//or the same thing written like this:
DateTime? myDateTime;


or have a go through to given links
http://msdn.microsoft.com/en-us/library/2cf62fcy(VS.80).aspx for using Using Nullable
http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx for using Nullable



Thanks & Regards
Ravi Ranjan Kumar
http://raviranjankr.wordpress.com
Author: cronynaval             
Posted Date: November 20, 2014     Points: 20   

By default DateTime is not nullable because it is a Value Type, using the nullable operator introduced in C# 2, you can achieve this.

Using a question mark (?) after the type or using the generic style Nullable.

Nullable < DateTime > nullDateTime;

or

DateTime? nullDateTime = null;

More info..http://net-informations.com/q/faq/nullable.html

Crony



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend