DataTable select method behaviour

A

Abhishek

Hello,
I was using Datagrid (Windows Forms) the datasource of which was
a DataTable.
If I try to modify or delete some rows from the Datagrid using the
underlying DataTable then
I use Select method of the DataTable to get the corresponding row from the
DataTable.
I use the code as mentioned below:
string strSelect = "TerminalNumber = " + iTermNo;
DataRows[] oRows = oTable.Select(strSelect);

This works fine till the stage when the DataTable has rows less than 10.
However when the
DataTable has rows more than 10 then the select method does not return any
rows even if
the TerminalNumber(if it is 2 digit eg. 10,11,12 etc.) is present in the
Grid/Table.

Is this a bug in Datagrid/DataTable?

It works fine if the code is changed as follows:
string strSelect = "TerminalNumber = '" + iTermNo + "'";
DataRows[] oRows = oTable.Select(strSelect);
i.e. enclosing the iTermNo within single quotes.

Thanks for your help,
Abhishek.
 
A

Abhishek

Yes, this is a keyed table and the Primary key is the same as that given in
the Query, i.e. the TerminalNumber.
However the problem exists. Is there any bug?

W.G. Ryan - MVP said:
Abhishek - before we go further, this is a keyed table correct?
Abhishek said:
Hello,
I was using Datagrid (Windows Forms) the datasource of which was
a DataTable.
If I try to modify or delete some rows from the Datagrid using the
underlying DataTable then
I use Select method of the DataTable to get the corresponding row from the
DataTable.
I use the code as mentioned below:
string strSelect = "TerminalNumber = " + iTermNo;
DataRows[] oRows = oTable.Select(strSelect);

This works fine till the stage when the DataTable has rows less than 10.
However when the
DataTable has rows more than 10 then the select method does not return any
rows even if
the TerminalNumber(if it is 2 digit eg. 10,11,12 etc.) is present in the
Grid/Table.

Is this a bug in Datagrid/DataTable?

It works fine if the code is changed as follows:
string strSelect = "TerminalNumber = '" + iTermNo + "'";
DataRows[] oRows = oTable.Select(strSelect);
i.e. enclosing the iTermNo within single quotes.

Thanks for your help,
Abhishek.
 
C

Cor Ligthert [MVP]

Abhishek,

It is strange, it seems that C# converts automaticly a integer to a string
in this case if it is less than 2 digits.

However the prefered method is to use parameters.

Have a look at this sample (it is VB however the only difference with C# in
this are the description of the methods, four declarations and that you have
to add in C# a semicolon at the end)

http://www.vb-tips.com/default.aspx?ID=2e452783-527a-402a-b37a-ae802ee71c86

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top