Friday, February 27, 2009

Filter table at runtime from dataset and store it in datatable in c#

In this article I am filtering data from dataset and store it in different different table.

//connect to sqlcon = new SqlConnection(”Data Source=firoz\\sqlexpress;Initial Catalog=pubs;Integrated Security=SSPI”);
//Here I have filled up dataset with two table, in first table all records are comming and in second table only unique id are coming from database.

String query = “select * from student;select distinct(id) from student”;
da = new SqlDataAdapter(query, con);da.Fill(ds);
//here I filled up different different table with unique id.
int count = ds.Tables[1].Rows.Count-1;
for (int i = 0; i <= count; i++){DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = “id=” + ds.Tables[1].Rows[i][0];
DataTable dt = new DataTable();dt=dv.ToTable(i.ToString());ds.Tables.Add(dt);}

No comments: