Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.NullReferenceException错误Csharp_C#_Sql_.net_Nullpointerexception - Fatal编程技术网

C# System.NullReferenceException错误Csharp

C# System.NullReferenceException错误Csharp,c#,sql,.net,nullpointerexception,C#,Sql,.net,Nullpointerexception,获取错误: System.NullReferenceException 有关C#Windows窗体上的以下代码 try { foreach (DataGridViewRow row in dataGridView2.Rows) { if (row.Cells["txtAttendance_Status"].Value.ToString() == "Attended") { row.Cells["txtStatus"].Val

获取错误:

System.NullReferenceException

有关C#Windows窗体上的以下代码

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());
}
下表如下:


在运行此线路之前

row.Cells["txtAttendance_Status"].Value.ToString()
您必须检查row.Cells[“txtAttention\u Status”]的内容。值不为空

您正在对Null执行ToString()

编辑:代码

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;
}
在这段代码中,如果任何数据库列具有null值,并且您正试图将其转换为字符串,那么此时将抛出System.NullReferenceException

比如说,,
根据您的SQL表结果,对于“requestid”50,“txtAttention_Status”具有空值和“System.NullReferenceException”如果您试图转换此空值,则会发生错误。

包括您的完整代码,因为您正在从数据库插入
获取
。还包括您的完整错误,以了解有关此问题的更多信息。例如if(row.Cells[“txttainment\u Status”]。value?.ToString()=“Attended”)它可以用lambda和linq在一行中完成,但这样可读性更强
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());
            }
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());
        }