【问题标题】:How to handle a NULL value in reader.GetByte(x)?如何处理 reader.GetByte(x) 中的 NULL 值?
【发布时间】:2015-04-01 23:02:48
【问题描述】:

我有以下代码块:

//Check to see if the Unique_ID is 10 or 11 and process accordingly
//10 = Both boxes, 11 = Rework only
if (reader.GetByte(16) == 10)
{
   int ES = Convert.ToInt32(reader["ESCALATED"]);
   if (ES == 0)
   {
      chkEscalated.Checked = false;
   }
   else
   {
      chkEscalated.Checked = true;
      chkEscalated.Visible = true;
      lblEscalated.Visible = true;
      chkRework.Visible = true;
      lblRework.Visible = true;
    }
}
else if (reader.GetByte(16) == 11)
{
}
else
{
}

我遇到的问题是,有时 reader.GetByte(16) 为 NULL。发生这种情况时,我会收到一个错误:

数据为空。不能对 Null 值调用此方法或属性。

我还是个新手,所以我确定我缺少一些明显的东西,但我就是找不到。

【问题讨论】:

  • 当你需要一个可能是 null 的值时,你总是做同样的事情;使用前检查是否为null
  • 如何在上面的代码中做到这一点?我可以使用 IsNullOrEmpty 方法吗?如果可以,语法是什么?我从来没有在这种类型的代码块中使用过它。
  • 您将它与空值进行比较,看看它是否实际上为空,就像您在任何地方处理任何空值一样,永远。或者,除此之外,只需询问 Google 您是否真的需要一种方法来确定阅读器中单元格的值是否为空。我知道你知道怎么做那个
  • 您的问题不在于您发布的代码。找出阅读器中的数据为空的原因。现在正如 anon 指出的那样,您确实希望执行一次 GetByte 调用,然后检查每个 if 语句中的值,否则您可能会解决您的直接问题,只是直接进入另一个问题。

标签: c# code-behind datareader


【解决方案1】:

使用IsDBNull方法:

//Check to see if the Unique_ID is 10 or 11 and process accordingly
//10 = Both boxes, 11 = Rework only
if (reader.IsDBNull(16))
{
   //Add here your code to handle null value
}
else
{
   //Use a switch to read the value only one time
   switch (reader.GetByte(16))
   {
     case 10:
       int ES = Convert.ToInt32(reader["ESCALATED"]);
       if (ES == 0)
       {
          chkEscalated.Checked = false;
       }
       else
       {
          chkEscalated.Checked = true;
          chkEscalated.Visible = true;
          lblEscalated.Visible = true;
          chkRework.Visible = true;
          lblRework.Visible = true;
        }
        break;

      case 11:
        break;

      default:
        break;
   }
}

【讨论】:

  • 是的,就是这个!我还建议用switch 替换reader.GetByte(16) 的重复测试。
【解决方案2】:

根据您发布的错误消息,似乎有时reader 未初始化。 reader 是否在某处填充或初始化?如果是,那是一个检查的好地方。错误消息只是意味着 reader 没有引用现有实例,因此它为 NULL,因此您不能在其上调用方法,即您不能在其中调用 GetByte()

【讨论】:

  • 读卡器不为空,该行某列的值没有值。
【解决方案3】:

如果你错过了第一个 if,你真的想读 2 遍吗? 也许这就是这里的问题。 像var a = reader.GetByte(16) 这样存储值并查找该值。 像这样:

//Check to see if the Unique_ID is 10 or 11 and process accordingly
//10 = Both boxes, 11 = Rework only 
var a = reader.GetByte(16);
if(a != null)
{
   if (a == 10)
   {
      int ES = Convert.ToInt32(reader["ESCALATED"]);
      if (ES == 0)
      {
         chkEscalated.Checked = false;
      }
      else
      {
         chkEscalated.Checked = true;
         chkEscalated.Visible = true;
         lblEscalated.Visible = true;
         chkRework.Visible = true;
         lblRework.Visible = true;
       }
   }
   else if (a == 11)
   {
   }
   else
   {
   }
}

【讨论】:

  • 这无助于防止他遇到的错误。另请注意,从阅读器中获取价值并不是需要优化的昂贵操作。
  • 如果他只在阅读器中存储一个值,他就不会得到错误。 (更新了答案)
  • 是的,他会的,因为他仍在尝试将空值转换为byte。他连一次都无法成功获得价值,更别说两次了。如果他执行您建议的代码行,出于同样的原因,他会得到同样的错误。
  • 当然。我当然明白。这是错误的,它不会阻止 OP 出现错误。它具有与 OP 代码完全相同的错误。
【解决方案4】:

你可以在if语句中添加if (reader.GetByte(16) != null && reader.GetByte(16) == 10) 这将检查 reader.GetByte(16) 是否为空

【讨论】:

    【解决方案5】:
    if(!String.IsNullOrEmpty(reader.GetByte(16).ToString()))
    {
    if (reader.GetByte(16) == 10)
    {
       int ES = Convert.ToInt32(reader["ESCALATED"]);
       if (ES == 0)
       {
          chkEscalated.Checked = false;
       }
       else
       {
          chkEscalated.Checked = true;
          chkEscalated.Visible = true;
          lblEscalated.Visible = true;
          chkRework.Visible = true;
          lblRework.Visible = true;
        }
    }
    else if (reader.GetByte(16) == 11)
    {
    }
    else
    {
    }
    }
    

    试试这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-16
      • 2011-10-12
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      相关资源
      最近更新 更多