For Answers, see/post comments

How to filter the datatable

We Often use to come across the situation where we use to filter the data with some condition, here i have taken a products table from northwind database and now i want to show all the products name which come under cateogory 1(Beverages)

SqlConnection myConn = new SqlConnection(strConString);

string strSql = "select * from Products";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strSql, myConn);
da.Fill(ds);
DataTable dt = ds.Tables[0];

// The following line filters the data by "CategoryId = 1" and returns an array of DataRowDataRow[] dRow = dt.Select("CategoryId=1");

for (int i = 0; i <>
{
Response.Write("Row " + dRow[i]["ProductName"].ToString());
}

No comments: