locked
String was not recognized as a valid DateTime

    Question

  • Dear All,

    I changed the locale in my regional settings to Vietnamese, so the datetime values will be formatting to the datetime format string in vietnamese (ex:'2/20/2011 7:30 AM' to '20/2/2011 7:30 SA', '2/20/2011 2:00 PM' to '20/2/2011 2:00 CH'), I have a datatable which have struct and data as follow:

    Task (DataType is System.String)---
    StartTime (DataType is System.String
    task1 15/2/2011 8:30 SA
    task2 14/2/2011 10:30 SA
    task3 15/2/2011 3:30 CH
    task4 15/2/2011 2:00 CH

     

    Now i want to add a DataColumn into the Datatable above, the code as follow:

    tblData.Columns.Add(new DataColumn("CopyStartTime", typeof(DateTime), "[StartTime]"))

    When i run the code above i am getting the error message as follow : "String was not recognized as a valid DateTime"

    I have test successful with the code as follow:

    Convert.ToDateTime('15/2/2011 8:30 SA', new CultureInfo("vi-VN"));

    I have researched the DataColumn.Expression article in MSDN, i found the function "Convert(expression, type)", i tried with it but still getting the error message above..

    can anyone give me some solutions to solve this issue, thanks in advance.

    Phu Dang

    Wednesday, March 02, 2011 3:11 AM

Answers

  • Hi,

    You can try to change the current CultureInfor to 'vi-VN' before you add the expression column.

    e.g.

     System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("vi-VN");
        System.Threading.Thread.CurrentThread.CurrentCulture = ci;
        System.Threading.Thread.CurrentThread.CurrentUICulture = ci; 
    
     DataTable dt = new DataTable();
            dt.Columns.Add("Test1", typeof(string));
            dt.Columns.Add("Test2", typeof(string));
            dt.Columns.Add("Test3", typeof(string));
            dt.Columns.Add("StartTime", typeof(string));
            DataRow dr1 = dt.NewRow();
            dr1[0] = "test1";
            dr1[1] = "test12";
            dr1[2] = "test1";
            dr1[3] = "15/2/2011 8:30 SA";
            dt.Rows.Add(dr1);
            DataRow dr2 = dt.NewRow();
            dr2[0] = "test2";
            dr2[1] = "test21";
            dr2[2] = "test2";
            dr2[3] = "10/12/2011 8:30 SA";
            dt.Rows.Add(dr2);
            dt.Columns.Add("TestDateTime", typeof(DateTime), "[StartTime]");
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
    

    Microsoft Online Community Support
    • Marked as answer by KeFang Chen Tuesday, March 08, 2011 10:09 AM
    Thursday, March 03, 2011 5:06 AM

All replies

  • Hi,

    As you said when you convert the string into datatime with proper cultureinfo then it's working fine so why are you not converting the string. And also you can retrieve cultureinfo programatically which you can use instead of using hard coded.

     


    thanksArup MCTS - SharePoint

    Play Sudoku Online
    Wednesday, March 02, 2011 6:02 AM
  • Hi Arup,

    Thanks for your reply, i am using hard coded just to test and then I realized that if i want to convert the string which was recognized then it must be specified the cultureinfo, but in the function Convert of the DataColumn.Expression object not support, you can be refer it at link here http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx , maybe, i'll solve this issue to the following code :

            private void prepareData()

           {

                   tblData.Column.Add("CopyStartTime", typeof(DateTime));

                  DateTimeFormatInfo  datetimeFormat = this.getFormatInfo();

                  foreach(DataRow dr in tblData.Rows)

                 {

                     dr["CopyStartTime"] = Convert.ToDateTime(dr["StartTime"].ToString(), datetimeFormat);

                 }

           }

           private DateTimeFormatInfo getFormatInfo()
           {
                SPWeb spWeb = SPControl.GetContextWeb(this.Context);
                DateTimeFormatInfo formatInfo = spWeb.Locale.DateTimeFormat;
                if (spWeb.CurrentUser != null && spWeb.CurrentUser.RegionalSettings != null)
                {
                    formatInfo = CultureInfo.GetCultureInfo((int)spWeb.CurrentUser.RegionalSettings.LocaleId).DateTimeFormat;
                }
                return formatInfo;
            }

    However the performance of this solution not optimum. anyone has the better ideas?

    Thanks,

    Phu Dang

    Wednesday, March 02, 2011 7:21 AM
  • Hi,

    You can try to change the current CultureInfor to 'vi-VN' before you add the expression column.

    e.g.

     System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("vi-VN");
        System.Threading.Thread.CurrentThread.CurrentCulture = ci;
        System.Threading.Thread.CurrentThread.CurrentUICulture = ci; 
    
     DataTable dt = new DataTable();
            dt.Columns.Add("Test1", typeof(string));
            dt.Columns.Add("Test2", typeof(string));
            dt.Columns.Add("Test3", typeof(string));
            dt.Columns.Add("StartTime", typeof(string));
            DataRow dr1 = dt.NewRow();
            dr1[0] = "test1";
            dr1[1] = "test12";
            dr1[2] = "test1";
            dr1[3] = "15/2/2011 8:30 SA";
            dt.Rows.Add(dr1);
            DataRow dr2 = dt.NewRow();
            dr2[0] = "test2";
            dr2[1] = "test21";
            dr2[2] = "test2";
            dr2[3] = "10/12/2011 8:30 SA";
            dt.Rows.Add(dr2);
            dt.Columns.Add("TestDateTime", typeof(DateTime), "[StartTime]");
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
    

    Microsoft Online Community Support
    • Marked as answer by KeFang Chen Tuesday, March 08, 2011 10:09 AM
    Thursday, March 03, 2011 5:06 AM
  • Hi KeFang Chen, thank you so such for your support.

     

    Phu Dang

    Monday, March 14, 2011 1:34 AM