Solved

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

Posted on 2011-09-02
1
1,299 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 500 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

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

19 Experts available now in Live!

Get 1:1 Help Now