Solved

C# - Having issue with List<>

Posted on 2012-03-23
4
303 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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

687 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