24

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);
     }
 }
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Peter
  • 351
  • 1
  • 5
  • 14

5 Answers5

24

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
bash.d
  • 13,029
  • 3
  • 29
  • 42
14
DateTime TaskStart = DateTime.Parse(dr["TaskStart"].ToString());
Denys Denysenko
  • 7,598
  • 1
  • 20
  • 30
  • Hi Thanks for the reply, now its just showing the last date on the last row for all records? – Peter Mar 06 '13 at 16:07
  • It looks like you have issue with your rpt.SetParameterValue("TaskStartDate", TaskStart); because you setting it at each lap but you use it elswhere outside of it. Also your formatted date doesn't go anywhere as well. – Denys Denysenko Mar 06 '13 at 16:09
8
foreach (DataRow dr in ds.Tables[0].Rows)
{
    //your code here
}
Alexey Malev
  • 6,408
  • 4
  • 34
  • 52
Chưa biết
  • 919
  • 8
  • 6
7
foreach (DataTable table in ds.Tables)
{
    foreach (DataRow dr in table.Rows)
    {
        var ParentId=dr["ParentId"].ToString();
    }
}
famousgarkin
  • 13,687
  • 5
  • 58
  • 74
Vaishakh
  • 71
  • 1
  • 1
0
        foreach (DataRow table in ds.Tables[0].Rows)
        {
                int ForType = Convert.ToInt32(table["Fortype"].ToString());
                ds.Tables[0].DefaultView.RowFilter = "ForType ='" + ForType +"'";
                rbtnQuestion.DataSource = ds.Tables[0].DefaultView;
                rbtnQuestion.DataBind();

        }
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 04 '23 at 20:21