Search code examples
c#winformscsvformatexception

C# Lumenworks Csvreader- Display Error in Message Box while reading a csv file with some Empty/Null fields


I have this code to read a csv file and store the fields in the objects_Records list of 'Records' class.

private String text2,text3,text4,text5,text6;
    private double text7,text8,text9;
    private int text1;
    private int count = 0;
    private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.Filter = "CSV files (*.csv)|*.csv";  // Show only .csv files among all the different files        
        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
        if (result == DialogResult.OK) // Test result.
        {
            String file = openFileDialog1.FileName;
            try
            {
                textBoxFilePath.Text = file;
                using (CsvReader csv = new CsvReader(new StreamReader(file), true))
                {
                    int fieldCount = csv.FieldCount;
                    string[] headers = csv.GetFieldHeaders();
                    while (csv.ReadNextRecord())
                    {
                        count += 1;
                            for (int i = 0; i < fieldCount; i++)
                            {                         
                                switch (headers[i].ToLower())
                                {
                                    case "plot":
                                        text1 = int.Parse(csv[i]);
                                        break;
                                    case "local name":
                                        text2 = csv[i];
                                        break;
                                    case "botanical name":
                                        text3 = csv[i];
                                        break;
                                    case "genera":
                                        text4 = csv[i];
                                        break;
                                    case "species":
                                        text5 = csv[i];
                                        break;
                                    case "family":
                                        text6 = csv[i];
                                        break;
                                    case "dbh":
                                        text7 = Double.Parse(csv[i]);
                                        break;
                                    case "ba(sqm)":
                                        text8 = Double.Parse(csv[i]);
                                        break;
                                    case "ba":
                                        text8 = Double.Parse(csv[i]);
                                        break;
                                    case "height":
                                        text9 = Double.Parse(csv[i]);
                                        break;
                                    default:
                                        MessageBox.Show("Please check the column headers of the fields once!");
                                        continue;
                                    }
                            }
                            object_Records.Add(new Records(text1, text2, text3, text4, text5, text6, text7, text8, text9));
                        }
                    }
                    textBox1.Text = count.ToString(); 
            }
            catch (IOException)
            {
            }
        }
    }

My code is running fine for the csv files where there are no 'NULL/EMPTY' fields. It is throwing an exception when encountering csv files that contain "NULL" fields.

A first chance exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.

Please do help me out with the exception handling mechanism that can handle that exception.


Solution

  • As suggested in the comments, have you tried this for the switch statement:

    switch (headers[i].ToLower())
    {
        case "plot":
            int.TryParse(csv[i], out text1);
            break;
        case "local name":
            text2 = csv[i];
            break;
        case "botanical name":
            text3 = csv[i];
            break;
        case "genera":
            text4 = csv[i];
            break;
        case "species":
            text5 = csv[i];
            break;
        case "family":
            text6 = csv[i];
            break;
        case "dbh":
            Double.TryParse(csv[i], out text7);
            break;
        case "ba(sqm)":
            Double.TryParse(csv[i], text8);
            break;
        case "ba":
            Double.TryParse(csv[i], out text8);
            break;
        case "height":
            Double.TryParse(csv[i], out text9);
            break;
        default:
            MessageBox.Show("Please check the column headers of the fields once!");
            continue;
    }
    

    If you tried it and it doesn't work, could you drill down on the exception and see where exactly it is being thrown ?