1

I know that sounds really weird, but I am fairly new to C# and I am hoping you guys can help me out.

I am looping thru a DataTable, then excute a qry and get the the result into a DataTable.

Works OK for one record, but as soon as there are more records, only the last record is in the DT, which makes sense, I just do not know how to fix it. I need to have all the records in the DT.

Here is my code, any suggestions are welcome....

    DataTable dt = ml.getRegistration();
    DataTable dt2 = new DataTable();

    foreach (DataRow row in dt.Rows)
    {     
        dt2 = ml.getRegistrationExport(row["ID"]);

    }
    GridView1.DataSource = dt2;
    GridView1.DataBind();
4
  • 2
    What does getRegistrationExport do? Add the code for it. Commented Dec 16, 2009 at 21:50
  • Could you elaborate a little more on what you need to do with the data? Do you have control over the code for ml.GetRegistration()? Commented Dec 16, 2009 at 21:52
  • 1
    If it's "resolved" then vote up the right response and mark it as an answer. Commented Dec 16, 2009 at 22:14
  • Could someone please edit "thru" to say "through"? With help, we can eliminate that word from this horribly lazy culture! Commented Dec 16, 2009 at 22:27

2 Answers 2

3

If ml.getRegistrationExport is returning a datatable, then it overwrites the data as you loop through the first datatable.. You need to use dt2.merge to add all the data to the datatable. HTH.

DataTable dt = ml.getRegistration();
DataTable dt2 = new DataTable();

foreach (DataRow row in dt.Rows)
{

dt2.merge(ml.getRegistrationExport(row["ID"]));

}

GridView1.DataSource = dt2;
GridView1.DataBind();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you would not do justice to the super fast and helpful reply. I am speechless. But that's all I have right now, a big THANK YOU, it works great!! Steve
No problem, glad to be of service.
2

One of the problems I see is each row you are looping through you are replace dt2. So if you have 5 rows, you will replace dt2 5 times and which ever row happen to be last would rule dt2.

I would re think this.

Are what you are trying to do is add a row to dt2 for each row in the original dt?

DataTable dt = ml.getRegistration();
DataTable dt2 = new DataTable();

foreach (DataRow row in dt.Rows)
{
    DataRow newRow = dt2.NewRow();

    // What Does getRegistrationExport return?  
    // A row or series of rows or a new DT?  
    // Each would change solution
    newRow["SomeColumn"] = ml.getRegistrationExport(row["ID"]);

    dt2.Rows.Add(newRow);
}

GridView1.DataSource = dt2;
GridView1.DataBind();

1 Comment

I doubt it is the row, because you wouldn't be able to assign it to dt2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.