Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Capture logon name 13 73
ASP.NET MVC 2 31
Reading the web config for a running service on Windows 10 16 41
.NET 2008 VB and C# 6 27
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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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