473,498 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

11 New Member
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
1 47936
Sri13
11 New Member
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

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

Similar topics

1
2285
by: ibm_97 | last post by:
DB2 8.2 on AIX Hi, I'd like to change a column's name in a table which is part of replication. This column is identity column (generated always). 1. Since DB2 will drop and recreate the...
10
6535
by: Colleyville Alan | last post by:
I am trying to turn a short and fat (63 columns) table into one that is tall and skinny (7 columns). Basically, I am trying to create a "reverse crosstab" using a looping structure in VBA along...
4
1916
by: JIM.H. | last post by:
Hello, string colVal =((TextBox)e.Item.Cells.Controls).Text; this is how I reach column value in a grid: how can I reach with the column name? some value should be int, some date, how should I...
7
2555
by: RSH | last post by:
I need to use a form element to display a list of users. I would like to store their ID "behind the scenes" so when the user selects a name the Event passes their ID. I can't find anything...
2
1722
by: RSH | last post by:
Hi, i have a situation where I have an object (Company) that returns a dataset for the Employees Property which in turn I would like to bind to a DDL. My problem is that I want to be able to...
1
2808
by: orenl | last post by:
I have a DataRow (from DataTable) and i want to get data from a column name that might not exist. public static object getObject(DataTable dataTable, int index, string columnName) { return...
3
1914
by: Jeff | last post by:
In .net2 using VB (Visual Web), I have the following code that properly determines the column name from the column number (5 in this example) from a single row table. ColName =...
1
1351
by: bbawa1 | last post by:
Hi, I have a grid view I am trying to invisible the fifth column. The rows are now not showing any data in column but the column name is still showing. I want to make invisible column name...
1
2306
by: Big Moxy | last post by:
Problem URL - http://projects.zanalysts.com/ariviewer/display.asp?ID=11 I'm using Access as the "database". I get this error in the above URL: ADODB.Fields error '800a0cc1' Item cannot be...
0
2688
by: Sebastijan | last post by:
Hi I am interested in DbConnection.GetSchema method of ODP.NET oracle library. When I call (to get primary keys metadata) DataTable pkeys = DbConnection.GetSchema("PrimaryKeys"... foreach...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.