Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-02
1
Medium Priority
?
1,358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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