Unable to cast object of type 'System.DateTime' to type 'System.String'.
Description: An
unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.String'.
Source Error:
Line 19: {
Line 20: String ID = TextBox1.Text;
Line 21: Reservation rs = db.Reservations.SingleOrDefault(o => o.reserveID.ToString() == ID);
Line 22:
Line 23: if (rs != null)
From your error message, this is a type conversion error. I suggest you check your code as the following options.
*Reservation class is match the database table.
*reserveID value and ID value are correct.
You could also share us more relevant code to help us reproduce the problem.
Best Regards,
Yohann Lu
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
None
0 Points
1 Post
Unable to cast object of type 'System.DateTime' to type 'System.String'
Mar 27, 2016 06:07 AM|limchiawei1|LINK
I want to retrieve data(date) from my database through my linqdatasource, but it seems point out
Unable to cast object of type 'System.DateTime' to type 'System.String'
My Code Sample:
protected void Button4_Click(object sender, EventArgs e)
{
String ID = TextBox1.Text;
Reservation rs = db.Reservations.SingleOrDefault(o => o.reserveID.ToString == ID);
if (rs != null)
{
lblreserveID.Text = rs.reserveID;
lblpName.Text = rs.foodPackage;
lblnoOfTable.Text = rs.noOfTable.ToString();
lbldate.Text = rs.reserveDate.ToString();
}
Error Warning:
Unable to cast object of type 'System.DateTime' to type 'System.String'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.String'.
Source Error:
Line 19: { Line 20: String ID = TextBox1.Text; Line 21: Reservation rs = db.Reservations.SingleOrDefault(o => o.reserveID.ToString() == ID); Line 22: Line 23: if (rs != null)
Star
11464 Points
2439 Posts
Re: Unable to cast object of type 'System.DateTime' to type 'System.String'
Mar 28, 2016 07:01 AM|Yohann Lu|LINK
Hi limchiawei1,
From your error message, this is a type conversion error. I suggest you check your code as the following options.
*Reservation class is match the database table.
*reserveID value and ID value are correct.
You could also share us more relevant code to help us reproduce the problem.
Best Regards,
Yohann Lu