Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.30/5 (4 votes)
See more:
Am having datatable full of values fetched from database...(the fields are SID,SNAME)

from that datatable i need to select only few rows...for example i need to select the rows containing SID in(101,102,109)..

I was advised to use IN Keyword like Sql to fetch those data from datatable...


I duno how?help me
Posted
Updated 21-Nov-18 5:40am

How to use IN[^]

And you might want to think about showing some appreciation to those that have answered your questions.
 
Share this answer
 
v2
Comments
VJ Reddy 30-Apr-12 7:42am    
Good reference. 5!
[no name] 30-Apr-12 7:46am    
Thanks
idhris2011 30-Apr-12 7:47am    
i need to use IN keyword to fetch data from datatable in Csharp, not sql
[no name] 30-Apr-12 7:52am    
The "in" in C# goes with a foreach loop.
VJ Reddy 30-Apr-12 7:57am    
You're correct. But IN inside the Filter expression has got a meaning similar to that of SQL IN as explained here http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
The reference for IN given by Wes Aday is good. But, since it was asked in the question to fetch rows from a DataTable, the Select method of DataTable can be used as follows:
SQL
DataRow[] rows = DataTable1.Select(
                    "SID IN (101,102,109)", "", 
System.Data.DataViewRowState.CurrentRows);

The filter expressions which can be used in the Select method are explained here
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx[^]
If the rows returned are required to be sorted, then second parameter of the Select method can be used as explained here
http://msdn.microsoft.com/en-us/library/b5c0xc84.aspx[^]
The System.Data.DataViewRowState.CurrentRows enumeration value is used as the third parameter in the above Select method to avoid returning the rows deleted in the DataTable using Row.Delete method.
 
Share this answer
 
Comments
[no name] 30-Apr-12 8:17am    
Nice
VJ Reddy 30-Apr-12 8:26am    
Thank you, Wes.
Hi ,
try this
C#
protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=test;Integrated Security=True");
        string statment = "select item_code, Item_name, brand, size, section, price, Material, Qty, tax, tt  from Items ";
        SqlDataAdapter da = new SqlDataAdapter(statment, con);       
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataRow[] foundRows = ds.Tables[0].Select("item_code = 1", " item_code DESC", DataViewRowState.CurrentRows);
        //this is for create datatable with same schema like the orginal one .
        string statment2 = "select item_code, Item_name, brand, size, section, price, Material, Qty, tax, tt  from Items where  item_code = 0";
        SqlDataAdapter da2 = new SqlDataAdapter(statment2, con);
        DataTable dt = new DataTable();
 
        da2.Fill(dt);
        DataRow dr = dt.NewRow();   
        foreach (DataRow row in foundRows)
        {            
            dr[0] = row[0];
            dr[1] = row[1];
            dr[2] = row[2];
            dr[3] = row[3];
            dr[4] = row[4];
            dr[5] = row[5];
            dt.Rows.Add(dr);
        }
        con.Close();
        cmd.Dispose();
        GridView1.DataSource = dt; 
        GridView1.DataBind();
    }


Best Regards
M.Mitwalli
 
Share this answer
 
DataRow[] rows= dt.Select("Columnname IN (PID's)");

dt=DataTable Object name
 
Share this answer
 
Comments
idhris2011 5-May-12 7:29am    
hey thnz man, correct soln
I'm not sure what exactly you mean. I have an idea that may help you though.
You can try:
SQL
select * from [name of your table] where sid=101 or sid=102 or sid=109;

If you have a DataTable object you can access the data with the following statement:
C#
string theData = [name of your object].Rows[Y].ItemArray[X].ToString();

Y is the row index (first row is 0) and x the column index.
 
Share this answer
 
Comments
idhris2011 30-Apr-12 7:48am    
its like using IN keyword in LINQ..

am altready having datatable, from that datatable i need tofetch some rows using IN keyword
Hi,
Do it like this...
Select * from [TableName] where SID IN(101,102,109);
 
Share this answer
 
string sql = "SELECT SID, SNAME from Your Table";
            DataRow dtrownew;
            int i=0;
            SqlConnection sqlcon = new SqlConnection("Data Source=server;Initial Catalog=database;uid=user;pwd=password");
            sqlcon.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(sql, sqlcon);
            DataSet dataSet = new DataSet("DS");
            adapter.Fill(dataSet, "TBL");
            //create new table with the original structure
            DataTable dttable = dataSet.Tables["TBL"].Clone();
            //loop though each row
            foreach (DataRow dtrow in dataSet.Tables["TBL"].Select("SID IN (101,102,109)"))
            {
                dtrownew = dttable.NewRow();
                //copy each field value to the new table
                while (i < dtrow.ItemArray.Length)
                {
                    dtrownew[i] = dtrow[i];
                    i++;
                }
                dttable.Rows.Add(dtrownew);
                i = 0;
            }
            gvCustomers.DataSource = dttable;
            gvCustomers.DataBind();
 
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