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

5

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);
     }
 }
Autry answered 6/3, 2013 at 16:4 Comment(0)
L
14
DateTime TaskStart = DateTime.Parse(dr["TaskStart"].ToString());
Latif answered 6/3, 2013 at 16:6 Comment(2)
Hi Thanks for the reply, now its just showing the last date on the last row for all records?Autry
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.Latif
F
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.

Flocculus answered 6/3, 2013 at 16:6 Comment(0)
L
14
DateTime TaskStart = DateTime.Parse(dr["TaskStart"].ToString());
Latif answered 6/3, 2013 at 16:6 Comment(2)
Hi Thanks for the reply, now its just showing the last date on the last row for all records?Autry
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.Latif
M
9
foreach (DataRow dr in ds.Tables[0].Rows)
{
    //your code here
}
Monogenetic answered 18/5, 2014 at 12:14 Comment(0)
R
7
foreach (DataTable table in ds.Tables)
{
    foreach (DataRow dr in table.Rows)
    {
        var ParentId=dr["ParentId"].ToString();
    }
}
Ravi answered 1/7, 2014 at 8:21 Comment(0)
C
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();

        }
Caucasoid answered 2/2, 2023 at 11:34 Comment(1)
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.Zonnya

© 2022 - 2024 — McMap. All rights reserved.