MaxCoder88
MaxCoder88

Reputation: 2428

Date record problem

I'm using properly JQuery Datepicker script for date entry in the form.When I record within database(SQL Server 2008) the values which are entered,I got the following error.

DateTime adate = Convert.ToDateTime(txtaddeddate.Text);

Error:

String was not recognized as a valid DateTime.

Upvotes: 0

Views: 181

Answers (1)

JonH
JonH

Reputation: 33183

Use ParseExact,

From MSDN:

Converts the specified string representation of a date and time to its DateTime equivalent. The format of the string representation must match a specified format exactly or an exception is thrown.

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

http://msdn.microsoft.com/en-us/library/system.datetime.parseexact.aspx

You didn't state what programming language you were using, I am assuming C#. And this is definately not a SQL Server issue.

What format are you giving the string? (dd/MM/yyyy or mm/dd/yyyy or ...)

Try this:

string myDate = "12/12/2011";
string f = "dd/MM/yyyy";
DateTime dt = DateTime.ParseExact(myDate, f, null);

Change f to fit your format. It also appears you are getting the date from a user so I passed null for the format provider interface, that makes it use the current culture.

Upvotes: 1

Related Questions