首页 > 解决方案 > 如何处理我的 DataSet 中的“Out Of Index”异常?

问题描述

请帮我。现在已经有 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();

我已经尝试了很多东西,我的代码现在就像一个战场。

谢谢你的帮助。

编辑:我只是愚蠢。我所要做的就是在我的 if 中使用“data.Tables[0].Rows.Count != 0”。不知道为什么它以前不起作用。我猜的编程逻辑

标签: c#winformswcf

解决方案


检查此条件以代替您的 if 块

if(data.Tables.Count > 0 & data.Tables[0].Rows.Count > 0)
{ //your code
}

让我知道,如果它解决了你的问题。也分享例外。


推荐阅读