请帮我。它一直样3天了,我对抗这件事情,我得到彻底疯了。
我在此代码了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();
我试过这么多的东西,我的代码就像是一个战场了。
谢谢你的帮助。
编辑:我只是愚蠢。我只有在我如果与“data.Tables [0] .Rows.Count!= 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 )
{
}
检查此条件到位,如果你的块
if(data.Tables.Count > 0 & data.Tables[0].Rows.Count > 0)
{ //your code
}
让我知道,如果它解决您的问题。也有着异常。