Mark Watson
Mark Watson

Reputation: 31

Dataset to each element in the dataset

I have a dataset that pulls names from a sql database, the data is of childrens parents. I am trying to loop over each child to get the childs information to another part of the program, This is what I have come up with so far but its not working all I am getting is ROW 0

 foreach (DataRow dataRow in ds.Tables["IDs"].Rows)
              {
                  string fammemberID = (ds.Tables["IDs"].Rows[0].ItemArray.GetValue(0).ToString());
                  string firstnameF = (ds.Tables["IDs"].Rows[0].ItemArray.GetValue(1).ToString());
                  string lastnameF = (ds.Tables["IDs"].Rows[0].ItemArray.GetValue(2).ToString());

                  createFile(value, firstnameF, lastnameF, fammemberID);
              }

Thanks in advance

Upvotes: 3

Views: 13011

Answers (1)

Jay Riggs
Jay Riggs

Reputation: 53603

The problem is you're not accessing the data in the DataRows you're iterating though. Change your code to this:

foreach (DataRow dataRow in ds.Tables["IDs"].Rows) {  
    string fammemberID = dataRow[0].ToString(); 
    string firstnameF = dataRow[1].ToString(); 
    string lastnameF = dataRow[2].ToString();

    createFile(value, firstnameF, lastnameF, fammemberID);  
} 

or even:

foreach (DataRow dataRow in ds.Tables["IDs"].Rows) {  
    createFile(value, dataRow[1].ToString(), dataRow[2].ToString(), dataRow[0].ToString());  
} 

Upvotes: 3

Related Questions