Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ADO.NET ENTITY DATA MODEL 3 32
ASP.NET 5 Templates 2 68
User Authentication using Digital Certificate 2 27
Where can I ask about my ASP.NET MVC project? 6 23
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

20 Experts available now in Live!

Get 1:1 Help Now