[英]How to handle “Out Of Index” exceptions in my DataSet?
请帮我。 大概三天了,我正在与这个东西作斗争,我变得完全疯了。
我在这段代码中没有Out Of Index Exception
。 每次我按下一个按钮(一组4个按钮)时都使用该代码。
我的问题是每次尝试查看行是否为空时, if
上都会出现异常错误。
//I get my dataset from my WFC from this
data = client.GetEtapes(numTransformateur, IdEtape);
//If my dataset is empty : this is were everything blows up
if (DBNull.Value.Equals(data.Tables[0].Rows[0]))
{
LblDateDebutEtape.Text = "Date de début d'étape : ";
LblDateFinEtape.Text = "Date de fin d'étape : ";
LblDateDebutEtape.Text = LblDateDebutEtape.Text + " " + data.Tables[0].Rows[0][1].ToString();
LblDateFinEtape.Text = LblDateFinEtape.Text + " " + data.Tables[0].Rows[0][2].ToString();
LblDateDebutEtape.Visible = true;
LblDateFinEtape.Visible = true;
//I need to fetch another kind of data
set = client.GetSousEtapesWithCommentary(data.Tables[0].Rows[0][0].ToString());
//Same test as before
if (DBNull.Value.Equals(set.Tables[0].Rows[0]))
{
Dtg_Fichiers.DataSource = data.Tables[0];
Dtg_Fichiers.Columns[0].Visible = false;
Dtg_Fichiers.Columns[Dtg_Fichiers.ColumnCount - 2].Visible = false;
}
}
//In any case. Thoses does not affect the data I fetch at all.
this.GetButtonAllEnabled(button);
Dtg_Fichiers.ClearSelection();
我已经尝试了很多东西,我的代码现在就像一个战场。
谢谢你的帮助。
编辑:我只是愚蠢。 我所要做的就是在if中使用“ data.Tables [0] .Rows.Count!= 0”。 邓诺(Dunno)为什么以前没有用。 我猜编程逻辑
检查此条件以代替if块
if(data.Tables.Count > 0 & data.Tables[0].Rows.Count > 0)
{ //your code
}
让我知道,是否可以解决您的问题。 也分享例外。
尝试首先检查是否为空值,因为如果为空,则索引将不为0。
if(data.Tables != null && data.Tables.Count > 0 && data.Tables[0].Rows != null && data.Tables[0].Rows.Count > 0)
{
}
或者,您可以做的更小一些,但不检查计数。
if(data.Tables != null && data.Tables[0].Rows != null )
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.