locked
DateTime Culture problem : "string was not recognized as a valid datetime". RRS feed

  • Question

  • Hi,

    I'm writing a code to insert the value from the csv file to SQL Server using C#. Everything seems working fine except the date issue. I get the date in the string format (14/12/2010) from the csv file. When i try to store this value to SQL Server column, it throws error "string was not recognized as a valid datetime". I convert the value using Convert.ToDateTime(), it works on my vPC but not on the main server.

    After some quick tests, i noticed that it doesn't throw error if the date is less than equals to 12/12/2010 (or when dd and mm not greater than 12). So, I understand that it is creating problem with the month. The culture in my vPC is United Kingdon as on my Server.

    I tried following options. But, it is not working on my server which has a United Kingdom culture and SQL server saves the date in yyyy-MM-dd format. The column in SQL Server is a datetime column.

    Option 1:

    string SSD = dt.Rows[i]["ScheduledStartDate"].ToString();
    string SED = dt.Rows[i]["ScheduledEndDate"].ToString();
    
    CultureInfo ciGB = new CultureInfo("en-GB");
    DateTime dtSSD = DateTime.Parse(SSD, ciGB); 
    DateTime dtSED = DateTime.Parse(SED, ciGB); 
    

    Option 2:

    string SSD = Convert.ToDateTime(dt.Rows[i]["ScheduledStartDate"].ToString()).ToString("yyyy-dd-MM"); 
    string SED = Convert.ToDateTime(dt.Rows[i]["ScheduledEndDate"].ToString()).ToString("yyyy-dd-MM"); 
    
    Convert.ToDateTime(SSD.ToString()) 
    Convert.ToDateTime(SED.ToString()) 
    

     

    May i know where the problem is ? Thanks,


    Sohaib Khan | http://muhskhac.spaces.live.com

    Tuesday, December 14, 2010 2:58 PM

Answers

  • This is how you can convert the date into a string with the wanted format "yyyy-dd-MM":

       string SSD = "14/12/2010";
       DateTime date = Convert.ToDateTime(SSD);
       string strDate = String.Format("{0:yyyy-MM-dd}", date);
       DateTime newdate = Convert.ToDateTime(strDate);

    WARNING: You said you want the date into this format: "yyyy-dd-MM", but as I have trying out, I always get an error of wrong format. So I have changed into "yyyy-MM-dd". Truly, I havent seen this kind of format as you want to convert to.

    One more thing, in my country the "newDate" get into this fimrat: "dd.MM.yyyy", I dont know how is if you use GB date format.

    Try it out, and let me know.

     

    Mitja

     

    • Marked as answer by Alan_chen Wednesday, December 22, 2010 5:29 AM
    Tuesday, December 14, 2010 3:09 PM

All replies

  • This is how you can convert the date into a string with the wanted format "yyyy-dd-MM":

       string SSD = "14/12/2010";
       DateTime date = Convert.ToDateTime(SSD);
       string strDate = String.Format("{0:yyyy-MM-dd}", date);
       DateTime newdate = Convert.ToDateTime(strDate);

    WARNING: You said you want the date into this format: "yyyy-dd-MM", but as I have trying out, I always get an error of wrong format. So I have changed into "yyyy-MM-dd". Truly, I havent seen this kind of format as you want to convert to.

    One more thing, in my country the "newDate" get into this fimrat: "dd.MM.yyyy", I dont know how is if you use GB date format.

    Try it out, and let me know.

     

    Mitja

     

    • Marked as answer by Alan_chen Wednesday, December 22, 2010 5:29 AM
    Tuesday, December 14, 2010 3:09 PM
  • Mitja,

    No, I don't want (yyyy-dd-MM) format but i tried this as well. Anyway, I tried your way but it also doesn't work. No error as well.

    Thanks for the help.


    Sohaib Khan | http://muhskhac.spaces.live.com
    Wednesday, December 15, 2010 6:46 AM