0

Hi i am developing an application, in that i required to store date in sql server 2005 database which is retrieved from a textbox in front end. In front end, user is allowed to pick a date from JQuery UI DatePicker plugin.

Error I am getting is as follows:

String was not recognized as a valid DateTime.

Code i have is:

db.eventDate = DateTime.Parse(txtDate.Text, new CultureInfo("en-US").DateTimeFormat);
7
  • be sure that the input is the correct format you are trying to convert to for the specified cultureinfo. what is the input format you are providing? Commented Apr 14, 2014 at 5:58
  • for ex: if i choose a date from the JQuery UI Datepicker, it would be like 14/04/2014 in textbox. I am trying to convert that textbox data (i.e., 14/04/2014) to a DateTime Format. Commented Apr 14, 2014 at 6:21
  • Right. this is exactly why it is not working - your format is a UK format. you need to give it the uk format so it can parse it to the correct datetime format in this case then convert it to a us format. however in the jquery ui picker I am sure you can set the default input type/format to be en-us which will then correctly set the format to a us format and then your parsing will work correctly. Commented Apr 14, 2014 at 6:35
  • while debugging the code i came to know that, textbox date is in format of 14/04/2014 and after converting it to a DateTime type it will remain like 1/1/0001 12:00 AM Commented Apr 14, 2014 at 6:36
  • yes because that is the default value when it cannot parse (I believe). like I said, do as I suggested Commented Apr 14, 2014 at 6:48

6 Answers 6

1

Try this code

string yourDate = txtDate.Text;
string formattedDate = yourDate.ToString("dd-MM-yyyy");
DateTime dt = DateTime.ParseExact(formattedDate , "dd-MM-yyyy", CultureInfo.InvariantCulture);
Sign up to request clarification or add additional context in comments.

Comments

0

You should either use DateTime.TryParse(expression) or wrap your call to DateTime.Parse in a try/catch block. Just out of interest, can you post some example dates you're converting ? They are in US format, right ?

2 Comments

Incorrect. having a try catch block around DateTime.TryParse will not throw an exception. TryParse does just that - tries to parse the given datetime value and returns true if success or false if not successful.
Please re-read what I wrote, I said "wrap your call to DateTime.Parse in a try/catch block", not DateTime.TryParse
0

you better use DateTime.ParseExact with the correct date time format you set to date time picker

for example if the format is "MM/dd/yyyy" you can do as below

db.eventDate =  DateTime.ParseExact(txtDate.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture);

1 Comment

Hi @Damith, I tried your code too. Now i am getting error like <prev><code>The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.</code></prev>
0

Try this..Working fine for me..

Convert.ToDateTime(txtDate.Text,System.Globalization.CultureInfo.GetCultureInfo("en-US").DateTimeFormat);

or

Convert.ToDateTime(txtDate.Text);

2 Comments

Hi Jameem, Thanks for your immediate response. I have tried your suggestion but still i am getting same error.
In database it is 1/1/1900 12:00:00 AM by default. In my application user will pick date from JQuery UI date picker and it would be in the form 14/04/2014 and i am trying to convert this data from textbox to a valid DateTime type in code behind before inserting into database.
0

Hope following code helps you.

        Dim dt As New DateTime
        Dim txtDateFromUser As String = "04/09/2014"

        If DateTime.TryParse(txtDateFromUser, dt) = True Then

            Dim connStr As String
            connStr = System.Configuration.ConfigurationManager.ConnectionStrings("youConnectionStringName").ConnectionString

            'Insert into database
            Dim sqlconn As New SqlConnection
            sqlconn.ConnectionString = connStr
            Dim sqlcmd As New SqlCommand("insert into tblDateTime (dtTestDate) values ('" + dt.ToString() + "')")
            sqlconn.Open()
            sqlcmd.Connection = sqlconn
            sqlcmd.ExecuteNonQuery()

        End If

Here is the test table I've used:

        create table tblDateTime (dtTestDate datetime)

2 Comments

Nothing happened:( textbox date is getting converted to a default date 1/1/0001 12:00:00 AM I don't know how to come out of this error. And morething, in database it's data type is datetime only.
what is the date string you're getting from the textbox?
0

i simply changed the date format of JQuery UI DatePicker in my code. Before it was in UK format so i was getting error while saving data to database. But once i change it to US format it working fine.

I thanks for everyone who reviewed my question and suggested me the answers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.