Solved

C# For Each Loop

Posted on 2011-03-10
2
539 Views
Last Modified: 2012-05-11
I'm looking for a good example of a for each loop in C#/ASP.Net. I need to open a recordset from a database, then iterate over each record with the loop. I'm drawing a blank on something that should be simple.
0
Comment
Question by:dodgerfan
2 Comments
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 250 total points
ID: 35097977
This is a sample using DataTable
DataTable tbl = new DataTable();
foreach (DataRow row in tbl.Rows)
{
foreach (DataColumn col in tbl.Columns)
{
object cellData = row[col];
}
}

Open in new window

0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 total points
ID: 35098126
If you're reading from a DataReader (which is the quickest method)  then you don't need a for/each loop, you loop calling Read() on the DataReader until it returns false.

Maybe this will be of use:

     www.myvirtualplayground.co.uk/blog/Fundamentals_Reading_From_a_Database_Part_1
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question