C-Sharp-Noob
C-Sharp-Noob

Reputation: 25

Unable to cast object of type 'System.DateTime' to type 'System.String'

Getting an error that has me stumped. I'm assuming it's something simple but can't seem to figure this one out, this is the code that is causing the error:

if ((string) _nullableDateTimePicker1.Value != string.Empty && _nullableDateTimePicker1.Value != null) {
     _nullableDateTimePicker1.Value = null;
}

When clicking on the search button in my windows form, this is what I get popping up:

Unable to cast object of type 'System.DateTime' to type 'System.String'.

Upvotes: 0

Views: 21466

Answers (7)

Jegan
Jegan

Reputation: 1237

DateTimePicker.Value returns DateTime type you cannot cast it string, what you need is to conver to string.

string dateTime = _nullableDateTimePicker1.Value.ToString();

so your code will be:

string dateTime = _nullableDateTimePicker1.Value.ToString();
if (!string.IsNullOrEmpty(dateTime)) 
{
    dateTime = null; 
 }

I am not sure about setting the DateTimePicker value to null.

Upvotes: 0

Tom Jacques
Tom Jacques

Reputation: 705

It looks like your _nullableDateTimePicker1 object is a Nullable<DateTime> (DateTime?), in which case you're getting this error because you can't cast it to a string.

(string) _nullableDateTimePicker1.Value

It looks like you're just trying to ensure that it has a valid DateTime, in which case you only need to do the null check. Without knowing exactly what you're trying to do that's my advice.

Upvotes: 0

orel
orel

Reputation: 1469

This code should work:

if (_nullableDateTimePicker1.Value != null && Convert.ToString(_nullableDateTimePicker1.Value) != string.Empty) {
     _nullableDateTimePicker1.Value = null;
}

Upvotes: 0

Daniel W.
Daniel W.

Reputation: 449

(string) _nullableDateTimePicker1.Value

throws the exception when _nullableDateTimePicker1 is null.

Don't use (string) when trying to check if value is not null

Upvotes: 0

Grant Thomas
Grant Thomas

Reputation: 45083

It appears the type of the nullable-type instance of _nullableDateTimePicker1 is DateTime, which is not a string; you would need to compare with a DateTime or convert that to a string.

So, in the simplest sense:

_nullableDateTimePicker1.Value.ToString() != string.Empty

However, beware the danger of not checking HasValue, as this would cause a NullReferenceException if Value is null; So, your check is a little backwards.

And even then, if it wasn't null, you'd have a DateTime.MinValue, and so ToString wouldn't return an empty string. Therefore, check for null on the nullable thing, and if not null, compare to DateTime.MinValue (unless there's a quirk specifically in your code which means it could have a different 'default'.

Upvotes: 1

Piotr Stapp
Piotr Stapp

Reputation: 19830

In this part (string) _nullableDateTimePicker1.Value != string.Empty you are trying to cast value to string. But Value is DateTime. The second part of this if is enough.

Upvotes: 0

David S.
David S.

Reputation: 6105

if ((string) _nullableDateTimePicker1.Value .....

Probably, _nullableDateTimePicker1.Value returns a DateTime. As the message explains, you can't directly cast a DateTime to a string.

Upvotes: 0

Related Questions