Reputation: 690
I made a date picker from the calendar control in ASP.net using C#. The date format in the TextBox is 08/01/2012
.
When I try to do an insert I get this error:
Message: Input string was not in a correct format.
here is my code:
cmd.Parameters.Add("@date", SqlDbType.DateTime).Value =
Convert.ToDateTime(txtDateins.Text);
How do I get that fixed ?
Upvotes: 1
Views: 5242
Reputation: 690
I had my table column defined as 15,2 and then I changed it to 18,2 and everything is working. I don't know why that happened. I am hoping to find a reason.
Upvotes: 0
Reputation: 20620
The error is from parsing the text into a DateTime object, not SQL.
Try using TryParseExact to convert the string to a DateTime object. And then, in a separate line, try inserting it. That way you will not be confused again about where the error is.
Upvotes: 2
Reputation: 1713
I would try .AddWithValue()
and DateTime.TryParse()
:
DateTime date;
if (DateTime.TryParse(txtDateins.Text, out date)
{
cmd.Parameters.AddWithValue("@date", date);
}
This will handle parsing errors and make sure you're getting valid data in the DB.
Upvotes: 0