Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
List<EventFile> CollectionsOfEvtfiles = new List<EventFile>();

if (args.IsOffline)
{
    using (SqlConnection conn = new SqlConnection(Db.ConnectionString))
    {
        conn.Open();
        FileHelper.ImportFile(Entities, newEventFile, ImportMode.ManualMode, false, conn,IsCellCreated);
        if (newEventFile.RecordsCount!=0)
        {
            CollectionsOfEvtfiles.Add(newEventFile);
        }
    }
}

In my code import multiple files. It working fine. But when add the files in list, It add last file only in list. (Ex. I am importing 5 files, But finally it take last file only to add list)Please help me.. i want to add all files in list
Posted
Updated 19-Feb-15 20:56pm
v2
Comments
TheRealSteveJudge 20-Feb-15 2:43am    
Do you want to append a list of event files to an existing list of event files?
Tomas Takac 20-Feb-15 3:00am    
How do you import 5 files? Your sample only imports one file.
SaranshSrivastava 20-Feb-15 3:29am    
Use AddRange() method of List instead Add. Like below:
string[] input = { "Apple",
"Orange",
"Apricots" };

List<string> fruits= new List<string>();
Console.WriteLine("\nAddRange(fruits)");
fruits.AddRange(input);

Console.WriteLine();
foreach( string fruit in fruits)
{
Console.WriteLine(fruit);
}

Use AddRange() method of List instead Add. Like below:
C#
string[] input = { "Apple",
                           "Orange",
                           "Apricots" };

        List<string> fruits= new List<string>();
        Console.WriteLine("\nAddRange(fruits)");
        fruits.AddRange(input);

        Console.WriteLine();
        foreach( string fruit in fruits)
        {
            Console.WriteLine(fruit);
        }        
 
Share this answer
 
Hello Rnshanmugavadivel,

Seeing Your Code you are checking
C#
if (newEventFile.RecordsCount!=0)
        {
            CollectionsOfEvtfiles.Add(newEventFile);//Error Point
        }


Simply you are adding only One Time..
Either you run the loop
or ANotheir way Use AdddRange Method.
which Adds the elements of the specified collection to the end of the List.

See The example & Try It
C#
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
    List<int> a = new List<int>();
    a.Add(1);
    a.Add(2);
    a.Add(5);
    a.Add(6);

    // Contains:
    // 1
    // 2
    // 5
    // 6

    int[] b = new int[3];
    b[0] = 7;
    b[1] = 6;
    b[2] = 7;
     //adding Multiple Items
    a.AddRange(b);

    // Contains:
    // 1
    // 2
    // 5
    // 6
    // 7 [added]
    // 6 [added]
    // 7 [added]
    foreach (int i in a)
    {
        Console.WriteLine(i);
    }
    }
}


Hope It Helps You.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900