Resolved Program not catching exception

chairmanPC

Active member
Joined
Apr 19, 2021
Messages
38
Programming Experience
10+
So here's the problem: I am trying to copy and paste data from one DataGridView to another. First DGV is called allData and second DGV is called selectedDGV. There are 8 columns in allData while there are 5 in selectedDGV.

As long as the program reads data with more than 8 columns, it's fine. If there is less than 8 columns, then of course I get NullReferenceException.

Codes below

Open FIle code:
 private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //updateDataBtn.Enabled = true;
                selectedDGV.Rows.Clear();
                importExcelToDataGridViewApp = new Microsoft.Office.Interop.Excel.Application();

                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = "Browse for Excel File";
                ofd.Filter = "Excel and CSV Files|*.xlsx;*.csv";
              
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = ofd.FileName;
                    importExcelToDataGridViewWorkbook = importExcelToDataGridViewApp.Workbooks.Open(ofd.FileName);
                    importExcelToDataGridViewWorksheet = importExcelToDataGridViewWorkbook.ActiveSheet;
                    importExcelToDataGridViewRange = importExcelToDataGridViewWorksheet.UsedRange;


                    for (int rowIndex = 2; rowIndex <= importExcelToDataGridViewRange.Rows.Count; rowIndex++)
                    {
                        allData.Rows.Add(importExcelToDataGridViewWorksheet.Cells[rowIndex, 1].Value,
                                                importExcelToDataGridViewWorksheet.Cells[rowIndex, 2].Value,
                                                importExcelToDataGridViewWorksheet.Cells[rowIndex, 3].Value,
                                                 importExcelToDataGridViewWorksheet.Cells[rowIndex, 6].Value,
                                                importExcelToDataGridViewWorksheet.Cells[rowIndex, 7].Value,
                                                 importExcelToDataGridViewWorksheet.Cells[rowIndex, 8].Value,
                                                 importExcelToDataGridViewWorksheet.Cells[rowIndex, 9].Value,
                                                 importExcelToDataGridViewWorksheet.Cells[rowIndex, 10].Value);
                    }
                }
            }catch(Exception nani)
            {
                ErrorForm error = new ErrorForm();
                error.ShowDialog();
                error.label3.Text = nani.Message;
            }
        }

And this is the code where I am supposed to copy selected rows.

C#:
       private void btnAdd_Click(object sender, EventArgs e)
        {
          
                selectedDGV.Rows.Clear();
                for (int i = 0; i < allData.Rows.Count; i++)
                {
                try
                {
                    if (Convert.ToBoolean(allData.Rows[i].Cells[8].Value) == true)
                    {
                            int n = selectedDGV.Rows.Add();
                            selectedDGV.Rows[n].Cells[0].Value = allData.Rows[i].Cells[1].Value.ToString();
                            selectedDGV.Rows[n].Cells[1].Value = allData.Rows[i].Cells[3].Value.ToString();
                            selectedDGV.Rows[n].Cells[2].Value = allData.Rows[i].Cells[4].Value.ToString();
                            selectedDGV.Rows[n].Cells[3].Value = allData.Rows[i].Cells[5].Value.ToString();
                            selectedDGV.Rows[n].Cells[4].Value = allData.Rows[i].Cells[6].Value.ToString();
                            selectedDGV.Rows[n].Cells[5].Value = allData.Rows[i].Cells[7].Value.ToString();
                        }
                      
                }
                catch (Exception nani)
                {
                    ErrorForm error = new ErrorForm();
                    error.label3.Text = nani.Message;
                    error.ShowDialog();
                }
            }
              
            }

For some odd reason, the program would not catch any exceptions for btnAdd function. Did I surround the try/catch in the wrong place?
 
Last edited:
Say again? Why would you expect there to be a null reference exception when there are less than 8 rows? Also why a null reference exception instead of an out of range exception?

Your second Add button click handler will only iterate over the number of rows it has. See line 5. So if you have only 3 rows, it will only iterate 3 times.
 
Say again? Why would you expect there to be a null reference exception when there are less than 8 rows? Also why a null reference exception instead of an out of range exception?

Your second Add button click handler will only iterate over the number of rows it has. See line 5. So if you have only 3 rows, it will only iterate 3 times.
Sorry, I mean, 8 columns.
 
Line 23-30 of your first chunk of code always adds 8 columns.
 
Lets back all the way back up to the start so you can explain...

  • What do you want to do?
  • What is your expected result?
  • What is the actual result of your expected result?
I am asking this because it is unclear and you come in to the topic like we are meant to already know what your trying to do. Please remember, nobody here has a crystal ball. You need to explain what you want to do, what you tried, and what your expected result is.

As long as the program reads data with more than 8 columns, it's fine.
8 columns from which dgv?
On line 4 you are clearing your dgv of rows, and then trying to set values on cells in rows that don't exist.
 
On btnAdd_Click, put a debug point on line 9 and check the iteration cycle on value.
At any time is that value null?
You know you really could help us by telling us what is null, instead of making us read all your code to work out the riddle.
 
Ok so you are adding them rows on line 11... but your only adding 6 of them???

Ah here, I'm so confused right now. :LOL:

Come back in here and do some explaining please.
 
Okay... But you said that your code is not catching any exceptions. How did you manage to catch the exceptions now if you were not able to catch them before?

Or perhaps, there were no exceptions in the first place? There were just error conditions that you assumed would throw exceptions, perhaps?
 
Okay... But you said that your code is not catching any exceptions. How did you manage to catch the exceptions now if you were not able to catch them before?

Or perhaps, there were no exceptions in the first place? There were just error conditions that you assumed would throw exceptions, perhaps?
There WERE exceptions in the first place that were uncaught.

I found the cause: at the beginning, the program simply read all sorts of excel files, regardless of whether the values are valid, or if there are blank cells. I did not implement an Excel validation check.

So when I tried to copy and paste the cells, of course exceptions happen, because the cells are blank. The Excel file required has to meet certain requirements for the check to pass. So I had to write something that would check the validity of the excel file, whether or not they are in the right format. I also wrote a function that checks for cells that are empty. If any emty cells are found, then the program will show an error from my handmade error form, and then clear the dataGridView.

C#:
foreach (DataGridViewRow rw in allData.Rows)
                    {
                        for (int i = 0; i < rw.Cells.Count; i++)
                        {
                            if (rw.Cells[i].Value == null || rw.Cells[i].Value == DBNull.Value || String.IsNullOrWhiteSpace(rw.Cells[i].Value.ToString())) //Checks for empty cells.)
                            {
                                ErrorForm error = new ErrorForm();
                                error.label3.Text = "Some corrupted or invalid data has been found. Please choose another file.";
                                error.ShowDialog();
                                invalidInput = true;
                                allData.Rows.Clear();
                                break;
                            }
                            break;
                        }
                    }

The problem now, is that the program will scan through the entire file and give error, even if it means scanning an excel file with 10000+ blank cells. That happens for excel files downloaded from G-Drive. I was actually supposed to break the loop when an empty cell was found, but it did not break.
 
Last edited by a moderator:
There WERE exceptions in the first place that were uncaught.
Okay, if that is true, which line in your original code from post #1 was throwing the uncaught exception?
 
Okay, if that is true, which line in your original code from post #1 was throwing the uncaught exception?
C#:
          }catch(Exception eee)
           {
                ErrorForm error = new ErrorForm();
                error.label3.Text = eee.Message;
                error.ShowDialog();
            }
        } <--- Right here. It's NullReferenceException
 
Last edited by a moderator:
Have you considered that the exception maybe actually being thrown by your exception handling code?I

Also, that doesn't seem to match up with the original code you presented. Your original code used nani as the exception variable, but you are now showing us eee.
 
Also, have you considered if you have accidentally turned on the option to have the debugger stop on first chance exceptions? With that setting, you get to see the exception before the exception handler gets called.
 

Latest posts

Back
Top Bottom