How to assign null value to DateTime object?

How to assign null value to DateTime object?
0
rip33174
11/3/2003 9:17:27 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

27 Replies
1832 Views

Similar Articles

[PageSpeed] 39
Get it on Google Play
Get it on Apple App Store

Are you referring to DateTime object's returned from a database?

Why can't you just set DateTime dateTemp = null?
0
throwexception
11/4/2003 12:11:14 AM
Yes, I need to assign a default value to a DateTime object

DateTime dateTemp = null ;
doesnt work. I also tried System.dbNull that doesnt work either.
Please help
0
rip33174
11/4/2003 3:18:06 AM
Try

DateTime dateTemp = nothing ;
although when you declare a DateTime object its value is nothing anyway. You can test the value with
if dateTemp == nothing ...
Bill.
0
staplefordbill
11/5/2003 12:32:08 AM

Try initializing the DateTime object to DateTime.MinValue.
Then check it against this value instead of null when you are about to use it.
DateTime.MinValue = 00:00:00.0000000 January 01, 0001
0
throwexception
11/5/2003 6:54:06 PM
r u getting eoor while assigining null or is there some default value when u acess it?
0
hits
11/5/2003 8:31:28 PM
Hi,

When I assign DateTime variable to null I get following error:
Cannot convert null to 'System.DateTime' because it is a value type
If I dont assign null to the variable, I can compile but later get an error that object refernce not found ... :-(
0
rip33174
11/12/2003 7:49:00 PM
In case anyone's still having problems with this, I read somewhere on the web that you can use DBNull.Value.  Very useful for SQL parameters where you have to pass a value.  I tried it in one of my pages and it worked fine.


Bill.
0
staplefordbill
12/4/2003 6:12:22 PM
DateTime is a value type. You can't put null in a value type.

If what you want to do is send a null value in a a database, send DbNull.Value instead of a DateTime object to the Parameter object.
Bertrand
----
This posting is provided "AS IS" with no warranties, and confers no rights.
0
bleroy
12/5/2003 1:14:01 AM

Use System.Data.SqlTypes.SqlDateTime.MinValue.Value to assign a min value and in SP check for min datetime value and assign null.

C# Code

myDateTime = System.Data.SqlTypes.SqlDateTime.MinValue.Value;

Sample SP

create procedure p1
@MYDATETIME AS DATETIME = NULL
As
set @MYDATETIME = NULLIF( @MYDATETIME, '1753-01-01 00:00:00.000')
select @MYDATETIME

- Mohammed Ghouse Barq
http://programmersvision.googlepages.com/index.htm


-Mohammed Ghouse Barq
www.geocities.com/BarqKadapavi
0
ghousebarq
12/11/2008 11:07:40 AM

bleroy:
DateTime is a value type. You can't put null in a value type.
If what you want to do is send a null value in a a database, send DbNull.Value instead of a DateTime object to the Parameter object.
 

I agree with Bertrand. You can only assign null (C#) or Nothing (VB) to an object type.

Try something like this: 

cmd.Parameters["@DateResigned"].Value = (dtpDateResigned.Checked == true) ? dtpDateResigned.Value : DBNull.Value;
dtpDateResigned is an instance of DateTimePicker control, and cmd is an instance of SqlCommand class.

I hope it helps. Happy programming!


Regards,
Van


Dont forget to Mark as Answer if it helps you.
0
van
12/11/2008 2:04:16 PM

Hi,

Try this:

YourDate = Convert.ToDateTime(null);

 

 

 

0
monika
12/11/2008 2:07:35 PM

Do a Search for nullable Types:

 

DateTime? myNullableDate;

if(myNullableDate.HasValue)

{

 DoSomething with myNullableDate;

}

0
shazelton
12/11/2008 2:21:07 PM

Yes, you can assign null value to DateTime object.  

private DateTime? yourDateTime; //This way, you can assign null to the DateTime object.

How to check your DB value with this way?

yourDateTime = row.IsNull("YuorColumn") ? null : (DateTime?)Convert.ToDateTime(row["yourColumn"]);

Hope it helps.

 


"Object reference not set to an instance of an object"
0
che3358
12/11/2008 2:23:57 PM

 try this,

DateTime dt = Convert.ToDateTime(null);

 then dt ll be initialized by - "1/1/0001 12:00:00 AM"

 

 

0
anandkbs
12/11/2008 2:47:11 PM

van.net:

bleroy:
DateTime is a value type. You can't put null in a value type.
If what you want to do is send a null value in a a database, send DbNull.Value instead of a DateTime object to the Parameter object.
 

I agree with Bertrand. You can only assign null (C#) or Nothing (VB) to an object type.

Try something like this: 

cmd.Parameters["@DateResigned"].Value = (dtpDateResigned.Checked == true) ? dtpDateResigned.Value : DBNull.Value;
dtpDateResigned is an instance of DateTimePicker control, and cmd is an instance of SqlCommand class.

I hope it helps. Happy programming!

 

I want to correct my mistake. In C#, you can declare a nullable value type by appending question mark (?) to its type name.

 

Wrong: 

DateTime d = null; // without question mark, error will occur.
 

Right: 

DateTime? d = null; // no error. notice the question mark.
 

I hope it helps. Happy programming!


Regards,
Van


Dont forget to Mark as Answer if it helps you.
0
van
12/11/2008 3:09:18 PM

That is another advantage of C# from VB.NET.


In VB.NET: 

'You can only write this.
Dim x As Nullable(Of Integer) = Nothing
 
In C#: 
//You can either write this...
Nullable<int> x1 = null;

//or simply like this...
int? x2 = null;
 
I hope it gives you additional info. Happy programming!
Regards,
Van


Dont forget to Mark as Answer if it helps you.
0
van
12/11/2008 4:15:21 PM

 Why just not:

DateTime date = new DateTime();
  
Dev
0
devcalpoly
12/11/2008 4:23:22 PM

devcalpoly:

 Why just not:

DateTime date = new DateTime();

  

 

Because that does not accept a null value.

 

Try to assign a null value: 

DateTime date = new DateTime();
date = null;
 

and you will get an error.

 

Put question mark: 

DateTime? date = new DateTime();
date = null;
 

and it will be okay.


Regards,
Van


Dont forget to Mark as Answer if it helps you.
0
van
12/11/2008 4:36:36 PM

 Make sense, after reading the following on another blog:

Nullable value types (denoted by a question mark, e.g. int? i = null;) which add null to the set of allowed values for any value type. This provides improved interaction with SQL databases, which can have nullable columns of types corresponding to C# primitive types: an SQL INTEGER NULL column type directly translates to the C# int?.



Dev
0
devcalpoly
12/11/2008 5:40:21 PM

devcalpoly:

 Make sense, after reading the following on another blog:

Nullable value types (denoted by a question mark, e.g. int? i = null;) which add null to the set of allowed values for any value type. This provides improved interaction with SQL databases, which can have nullable columns of types corresponding to C# primitive types: an SQL INTEGER NULL column type directly translates to the C# int?.


Nice explanations.

Cheers.


Regards,
Van


Dont forget to Mark as Answer if it helps you.
0
van
12/11/2008 6:18:17 PM

Hi

You can assing null value by using:

Date1.date=nothing   

Best Regards.

 


TUFAN01MAN
0
TUFAN01MAN
12/11/2008 6:55:33 PM

TUFAN01MAN:

Hi

You can assing null value by using:

Date1.date=nothing   

Best Regards.

 

 

You cannot assign value to a ReadOnly property.


Regards,
Van


Dont forget to Mark as Answer if it helps you.
0
van
12/12/2008 12:28:44 AM

In C#: 

DateTime d;
d = Convert.ToDateTime(null);
 

In VB.NET: 

Dim d As DateTime
d = CType(Nothing, DateTime)
 

I notice that this is not assigning null or Nothing but actually assigning the smallest possible value of DateTime.

I hope it gives you additional info. Happy programming!


Regards,
Van


Dont forget to Mark as Answer if it helps you.
0
van
12/12/2008 1:32:55 AM

 hi


Nullable<DateTime> date = null; or DateTime? date = null;
 

0
xuannien84
12/12/2008 1:33:03 AM
yes,it worked fine.thank you.
0
xhmjoy
2/21/2009 1:08:40 PM

hi

Enjoy buddy

Nullable<DateTime> dt = null;

 

vinay


Vinay Kumar Sinha
.net Sr. Developer
Hyderabad
0
vinayget
2/21/2009 1:13:17 PM

Declare your datetime as following:

public System.Nullable<System.DateTime> YourDateTime;

0
hlaasri123
4/22/2009 2:24:28 PM
Reply: