?
Solved

C# - Having issue with List<>

Posted on 2012-03-23
4
Medium Priority
?
304 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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