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);
vrajs5
  • 4,066
  • 1
  • 27
  • 44
shary.sharath
  • 649
  • 2
  • 14
  • 29
  • 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? – Ahmed ilyas Apr 14 '14 at 05: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. – shary.sharath Apr 14 '14 at 06: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. – Ahmed ilyas Apr 14 '14 at 06: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 – shary.sharath Apr 14 '14 at 06:36
  • yes because that is the default value when it cannot parse (I believe). like I said, do as I suggested – Ahmed ilyas Apr 14 '14 at 06:48
  • Hi @Ahmed, i thank you for your suggestion, but to be frank i tried as you told but no effect. So kindly can you send a snippet of that particular. – shary.sharath Apr 14 '14 at 06:52
  • Hey Ahmed, I really thank you very much... since your suggestion worked for me. Thank you dude... – shary.sharath Apr 14 '14 at 06:56

6 Answers6

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);
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);
Damith
  • 62,401
  • 13
  • 102
  • 153
  • Hi @Damith, I tried your code too. Now i am getting error like The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar. – shary.sharath Apr 14 '14 at 06:01
0

Try this..Working fine for me..

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

or

Convert.ToDateTime(txtDate.Text);
Jameem
  • 1,840
  • 3
  • 15
  • 26
  • Hi Jameem, Thanks for your immediate response. I have tried your suggestion but still i am getting same error. – shary.sharath Apr 14 '14 at 05:56
  • 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. – shary.sharath Apr 14 '14 at 06:24
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 ?

sh1rts
  • 1,874
  • 1
  • 13
  • 14
  • 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. – Ahmed ilyas Apr 14 '14 at 05:58
  • Please re-read what I wrote, I said "wrap your call to *DateTime.Parse* in a try/catch block", not DateTime.TryParse – sh1rts Apr 14 '14 at 06:02
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)
  • 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. – shary.sharath Apr 14 '14 at 06:50
  • what is the date string you're getting from the textbox? –  Apr 19 '14 at 22:01
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.

shary.sharath
  • 649
  • 2
  • 14
  • 29