Solved

C# - Having issue with List<>

Posted on 2012-03-23
4
302 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
[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
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:Bill Nolan
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

737 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