Onur Can
Onur Can

Reputation: 35

System.InvalidOperationException while checking null value

I'm trying to check null value of a cell. I'm getting this error hardly so I need some help about how to test this. Here's my code:

private void installBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    File.WriteAllText("program_names.txt", String.Empty); // Clear program_names file
    int num = 0;
    foreach (DataGridViewRow row in programs_dgv.Rows)
    {
        DataGridViewCheckBoxCell cell = row.Cells[1] as DataGridViewCheckBoxCell;

        if (cell.Value != null)
        {
          ...

How can I test and fix this?

Upvotes: 0

Views: 1120

Answers (1)

Skyuppercut
Skyuppercut

Reputation: 332

Please check for cell != null && cell.HasValue instead of cell.Value != null

Upvotes: 1

Related Questions