Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Iam accessing datable with multiple columns using select method. But iam returning value as
{System.Data.DataRow[0]}

DataRow[] row = datatablename.Select("KEY = '"+ SKEY + "' AND COLUMN_NAME = '"+ item[0].tostring() + "'");

i need value in datarow[] and loop through it.

What is the problem with this code.

What I have tried:

C#
DataRow[] row = datatablename.Select("KEY = '"+ SKEY + "' AND COLUMN_NAME = '"+ item[0].tostring() + "'");
Posted
Updated 8-Jul-18 3:52am
v3
Comments
Bryian Tan 8-Jul-18 1:28am    
Did you verify if the skey and item[0] in the datatable?

1 solution

First, tostring() should be ToString(). Second, you should be using a lambda expression. The following code should work (may need some massaging) as long as you previously verified that the KEY and COLUMN_NAME columns exist in the dataset.

C#
var rows = datatable.Rows.Where(x => x.GetInt32("KEY") == SKEY && 
                                     x.GetString("COLUMN_NAME") == item[0].ToString());


You'll end up with an IEnumerable collection of DataRow items that meet the specified criteria, or a null result if no matches were found.

I personally don't like working directly with datatables, and instead prefer to move the datatable contents into a list of strongly typed objects because things are much more controllable that way. But that's just me.

Finally, try to avoid using SQL reserved words (such as KEY) for column names. It doen't really hurt anything, but it's bad practice.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900