C#, Looping through dataset and show each record from a dataset column

cdatasetforeach

In C#, I'm trying to loop through my dataset to show data from each row from a specific column. I want the get each date under the column name "TaskStart" and display it on a report, but its just shows the date from the first row for all rows can anybody help?

 foreach (DataTable table in ds.Tables)
 {

     foreach (DataRow dr in table.Rows)
     {
         DateTime TaskStart = DateTime.Parse(
             ds.Tables[0].Rows[0]["TaskStart"].ToString());
         TaskStart.ToString("dd-MMMM-yyyy");
         rpt.SetParameterValue("TaskStartDate", TaskStart);
     }
 }

Best Answer

I believe you intended it more this way:

foreach (DataTable table in ds.Tables)
{
    foreach (DataRow dr in table.Rows)
    {
        DateTime TaskStart = DateTime.Parse(dr["TaskStart"].ToString());
        TaskStart.ToString("dd-MMMM-yyyy");
        rpt.SetParameterValue("TaskStartDate", TaskStart);
    }
}

You always accessed your first row in your dataset.

Related Topic