none
DataTable.Select problem when moving from .net1.1 to .net4.0 RRS feed

  • Question

  • Hi,

    I am moving c# code from .net1.1 to .net4.

    The .net1.1 code is working ok, when running it with .net4 framework i get this error :

    System.Data.EvaluateException: Cannot perform '=' operation on System.DateTime and System.String.     at System.Data.BinaryNode.SetTypeMismatchError(Int32 op, Type left, Type right)     at System.Data.BinaryNode.BinaryCompare(Object vLeft, Object vRight, StorageType resultType, Int32 op)     at System.Data.Select.Eval(BinaryNode expr, DataRow row, DataRowVersion version)     at System.Data.Select.Evaluate(Int32 record)     at System.Data.Select.FindFirstMatchingRecord()     at System.Data.Select.GetBinaryFilteredRecords()     at System.Data.Select.SelectRows()     at System.Data.DataTable.Select(String filterExpression)     at

    I found that i need to use convert to invariant in the first paramter of the DataTable.Select method.

    but there is lot of places in my .net1.1 code using the datatable.select without using the convert , is there is an elegant way for porting this code?

     

    Thanks in advance,

    Yaron.

     

     

    Monday, August 16, 2010 2:23 PM

Answers

  • > I found that i need to use convert to invariant in the first paramter of the DataTable.Select method.

    Could you provide more details:

    1.  The actual expression you are passing to Select.

    2.  Your region / current culture (Thread.CurrentCulture).

    Note that setting the entire DataSet to invariant, and then correspondingly using invariant in the Select expression, is a good practice: http://msdn.microsoft.com/en-us/library/ms182188.aspx  I'm not sure what changed with .NET 4.0, but providing this additional info might help get to the bottom of this.

     

    Tuesday, August 17, 2010 12:43 AM
  • Hi Yaron,

    Since your thread CurrentCulture is "he-IL", and by default the locale of your dataset is en-US, so there is a mismatch between these two cultures. I am wondering how did you set the CurrentThread's CurrentCulture to "he-IL", through your code or by setting the Region and Language of your system ? And how did you add your dataset, through the designer ? If you add the dataset through the designer, the default locale of the dataset is "en-US", so even if you change the CurrentThread's CurrentCulture to "he-IL", that dataset's locale is still "en-US". You can change the default locale of the dataset to "he-IL" through designer by changing the Locale property of the dataset in the property window of the dataset designer. If you add the dataset through code, for example, by "DataSet ds = new DataSet", then this dataset's culture info is the same with your current thread.

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, September 16, 2010 3:04 AM
    Moderator

All replies

  • > I found that i need to use convert to invariant in the first paramter of the DataTable.Select method.

    Could you provide more details:

    1.  The actual expression you are passing to Select.

    2.  Your region / current culture (Thread.CurrentCulture).

    Note that setting the entire DataSet to invariant, and then correspondingly using invariant in the Select expression, is a good practice: http://msdn.microsoft.com/en-us/library/ms182188.aspx  I'm not sure what changed with .NET 4.0, but providing this additional info might help get to the bottom of this.

     

    Tuesday, August 17, 2010 12:43 AM
  • Hi Yaron, 

    I'm writing to follow up the post. Does BinaryCoder's suggestion work ? Or could you please provide more detailed information ?

    Please feel free to let me know if you need any help.

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Sunday, August 22, 2010 4:45 AM
    Moderator
  •  

    1.   filter= " myDate <= '18/08/2010 00:00:00' " 

    2.

    System.Threading.Thread.CurrentThread.CurrentCulture

    {he-IL}

    Calendar: {System.Globalization.GregorianCalendar}

    CompareInfo: {CompareInfo - he-IL}

    CultureTypes: SpecificCultures | InstalledWin32Cultures | FrameworkCultures

    DateTimeFormat: {System.Globalization.DateTimeFormatInfo}

    DisplayName: "Hebrew (Israel)"

    EnglishName: "Hebrew (Israel)"

    IetfLanguageTag: "he-IL"

    IsNeutralCulture: false

    IsReadOnly: false

    KeyboardLayoutId: 1037

    LCID: 1037

    Name: "he-IL"

    NativeName: "עברית (ישראל)"

    NumberFormat: {System.Globalization.NumberFormatInfo}

    OptionalCalendars: {System.Globalization.Calendar[2]}

    Parent: {he}

    TextInfo: {TextInfo - he-IL}

    ThreeLetterISOLanguageName: "heb"

    ThreeLetterWindowsLanguageName: "HEB"

    TwoLetterISOLanguageName: "he"

    UseUserOverride: true

     

    when i use this :

    string

     

     

    filter = string.Format(CultureInfo.InvariantCulture, "Convert({0}, 'System.DateTime') <= '{1}',a,b)

    it's solved the problem. it's changed the datetime format to mmddyyyy.

    Thanks.

     

    Sunday, August 22, 2010 2:13 PM
  • by default the locale of my dataset is en-US.

    my thread CurrentCulture is he-IL, so there is a problem with the date time format (which was not with .net1.1).

    I want to use the he-IL culture in my dataTable.Select(filter) for datetime fields.

    is there a one place to fix this mismatch so it will affect all my dataTable.Select(filter) which are use datetime fields in the filter expression ?

    is something was change between the .net versions related to this issue ?

     

    Thanks in advance,

    Yaron.

     

     

     

    Monday, August 23, 2010 6:51 AM
  • Hi Yaron,

    I think you need to set DataSet.Locale property to he-IL culture, then use dataTable.Select(filter) to select records. For example :

    System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("he-IL");
    dataset.Locale = culture;
    ...

    dataTable.Select(filter)

    You can refer to the following link to get more information about DataSet.Locale Property.
    http://msdn.microsoft.com/en-us/library/system.data.dataset.locale.aspx

    Best regards,

    Alex Liang

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, August 23, 2010 12:27 PM
    Moderator
  • Hi Yaron, 

    I'm writing to follow up the post. Does above suggestion work ?

    Please feel free to let me know if you need any help.

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, August 26, 2010 3:30 AM
    Moderator
  • Hi Alex,

    is there is a config file setting the locale for the application (the thread default locale and the dataset default locale)?

    is something chage after .net version 1.1 about how the datatable select method use locale?

    Thanks,

    Yaron.

     

    Wednesday, September 15, 2010 9:38 AM
  • Hi Yaron,

    Since your thread CurrentCulture is "he-IL", and by default the locale of your dataset is en-US, so there is a mismatch between these two cultures. I am wondering how did you set the CurrentThread's CurrentCulture to "he-IL", through your code or by setting the Region and Language of your system ? And how did you add your dataset, through the designer ? If you add the dataset through the designer, the default locale of the dataset is "en-US", so even if you change the CurrentThread's CurrentCulture to "he-IL", that dataset's locale is still "en-US". You can change the default locale of the dataset to "he-IL" through designer by changing the Locale property of the dataset in the property window of the dataset designer. If you add the dataset through code, for example, by "DataSet ds = new DataSet", then this dataset's culture info is the same with your current thread.

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, September 16, 2010 3:04 AM
    Moderator
  • Hi Yaron, 

    I'm writing to follow up the post. Does above suggestion work ?

    Please feel free to let me know if you need any help.

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, September 27, 2010 5:48 AM
    Moderator
  • Hi Yaron, 

    I'm closing this thread and marking the reply as answer. Please feel free to let me know if you need any help.

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, October 6, 2010 7:46 AM
    Moderator