Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

problem with iterating through a dataset.

Hi All,
Can anyone help?

I have class that I want to connect to an access databse.

///////////////////////////////////////////////////////////////////////////
    public class OldCustomer : ICustomer, IControllerCust
    {
        /// <summary> Component customer properties. </summary>
        private int ptyCustomerID = 0;
        private string ptyCustomer_Surname= "None";
        private string ptyCustomer_Forname = "None";

.........etc
////////////////////////////////////////////////////////////////////////////

Connecting to the database is fairly straight forward but I can't seem to find a way to iterate through the records and assign them to the properties i.e

/////////////////////////////////////////////////////////////////////////
        public string Customer_Surname
        {
            get
            {
                return ptyCustomer_Surname;
            }
            set
            {
                ptyCustomer_Surname = value;
            }
        }


        public string Customer_Forname
        {
            get
            {
                return ptyCustomer_Forname;
            }
            set
            {
                ptyCustomer_Forname = value;
            }
        }

//////////////////////////////////////////////////////////////////

What I want to do is have a MoveNext() method that will go to the next record in the dataset and assign the values to th above properties.  It seems easy enough on a form class, all you do is bind a text control to the column and use the position method to iterate through the dataset.

If anyone can help I would be really grateful.
Cheers guys.
Shaun.

shaun bethell
Monday, April 26, 2004

you need to loop through the DataRows of the Datatable. I assume your DataSet has only one DataTable (since you only did a single SELECT statement).

DataSet ds = new DataSet();
adapter.Fill(ds);

foreach(DataRow row in ds.Tables[0].Rows)
{
      name = row["Name"];
      dateOfBirth = row["BirthDate"];
}

etc..

.NET Developer
Tuesday, April 27, 2004

Thanks, that's spot on!!!

shaun bethell
Tuesday, April 27, 2004

*  Recent Topics

*  Fog Creek Home