CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2003
    Posts
    145

    Problem with DataTable’s ‘Select’ method

    I have a weird problem with the DataTable’s ‘Select’ method.
    I use the ‘Select’ method throughout my application, and it works fine, but there is one occasion when it doesn’t work.

    One of the columns in my DataTable is of type bool, named ‘X’.
    At any point during the running of the application there is only one row at the most, that has this column set to true.
    When I want to find out that row, I use the following statement:
    Code:
    DataRow[] rows = this.myDataSet.Tables["Item"].Select("[X] = 'true'");
    if (rows.Length > 0)
    {
    	// do something wirh rows[0]
    }
    This works fine, but there is one occasion that I spotted where the ‘Select’ doesn’t find any row, even though I know there’s one row (with ‘Id’ 123 for instance) having column ‘X’ set to true,

    In order to make sure I’m not imagining things, I added the following line above:
    Code:
    DataRow[] tempRows = this.myDataSet.Tables["Item"].Select("[Id] = '123'");
    I started debugging and stopped at the point in the application when I have this problem.
    In the watch window, I saw the following:

    tempRows[“Id”] = 123
    tempRows[“X”] = true
    rows.Length = 0

    How is that possible?
    Did anyone stumble upon this problem?
    When I change a value of a row’s cell do I have to use the ‘AcceptChanges’ method?

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Problem with DataTable’s ‘Select’ method

    Please, please please, do NOT use Select for functionallity such as this. It is totally in appropriate. The usage of Select should be completely restricted to those circumatances where you can not know the "expression" at compile time.

    For ALL other conditions, please use the appropriate functionallity.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Mar 2003
    Posts
    145

    Re: Problem with DataTable’s ‘Select’ method

    I don’t understand your answer.
    Using the ‘Select’ method is the only way I know to find rows that answer a certain condition. Is there another way?

    P.S.
    Sorry about the late reply.

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Problem with DataTable’s ‘Select’ method

    What CPUWizard is talking about IMHO is that you should use
    'Select' in cases like

    Code:
    DataRow[] rows = this.myDataSet.Tables["Item"].Select([X] =" + myBool.ToString() + ")";
    if (rows.Length > 0)
    {
    	// do something with rows[0]
    }
    where myBool is different, depending on other results in your program at runtime. But IMHO you have only given an example here and 'true' isn't implemented this way, isn't it ? My question is: is X of type bool and why do you use apostrophs in your code as as much as I know this compares only strings ?
    Last edited by JonnyPoet; June 6th, 2008 at 06:15 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Mar 2003
    Posts
    145

    Re: Problem with DataTable’s ‘Select’ method

    The ‘X’ column is of type bool.
    Why I use the apostrophes is in fact a good question. I think I saw it in an example somewhere.
    You don’t think that this is what causes the problem, do you?

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Problem with DataTable’s ‘Select’ method

    Quote Originally Posted by TheCPUWizard
    Please, please please, do NOT use Select for functionallity such as this. It is totally in appropriate. The usage of Select should be completely restricted to those circumatances where you can not know the "expression" at compile time.

    For ALL other conditions, please use the appropriate functionallity.
    I dont find a problem with it.. It is for xmpl, perfctly reasonably to use Select to pull a list of rows from a datatable where the user has elected to e.g. Print that row by ticking a box.. The underlying column is set to true, and you the coder basically want a list of rows where the ShouldPrint column is true. I do have a problem if someone is downloading a whole database into a datatable then using select instead of proper SQL queries..

    OK, X as a column name sucks, but it might be obfuscated for public forum..


    Holly, I think you might be able to just say:

    .Select("[X]")


    -> the parser requires that a boolean operation results. If youre selecting a boolean column, then you shouldnt need to compare it to another boolean to produce a boolean.. However if you find you do need to, I would try removing the apostrophes. There is a world of difference between:

    true
    'true'


    one is a boolean, the other (in .Select() terms) is a string containing 4 characters: t, r, u and e.
    If the column in question were a string column containing "true" and "false" then equating to 'true' would work
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Problem with DataTable’s ‘Select’ method

    Quote Originally Posted by Holly_vi
    The ‘X’ column is of type bool.
    Why I use the apostrophes is in fact a good question. I think I saw it in an example somewhere.
    You don’t think that this is what causes the problem, do you?
    I wouldn't bet, I would simple try but IMHO it could do a problem as apostrophes are for strings
    Last edited by JonnyPoet; June 6th, 2008 at 10:42 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    Mar 2003
    Posts
    145

    Re: Problem with DataTable’s ‘Select’ method

    I tried movig the apostrophes, but it made no change.

  9. #9
    Join Date
    Apr 2006
    Posts
    220

    Re: Problem with DataTable’s ‘Select’ method

    You can use DataView and its filter mechanism for your desired task, also.

  10. #10
    Join Date
    Mar 2003
    Posts
    145

    Re: Problem with DataTable’s ‘Select’ method

    Quote Originally Posted by nabeelisnabeel
    You can use DataView and its filter mechanism for your desired task, also.
    I don't understand what you mean. Can you explain?

  11. #11
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Problem with DataTable’s ‘Select’ method

    The reason I recommend against using Select in most circumstances is because of the way it works... This happens AT RUNTIME when the select statement is encountered...

    1) Evaluate the Expression, and generate SOURCE CODE that will evaluate the condition in terms of elements in a row.

    2) Feed that source code to the Compiler, generate MSIL

    3) Load the MSIL into an AppDomain.

    4) JIT Compile the code.

    5) Iterate over each row passing the row to the dynamically generated code.

    EXTREMELY POWERFUL....EXTREMELY COMPLEX....HIGH POSSIBILITY FOR ERRORS (failure to load appdomain,. failure to invoke compiler....)....HIGH OVERHEAD (memory AND performance).

    Therefore it should only be used where you can NOT code:
    Code:
    List<DataRow> result = new List<DataRow>();
    foreach (DataRow row in Table.Rows)
    {
       if (/*write your own code here*/)
        result.Add(row);
    }
    As long as you can fill in the comment, then this is MUCH preferred. However if you want to allow dynamic (unpredictible) input such as:

    [CustomerNumber]>1000 && [Price]<49.99
    or
    [CustomerState]="NY" && [Quantity]>10

    Then by all means use Select.


    But if you KNOW the comparision is going to involve specific fields (compared angainst unknown limits) then a coded loop is much better.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  12. #12
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Problem with DataTable’s ‘Select’ method

    Quote Originally Posted by Holly_vi
    I don't understand what you mean. Can you explain?
    Re DataView: He is talking about this
    http://msdn.microsoft.com/en-us/libr....dataview.aspx
    a very powerfull class
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Problem with DataTable’s ‘Select’ method

    Quote Originally Posted by JonnyPoet
    Re DataView: He is talking about this
    http://msdn.microsoft.com/en-us/libr....dataview.aspx
    a very powerfull class

    That is indeed a powerful class. and I use it somewhat often. Since it is intended for UI, the performance demands are (typically) much lower.

    For example, it is generally considered acceptable if a user action returns a response in under 300mS (just less than 1/3 of a second). Anthing between "instant" and 300mS is not noticable.

    Since this class would (again typically) be impacted ONCE per user update, the performance would be well within the acceptable bounds.

    My earlier posts were dealing with non-UI situations (since they were classes not tied to a UI). Man many cases the code may need to run freq frequently (inside a loop perhaps), and performance can become critical.

    Sincein those situations it is just as easy (if the topoligy of the selection criteria is known) to wrie the 4 lines of code, the "benefits" of the one line select turn into detriments more often than not.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured