Click here to Skip to main content
12,076,727 members (46,886 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C# ASP.NET SQL-Server , +
Hi

How to assign null value to date? By default my datetime is 1/1/0001, but I want to assign datetime to null. How can I assign null value to datetime?

I tried by this code but it is not working.
dt = Convert.ToDateTime(strposteddate);
strposteddate = dt.ToShortDateString();
if (strposteddate == "1/1/0001")
{
   strposteddate = null;
}
Posted 5-Sep-11 20:13pm
Edited 5-Sep-11 20:15pm
Prerak Patel141.5K
v2
Comments
suman161288 21-Jun-13 6:49am
   
Please can you tell me the DataType of dt and strposteddate variable to we can clarify it.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You define the datetime variable as nullable:
DateTime? dt = null;
  Permalink  
Comments
SAKryukov 6-Sep-11 1:19am
   
Correct, my 5. OP needs to learn basics of .NET types.
--SA
Mika Wendelius 6-Sep-11 1:27am
   
Thanks, I agree
Member 7932936 6-Sep-11 1:20am
   
i already define datetime? dt=null;
but it takes default value i.e 1/1/0001
Mika Wendelius 6-Sep-11 1:29am
   
If you define the datetime as nullable, it's default value is null, not any date. Especially when you have explicitely set the variable to null, the default doesn't even matter.

Sounds like you're either using a different variable or you set it somewhere else. Use the debugger and with a watch, see the changes that are taking place concerning this variable.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

DateTime is not nullable type. You can use Nullable<datetime> dt;
  Permalink  
v2
Comments
Member 7932936 6-Sep-11 1:21am
   
I already used like that

but it has set default datetime 1/1/0001
SAKryukov 6-Sep-11 1:23am
   
Don't mix up default time with null. Null should be used to carry semantic if "undetermined time, no time".
--SA
SAKryukov 6-Sep-11 1:22am
   
Correct, my 5. I would prefer the simpler-to-use variant shown by Mika though, please see.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Well, Here is the fundamental stuff of nullable DateTime. DateTime struct itself does not provide a null option. but we can make it a nullable type which allows you to assign the null to a datetime.

DateTime ? nullableDateTime = null ;
           if(nullableDateTime.HasValue)
           Console.WriteLine(nullableDateTime.Value.ToShortDateString());

As, my nullableDateTime is set to Null, above code block wont print anything.
This will only print a value, if the nullableDateTime has some value.

Come to the minimum date,

var defaultDateTimeValue = default(DateTime);
Here defaultDateTimeValu will be the 1/1/0001 12:00:00 AM}
But, for nullableType,

var defaultDateTimeValue = default(DateTime?);
the value of defaultDateTimeValue should be null.

I hope this will give you enough idea.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

we can't assign null value to datetime

because null value takes the system default date that is minimun date='1/1/0001'
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

Null Value pass in datetime field
cmd1.Parameters.AddWithValue("@ups_delivery_date", SqlDbType.DateTime).Value = System.DBNull.Value;
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

dt = Convert.ToDateTime(strposteddate);
            strposteddate = dt.ToShortDateString();
            if (strposteddate ==null)
            {
              //allow something
            } 
            else
               
              // allow something
  Permalink  
v2
Comments
Member 7932936 6-Sep-11 1:26am
   
if null

my datetime set to 1/1/0001 but hasn't takes null value
archanakumari 6-Sep-11 1:36am
   
i have not read the question Properly
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 9

You can solve this problem in two ways, either make it a nullable type, or use the System.DateTime.MinValue.

DateTime? nullDateTime = null;

Example with source....C# datetime to null

watson
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 10

We can't assign null value to datetime.
I've made some changes to your code so that it can correctly work.

try
{
dt = Convert.ToDateTime(strposteddate);
}
catch(FormatException)
{
Console.WriteLine("{0} is not in the proper format", strpsoteddate);
strposteddate = "";
}
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160212.1 | Last Updated 10 Jun 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100