Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 559
  • Last Modified:

C# For Each Loop

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
dodgerfan
Asked:
dodgerfan
2 Solutions
 
Rajkumar GsSoftware EngineerCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now