0

Getting the error :

System.NullReferenceException

for the below code on a C# Windows form.

try
{
    foreach (DataGridViewRow row in dataGridView2.Rows)
    {
        if (row.Cells["txtAttendance_Status"].Value.ToString() == "Attended")
        {
            row.Cells["txtStatus"].Value = true;
        }
        else if (row.Cells["txtAttendance_Status"].Value.ToString() == "Not Attended")
        {
            row.Cells["txtStatus"].Value = false;
        }
        else
        {
            row.Cells["txtStatus"].Value = false;
        }
    }
}

catch (Exception ex)
{
    MessageBox.Show("Error", ex.ToString());
}

Below is the table :

enter image description here

LopDev
  • 823
  • 10
  • 26
Ruchi
  • 146
  • 1
  • 8

3 Answers3

1

Before running this line

row.Cells["txtAttendance_Status"].Value.ToString()

you have to check the content of row.Cells["txtAttendance_Status"].Value is not null.

You are performing a ToString() over a Null

EDIT: The code

switch (row.Cells["txtAttendance_Status"].Value)
{
    //only if its string with value Attended
    case string s when s.Equals("Attended", StringComparison.InvariantCultureIgnoreCase):
        row.Cells["txtStatus"].Value = true;
        break;
    default:
        //All other cases "Not Attended", null or whatever
        row.Cells["txtStatus"].Value = false;
}
Arys
  • 477
  • 3
  • 12
  • somthing like if (row.Cells["txtAttendance_Status"].Value?.ToString() == "Attended") – Ive Mar 05 '20 at 11:08
  • It could be done in a single line with lambda and linq but its more readable this way – Arys Mar 05 '20 at 12:04
0
try
            {
                foreach (DataGridViewRow row in dataGridView2.Rows)


                        if (Convert.ToString(row.Cells["txtAttendance_Status"].Value) == "Attended")
                        {
                            row.Cells["txtStatus"].Value = true;
                        }
                        else if (Convert.ToString(row.Cells["txtAttendance_Status"].Value) == "Not Attended")
                        {
                            row.Cells["txtStatus"].Value = false;
                        }
                        else
                        {
                            row.Cells["txtStatus"].Value = false;
                        }

            }

            catch (Exception ex)
            {
                MessageBox.Show("Error", ex.ToString());
            }
Vijay
  • 137
  • 13
0
try
        {
            foreach (DataGridViewRow row in dataGridView2.Rows)


                    if (row.Cells["txtAttendance_Status"].Value.ToString() == "Attended")
                    {
                        row.Cells["txtStatus"].Value = true;
                    }
                    else if (row.Cells["txtAttendance_Status"].Value.ToString() == "Not Attended")
                    {
                        row.Cells["txtStatus"].Value = false;
                    }
                    else
                    {
                        row.Cells["txtStatus"].Value = false;
                    }

        }

        catch (Exception ex)
        {
            MessageBox.Show("Error", ex.ToString());
        }

In this code, If any database column having null value and you are trying to convert it as string, That time System.NullReferenceException is thrown.

For Example, as per your SQL table result, for "requestid" 50 the "txtAttendance_Status" having a null value and the "System.NullReferenceException" error will occur if you try to convert this null value.1