tags:

views:

128

answers:

1

Hi, I am getting an error when I attempt to display a datetime value in a textbox:

My code is:

txtStartDate.Text = rdrGetUserInfo.IsDBNull(14) ? String.Empty : Convert.ToString(rdrGetUserInfo.GetString(14));

The error message is: ex.Message = "Unable to cast object of type 'System.DateTime' to type 'System.String'."

Any ideas how I can resolve this?

+6  A: 

Try:

txtStartDate.Text = rdrGetUserInfo.IsDBNull(14) ? String.Empty : Convert.ToString(rdrGetUserInfo.GetDateTime(14).ToString());
Leniel Macaferi
Do you still need the `Convert.ToString()`?
Joel Potter
Maybe no... I don't have a compiler right here. I think you can convert a DateTime to String directly with Convert.ToString() if it has a method overload for DateTime.
Leniel Macaferi
Thanks that works. Though how do I get just the date, not the date and time?
user279521
Try to ShortDateString() this way: rdrGetUserInfo.GetDateTime(14).ToShortDateString()
Leniel Macaferi
Perfect. Thanks very much.
user279521
This helped me very much too. Thank you.
Csharp