Rhyous

April 6, 2010

How to use DataTable.Select() when the DataColumn.ColumnName contains a space?

Filed under: C# (C-Sharp),Database — J. Abram barneck @ 9:49 am

I have a DataColumn that contains an space and I just couldn’t get the DataTable.Select(“Column Name=value) function to work.

So i found a solution on some other guys wordpress blog here.

The answer should be obvious to those who use SQL. In SQL to use a space, it often adds square brackets around the column names. [Column Name]. Yes, using square brackets is the solution.

String colName = "Column Name";
String Value = "some data";
DataTable.Select("[" + colName + "]='" + value + "'");
Advertisements

4 Comments »

  1. Thanks very much. Helped me.

    Comment by Gulab — April 29, 2010 @ 10:55 am | Reply

  2. can anyone help me how to fix this… i should get records because id is greater than 1

    Dim d As New DataSet2

    Dim table As DataTable = d.Tables(“ClientsPersonal”)

    ‘ Presuming the DataTable has a column named Date.
    Dim expression As String
    expression = “id > 1”
    Dim foundRows() As DataRow

    ‘ Use the Select method to find all rows matching the filter.
    foundRows = table.Select(expression)
    MsgBox(foundRows.Count)
    Dim i As Integer
    ‘ Print column 0 of each returned row.
    For i = 0 To foundRows.GetUpperBound(0)
    MsgBox(foundRows(i)(0))
    Next i

    Comment by ramz — June 3, 2010 @ 4:05 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: