Saturday, October 16, 2010

How to use where condition in Dataset

Question: where condition is possible only with SQL?
Ans:  No, we can use where condition in .NET Datset also

The main requirement is  delete a row when row text is "Invalid" from a DropDownList
Sol:  i need to use where condition in  a dataset, is possible only by using Select() method of dataset

EX:
private Dataset GetstudentData()
{
        string query=//write your query as per your requirement
        sqldataadapter da=new sqldataadapter(query,con);
       Dataset ds=new Dataset()    
       da.fill(ds,"TableName");
       DataRow []row1 = ds.Tables[0].Select(("Text='Invalid'"));//Where condition goes here // i need to delete                      row which contains  invalid text
       ds.Tables[0].DefaultView.Delete(ds.Tables[0].Rows.IndexOf(row1[0]));
        return ds;
 }

No comments:

Post a Comment