Solved

C# For Each Loop

Posted on 2011-03-10
2
531 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net mvc5 3 38
Problem to open Excel file 15 43
Different Delete Messages 7 17
Iterate two lists and remove inactive 11 35
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now