PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] [2005] Selecting a specific Row from a DataTable.-VBForums
Results 1 to 3 of 3

Thread: [RESOLVED] [2005] Selecting a specific Row from a DataTable.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    88

    Resolved [RESOLVED] [2005] Selecting a specific Row from a DataTable.

    Hi all. I could use a little help with selecting a specific DataRow from a DataTable.

    I am using the following code to select specific DataRows from a DataTable and this works fine, but many times I am looking for only a single row of data (there is not a possibility of multiple rows) and I was wondering if there is a better method to select a single row rather than the For-Next loop which seams set up specifically for multiple rows.

    Orů am I just making too much of this? In this example, the DataTable does not have any controls bound to it.

    Code:
    For Each row As ScoreboardDataSet.tbl_StudentPhysicalDataRow In Me.ScoreboardDataSet.tbl_StudentPhysicalData.Select(String.Format("StudentID LIKE '{0}'", GWrestlerID))
    
        If Not row.LastWeighInWeight = Nothing Then MyVariable = row.LastWeighInWeight
    
    Next Mrow
    Thanks for your help.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,252

    Re: [2005] Selecting a specific Row from a DataTable.

    If you want to find a single row by its primary key the you would use the DataRowCollection.Find method, e.g.
    vb.net Code:
    1. Dim row As DataRow = table.Rows.Find(id)
    Find also accepts an array of values for composite keys.

    If you want to find one or more rows based on one or more non-key columns you would use the DataTable.Select method, as you are. If you know for a fact that there's only going to be one row then there's no need to use a loop. You'd just get the row at index 0 straight off, e.g.
    vb.net Code:
    1. Dim row As DataRow = table.Select(String.Format("Name = '{0}'", name))(0)
    Note also that if you are looking for a row where a field value is equal to a specific value then you should use '=', not 'LIKE'. If you are not using wildcards then using 'LIKE' is pointless.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    88

    Thumbs up Re: [2005] Selecting a specific Row from a DataTable.

    Perfect... Thanks JM.

    I have been in the MSDN Lib reading about the DataTable Methods, but this really puts a bow on the information provided there.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width