Solved

C# - Having issue with List<>

Posted on 2012-03-23
4
300 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: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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

685 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