Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
DataTable Class
DataTable Methods
Select Method
 Select Method (String)
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
DataTable..::.Select Method (String)

Gets an array of all DataRow objects that match the filter criteria in order of primary key (or lacking one, order of addition.)

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)
Visual Basic (Declaration)
Public Function Select ( _
    filterExpression As String _
) As DataRow()
Visual Basic (Usage)
Dim instance As DataTable
Dim filterExpression As String
Dim returnValue As DataRow()

returnValue = instance.Select(filterExpression)
C#
public DataRow[] Select(
    string filterExpression
)
Visual C++
public:
array<DataRow^>^ Select(
    String^ filterExpression
)
JScript
public function Select(
    filterExpression : String
) : DataRow[]

Parameters

filterExpression
Type: System..::.String
The criteria to use to filter the rows.

Return Value

Type: array<System.Data..::.DataRow>[]()[]
An array of DataRow objects.

To create the filterExpression argument, use the same rules that apply to the DataColumn class's Expression property value for creating filters.

The following example uses a filter expression to return an array of DataRow objects.

Visual Basic
Private Sub GetRowsByFilter()

    Dim table As DataTable = DataSet1.Tables("Orders")

    ' Presuming the DataTable has a column named Date.
    Dim expression As String
    expression = "Date > #1/1/00#"
    Dim foundRows() As DataRow

    ' Use the Select method to find all rows matching the filter.
    foundRows = table.Select(expression)

    Dim i As Integer
    ' Print column 0 of each returned row.
    For i = 0 to foundRows.GetUpperBound(0)
       Console.WriteLine(foundRows(i)(0))
    Next i
End Sub
C#
private void GetRowsByFilter()
{
    DataTable table = DataSet1.Tables["Orders"];
    // Presuming the DataTable has a column named Date.
    string expression;
    expression = "Date > #1/1/00#";
    DataRow[] foundRows;

    // Use the Select method to find all rows matching the filter.
    foundRows = table.Select(expression);

    // Print column 0 of each returned row.
    for(int i = 0; i < foundRows.Length; i ++)
    {
        Console.WriteLine(foundRows[i][0]);
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Be Wary of Injection      jrg   |   Edit   |   Show History

In a similar manner to an SQL Injection Attack it is possible for the filter parameter of Select() to cause code to malfunction.

In the following example, DataTable "failureTable" is a subset of DataTable "populationTable". It is expected that one will find all values of the subset in the superset however this is not the case if the Select statement fails to select properly:


// Iterate through failureTable and create rows of percentsTable
foreach (DataRow failureRow in failureTable.Rows)
{
String name = (String)failureRow["Name"];
String value = (String)failureRow["Value"];
DataRow[] populationRows = populationTable.Select(String.Format("Name = '{0}' AND Value = '{1}'", name, value));
DataRow populationRow = populationRows[0]; // population is ALWAYS the superset, this should ALWAYS work

Suppose value = "Someone''s Creating A Problem" (double single quotes in Someone's) as retrieved from failureRow.

The Select statement will succeed because it is valid but the code may now fail on the last line with index out of range.

Tags What's this?: Add a tag
Flag as ContentBug
Stack Overflow      ParaMoxie ... Stanley Roark   |   Edit   |   Show History

- Stack Overflow can be caused by having combination of:

1) too many Rows in the DataTable

2) too many OR/AND expressions in the filter string (also see

http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3033750&SiteID=1 )

3) too long colomn names.

- I tried catching the exception and for some reason it could not be caught. Proabbly because this is an infinite loop/recurssion type of exception and process is not around to even throw the exception.

- The definition of "too long" and "too many" is not clear.

- Following program can be used to cause stack overflow exception. Set different values for the factors in the program and test to see results:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Data; 
namespace Datatable Tester 
{
class Program 
{
static void Main(string[] args) 
{
DataTable dt = newDataTable("test"); 
string filter = ""; 
/* following factors could cause the overflow */ 
string cname = "col"; 
int colmax = 50; 
int rowsmax = 50; 
int linesmax = 40; 
int factor = 20; 
for (int col = 1 ;col <= colmax ;col ++) 
dt.Columns.Add(cname + col.ToString());
for ( int rows = 0 ;rows< rowsmax; rows++) 
dt.Rows.Add(123411, 234122, 123423, 12341244, 1234, 13412346, 123417, 12341, 123419);
  
for(int num = 0; num < linesmax; num++) 
for (int i =1 ; i <= colmax; i++) 
{
filter = filter + 
" " + cname + i.ToString() + " = " + (i * factor).ToString() + " OR "; 
}
filter = filter + 
" " + cname + "1 = null"; 
/* Even though the exception is handled, its not getting caught */ 
try {
DataRow[] dr = dt.Select(filter); 
}
catch (Exception se) {
Console.WriteLine(se.Message); 
}
}
}
}
Tags What's this?: stack (x) Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker