Monday, June 11, 2007

How to loop through a dataset

How to loop throught a dot net dataset
============================



private void printDataSet(DataSet myDataSet)
{
int i = 0;
//looping through a dataset
//you may specify the name of the table (Eg: Ds.Tables["myTableName"].Rows.Count
//instead of myDataSet.Tables[0].Rows.Count
for (i = 0; i < myDataSet.Tables[0].Rows.Count; i++)
{
//fetch 1st field value in a row
Console.WriteLine(myDataSet.Tables[0].Rows[i][0].ToString());
//fetch 2nd field value in a row. you may also use the alternate syntax as
//myDataSet.Tables[0].Rows[i]["myFieldName"].ToString()

Console.WriteLine(myDataSet.Tables[0].Rows[i][1].ToString());
}
}

No comments: