By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,275 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,275 IT Pros & Developers. It's quick & easy.

How to loop through DataRow[] to get the column name and value

P: 11
Hi
I have a DataTable and get selected values out of it in a DataRow collection. I have the code that I want to make it run. Not sure if this is the correct approach to store column names and value in memory for later use?
Expand|Select|Wrap|Line Numbers
  1. DataTable dt = ds.Tables["Employee"];
  2. DataRow[] foundEmpRows = dt.Select("Dept = 10");
  3.  
  4. NameValueCollection nvEmp = new NameValueCollection();
  5. foreach (DataRow dr in foundEmpRows)
  6. {
  7.   foreach (DataColumn dc in foundEmpRows)
  8.   {
  9.     nvEmp.Add(dc.ColumnName, dr[dc]);
  10.   }
  11. }                            
  12.  
The statement in BOLD is giving error. How to access DataColumn from DataRow[] array?
Sep 24 '10 #1
Share this Question
Share on Google+
1 Reply


P: 11
I was able to resolve it myself :)
I updated the inner loop to:
Expand|Select|Wrap|Line Numbers
  1. foreach (DataColumn dc in dr.Table.Columns) 
  2.   nvEmp.Add(dc.ColumnName.ToString(), dr[dc].ToString()); 
  3. }
  4.  
Sep 24 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.