Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a web application to print a certificate.i have opened a csv file and read the file in a datagridview list.then it prints the contents in the datagridview list.everything runs fine.bt the problem is tat some csv files are not working.when i tried to open a csv file it shows "caught ArgumentOutOfRange Exception,index was out of range.must be non-negative less than the size of the collection."
what should i do?pls help me.......

my code is this:

C#
private void openFileDialogOpenCSV_FileOk(object sender, CancelEventArgs e)
    {
        if (openFileDialogOpenCSV.CheckFileExists)
        {
            objStudents = new students();
            objSelectedStudents = new students();
            try
            {
                CsvFileReader objCsvFileReader = new CsvFileReader(openFileDialogOpenCSV.OpenFile());
                CsvRow objCsvRows = new CsvRow();
                while (objCsvFileReader.ReadRow(objCsvRows))
                {
                    student objStudent = new student(true, objCsvRows[0], objCsvRows[1], objCsvRows[2], objCsvRows[3], objCsvRows[4], objCsvRows[5]);
                    objStudents.Add(objStudent);
                    objSelectedStudents.Add(objStudent);
                }
                objCsvFileReader.Dispose();
                objCsvFileReader.Close();
                dataGridViewList.Rows.Clear();
                if ((objStudents != null) && (objStudents.Count > 0))
                {
                    panelList.BringToFront();
                    updateGridView();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
Posted
Updated 17-May-12 23:51pm
v3
Comments
Zoltán Zörgő 18-May-12 2:28am    
Ok, now that we have some code, which statement throws the exception? What is the exception text exactly? What is the input file - especially how a good row looks like, and how the row throwing the exception?
sjelen 18-May-12 5:57am    
In your code you expect that each row have 6 columns. If one of your CSV files has less than 6 columns you get that error. Depending on CsvFileReader maybe if even a single line in a file has < 6 colums you get the same error.
John d. Bartels 20-May-12 12:01pm    
I Agree with Zoltan and sjelen; this could be causes by a lack of content in the CSV file, and could be caused by having only five comma-separated values on a line instead of six which is what the code is expecting. First, check to ensure the file has at least one line. Then check to ensure each line has the six values you are looking for. If not, break out of the logic and state why the break occurred. (i.e. The File is empty! or There were only five values in line x for the file ...)

1 solution

Debug!
You have some logical error in your code, or you have an assumption about the input file, that is not always true. Without code, the statement raising the exception, and the input file generating the error nobody will have a clue, what your problem is.
So, bebug!
 
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