请帮帮我。好像三天前,我正在与这件事作斗争,我变得完全疯了。
我在这段代码中得到了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)为什么以前没有用。我猜是编程逻辑
答案 0 :(得分:0)
检查这种情况以代替if阻止
if(data.Tables.Count > 0 & data.Tables[0].Rows.Count > 0)
{ //your code
}
如果它可以解决您的问题,请告诉我。也分享例外。
答案 1 :(得分:0)
尝试首先检查它是否为空值,因为如果为空,则将没有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 )
{
}