Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 18th, 2008, 06:28 AM
Registered User
 
Join Date: Jul 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Loop through 17 tables in RecordSet object

I have a recordset that contains 17 tables I want to loop through the recordset and dynamically populate the colums in the gridview control.
Here is my code snippet

 for (int i = 0; i < dsRpt.Tables[i].Rows.Count; i++)
        {
            //Populate the first Question Data
            tempProjectNameData = dsRpt.Tables[i].Rows[i].ItemArray[2].ToString();
            tempNumberData = dsRpt.Tables[i].Rows[i].ItemArray[3].ToString();

            row = table.NewRow();

            if (tempProjectNameData == dsRpt.Tables[i].Rows[i].ItemArray[2].ToString())
            {

                row["No"] = dsRpt.Tables[i].Rows[i].ItemArray[3].ToString();
                row["Question"] = dsRpt.Tables[i].Rows[i].ItemArray[4].ToString();
                row[dsRpt.Tables[i].Rows[i].ItemArray[1].ToString() + " " +
               dsRpt.Tables[i].Rows[i].ItemArray[2].ToString()] = dsRpt.Tables[i].Rows[i].ItemArray[5].ToString();

                table.Rows.Add(row);

            }

            tempProjectNameData = dsRpt.Tables[i].Rows[i].ItemArray[2].ToString();
            tempNumberData = dsRpt.Tables[i].Rows[i].ItemArray[3].ToString();
        }
I then assign the table object to view object and bind it to the gridview control. It works partially hence I need an assistance.

I have 17 Rows in each table and exactly same data structure.

The problem with the code above is that I get only three rows populated and the columns are populate with missing columns for each row.

A very nifty clean code to loop through this dataset object will do me good.

Thanks in advance.

 
Old July 18th, 2008, 06:53 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You are using i twice, once for the table, and then again for each row.

Much better to do this:

foreach(DataTable innerTable in dsRpt.Tables)
{
   foreach(DataRow row in innerTable.Rows)
   {
      DataRow newRow = table.NewRow();
      newRow["No"] = row[3].ToString();
      newRow["Question"] = row[4].ToString();
      newRow[row[1].ToString() + " " + row[2].ToString()] = row[5].ToString();
      table.Rows.Add(newRow);
   }
}

Also, you assign tempProjectNameData to the second item in the row, then test to see if it equals that same value, which will always be true. You never use tempNumberData.

Then at the end you assign the variables again, for no reason.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 17 - Sender Object not working? deanC4 BOOK: Beginning ASP.NET 2.0 and Databases 1 April 18th, 2007 01:25 PM
recordset not showing up on page loop pablohoney Classic ASP Databases 1 December 14th, 2005 06:43 PM
Querying Recordset with a Loop rabu Access VBA 10 December 14th, 2005 04:49 PM
Filter recordset in loop Freddyfred Access 1 February 15th, 2005 09:09 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.