Super Girl
Super Girl

Reputation: 117

Specified cast is not valid

I have an application that get a datetime value from the database but it give me this error

Specified cast is not valid

here is the code

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Golden_RoseConnectionString"].ConnectionString);

con.Open();
String sel3 = "select Booking_Date from booking where Booking_ID='" + Booking_Id + "'";
SqlCommand cmd3 = new SqlCommand(sel2, con);
SqlDataReader n3 = cmd3.ExecuteReader();
n3.Read();
DateTime Booking_Date = (DateTime)n3.GetSqlDateTime(0);
con.Close();

How can I solve this problem?

Upvotes: 1

Views: 884

Answers (2)

ridoy
ridoy

Reputation: 6332

You should use:

var Date = Convert.ToDateTime(n3["YOUR_DATE_COLUMN"]);

Upvotes: 2

radarbob
radarbob

Reputation: 5101

an sql date is not a .Net DateTime object.

GetSqlDateTime does not do a convertion - see MSDN.

It must be converted - see the SO question as an example

Upvotes: 2

Related Questions