Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys

I need help with this piece of code. The code ready multiple text file from a directory then add the contents of a file to an ArrayList. Somehow I can only read the first item at index zero on the array list. indexed 1 to n comes up with "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


.
Please see code below

C#
protected void GetFileValues()
        {
            
            int lineCounter = 0;
            string line = "";
              
            foreach (string files in Directory.GetFiles(Server.MapPath("DataFiles"), "*.whl"))
            {
                StreamReader sr = new StreamReader(files);
                ArrayList list = new ArrayList();
               
                while (((line = sr.ReadLine()) != null) && (lineCounter < 3))
                {
                    string[] token = line.Split('=');

                    if (String.IsNullOrEmpty(token[1]))
                    {
                        list.Add("0");
                    }
                    else
                    {
                        list.Add(token[1]);// add the value to the right of '=' into an arraylist
                    }
                 lineCounter++;
                  
               }//end while
               
                sr.Close();
                ListBox1.Items.Add((string)list[1]);
            }
        }
Posted
Updated 8-Apr-14 4:04am
v2

You are resetting the array list each time your loop iterates.
You need to move the line:
C#
ArrayList list = new ArrayList();
outside the foreach lop so it doesn't get reset.

Additional Response based on comment 1:

This line:
C#
ListBox1.Items.Add((string)list[1]);

This is written to only read element 1 from your list, as a result on the screen (i.e. in your listbox1) you will see the same entry repeated.

Now, I'm slightly unsure what it is exactly you are trying to achieve from your code.

By moving ArrayList to outside the outer for loop it will result in every entry in all of the files being stored in the list, from 0 -> X.

Where is the code you are using to write this to a database as this is not in the code example?
 
Share this answer
 
v2
Comments
Member 9528427 8-Apr-14 10:47am    
if I move ArrayList list = new ArrayList() above foreach loop, it inserts duplicates to a database with only one line
Pheonyx 8-Apr-14 11:05am    
See my updated solution based on your comment.
Also, what you have posted in "Solutions 2 and 3" are not Solutions, please remove them and either add them to your question or to your comment by using the "improve question" link which appears when you move your mouse over your question.
Member 9528427 8-Apr-14 11:13am    
Pheonyx

Don't worry about the line ListBox1.Items.Add(list[1]);

I was just using it to test;
here is the code that inserts to the database

SqlConnection conn = new SqlConnection(connStr);
string insertToDB = "";
try
{



conn.Open();

insertToDB = "INSERT INTO tblMiniprof ([SerialNo],[ProgramName],ProgramVer])" +

"VALUES @SerialNo,@ProgramName,@ProgramVer)";

SqlCommand cmd = new SqlCommand(insertToDB, conn);

cmd.Parameters.AddWithValue("@SerialNo", (string)list[2] + "-" + (string)list[3]);
cmd.Parameters.AddWithValue("@ProgramName", (string)list[0]);
cmd.Parameters.AddWithValue("@ProgramVer", (string)list[1]);

cmd.ExecuteNonQuery();

}
finally
{
conn.Close();
}
 
Share this answer
 
Comments
Pheonyx 8-Apr-14 11:26am    
I think you need to revisit your design with regards to what you are trying to do.

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