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

x
?
Solved

ASP.NET C# loop through DataReader and fill List<MyList>

Posted on 2011-09-02
1
Medium Priority
?
1,370 Views
Last Modified: 2013-12-17
I am processing data from an Excel file using ADO and a DataReader.

How can I fill a list when looping through the rows?


    class MyList
    {
        public string Area { get; set; }
        public string Category { get; set; }
        public string Right { get; set; }
        public decimal Amount { get; set; }
        public string Notes { get; set; }
    }

List<MyList> list = new List<MyList>();

while (dbReader.Read())
                {
                    string area = dbReader.GetValue(columnArea).ToString();
                    string category = dbReader.GetValue(columnCategory).ToString();
                    string right = dbReader.GetValue(columnRight).ToString();
                    string amount = dbReader.GetValue(columnAmount).ToString();
                    string notes = dbReader.GetValue(columnNotes).ToString();
                    // fill the list with values
                }
0
Comment
Question by:conrad2010
1 Comment
 
LVL 6

Accepted Solution

by:
c1nmo earned 2000 total points
ID: 36476492
class MyList
    {
        public string Area { get; set; }
        public string Category { get; set; }
        public string Right { get; set; }
        public decimal Amount { get; set; }
        public string Notes { get; set; }
    }

List<MyList> list = new List<MyList>();
while (dbReader.Read())
                {
list.Add(new MyList()
{
                    Area = dbReader.GetValue(columnArea).ToString();
                    Category = dbReader.GetValue(columnCategory).ToString();
                    Right = dbReader.GetValue(columnRight).ToString();
                    Amount = dbReader.GetValue(columnAmount).ToString();
                    Notes = dbReader.GetValue(columnNotes).ToString();
                } );

                }
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Integration Management Part 2
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

963 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