C# 如何处理reader.GetByte(x)中的空值?
我有以下代码块:C# 如何处理reader.GetByte(x)中的空值?,c#,code-behind,datareader,C#,Code Behind,Datareader,我有以下代码块: //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
//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)为空。当这种情况发生时,我会得到一个错误:
数据为空。无法对空值调用此方法或属性
我还是个新手,所以我肯定有一些明显的东西我遗漏了,但我就是找不到。如果你遗漏了第一个if,你真的想读两遍吗? 也许这就是问题所在。 存储值,如
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
{
}
}
根据您发布的错误消息,有时
读卡器
似乎未初始化。读卡器是否正在某个地方填充或初始化?如果是的话,那是一个检查的好地方。错误消息仅仅意味着reader
没有引用一个现有实例,因此它是空的,因此您不能对它调用一个方法,也就是说,您不能在其中调用GetByte()
您可以在if语句中添加if(reader.GetByte(16)!=NULL&&reader.GetByte(16)==10)
这将检查reader.GetByte(16)是否为空
试试这个。使用方法:
当您需要一个可能为null
的值时,请执行与以前相同的操作;在使用它之前,请检查它是否为null
。在上面的代码中我该如何做?我可以使用IsNullOrEmpty方法吗?如果可以,语法是什么?我从来没有在这种类型的代码块中使用过它。你将它与空值进行比较,看看它是否实际上是空的,就像你在任何地方使用任何空值一样。或者,除此之外,只需询问谷歌是否真的需要一种方法来确定读卡器中单元格的值是否为空。我知道你知道怎么做。你的问题不在于你发布的代码。找出读取器中数据为空的原因。现在,正如anon指出的,您确实希望执行一次GetByte调用,然后检查每个if语句中的值,否则您可能会修复直接问题,只需直接进入另一个语句。读取器不是空的,该行中某列的值没有值。这无助于防止他出现错误。还要注意的是,从读卡器中获取值并不是一个需要优化的昂贵操作。如果他只在读卡器中存储一个值,就不会得到错误。(更新了答案)是的,他会的,因为他仍在尝试将空值转换为字节。他甚至不能成功地获得一次价值,更不用说两次了。如果他执行了你建议的代码行,出于同样的原因,他会得到同样的错误。当然。我完全理解。这是错误的,它无法阻止OP所犯的错误。它和OP的代码有着完全相同的bug。是的,就是那个!我还建议用开关
替换reader.GetByte(16)
的重复测试。
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
{
}
}
//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;
}
}