Solved

C# - Having issue with List<>

Posted on 2012-03-23
4
296 Views
Last Modified: 2012-03-23
I have a WCF Service which returns a list of files.  Below is the code for it.

[OperationContract]
List<APICustomerFiles> APIGetCustomerFiles(string AccessKey, bool IsDocument);

[DataContract]
public class APICustomerFiles
{
    [DataMember]
    public string FileName { get; set; }

    [DataMember]
    public Int64 FileSize { get; set; }
}

public List<APICustomerFiles> APIGetCustomerFiles(string AccessKey, bool IsDocument)
{
    using (SqlConnection connection = new SqlConnection(mdrConnection))
    {
        connection.Open();
        SqlCommand cmd = connection.CreateCommand();
        cmd.CommandText = "uspAPIGetCustomerFiles";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@AccessKey", AccessKey));
        cmd.Parameters.Add(new SqlParameter("@FileType", Convert.ToInt32(IsDocument)));
        using (SqlDataReader rdr = cmd.ExecuteReader())
        {
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                     _CustomerFiles = new List<APICustomerFiles>();
          _CustomerFilesRecord = new APICustomerFiles();
          _CustomerFilesRecord.FileName = rdr.GetValue(1).ToString();
          _CustomerFilesRecord.FileSize = Convert.ToInt64(rdr.GetValue(4).ToString());
          _CustomerFiles.Add(_CustomerFilesRecord);
                }
          }
     }
    return _CustomerFiles;
}

Below is the code to get the files

    //FILES
    public class FileList
    {
        static FileList()
        {
            ITransferService _getFiles = new TransferServiceClient();

            getFiles = new List<Files>();
            foreach (var files in _getFiles.APICustomerFiles("1234567", false))
            {
                getFiles.Add(new Files() { FileName = files.FileName });
            }

        }
        public static List<Files> getFiles { get; set; }
    }

    public class Files
    {
        public string FileName { get; set; }
    }

The problem is the SPROC returns 4 files but the for loop only processes the last one.

How do I resolve?
0
Comment
Question by:CipherIS
4 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 37758371
while (rdr.Read())
                {
                     _CustomerFiles = new List<APICustomerFiles>();

EAch time through the loop you trash the existing list and replace it witha new one.
I think this is what you require

                     _CustomerFiles = new List<APICustomerFiles>();
          while (rdr.Read())
                {
0
 
LVL 7

Expert Comment

by:Slimfinger
ID: 37758384
Things to consider:

a) Does "APIGetCustomerFiles" return the proper list?
b) Not generally good practice to do this:

      foreach (var files in _getFiles.APICustomerFiles("1234567", false))

...as, depending on the compiler, it may execute at each iteration, and in the best case is slow and ugly in general.  Instead, do this:

     List<APICustomerFiles> list_Files =  _getFiles.APICustomerFiles("1234567", false));
     foreach (APICustomerFiles file in list_Files)
0
 
LVL 1

Author Closing Comment

by:CipherIS
ID: 37758417
Thank You
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37758426
do this:-

using (SqlDataReader rdr = cmd.ExecuteReader())
        {
            if (rdr.HasRows)
            {
 _CustomerFiles = new List<APICustomerFiles>();
                while (rdr.Read())
                {
          _CustomerFilesRecord = new APICustomerFiles();
          _CustomerFilesRecord.FileName = rdr.GetValue(1).ToString();
          _CustomerFilesRecord.FileSize = Convert.ToInt64(rdr.GetValue(4).ToString());
          _CustomerFiles.Add(_CustomerFilesRecord);
                }
          }
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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