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

Peter picture Peter · Mar 6, 2013 · Viewed 182.3k times · Source

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);
     }
 }

Answer

bash.d picture bash.d · Mar 6, 2013

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.