c# - 如何处理我的 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”。不知道为什么它以前不起作用。我猜的编程逻辑
解决方案
检查此条件以代替您的 if 块
if(data.Tables.Count > 0 & data.Tables[0].Rows.Count > 0)
{ //your code
}
让我知道,如果它解决了你的问题。也分享例外。
推荐阅读
- android - 第二个片段布局错误上的 setOnClickListener
- python-3.x - 使用 ZMQ 黑盒模式进行实时市场数据流的多个发布者
- html - ExtJS - 如何嵌套 Ext.Component
- c# - 跟踪为在 C#/Visual Studio 中进行调试而进行的更改
- html - 如何使我的 HTML 背景颜色到达页面的两侧?
- javascript - CSS Grid 响应式布局保持完整的行
- haskell - 在有序二叉树遍历期间避免列表连接
- apache-nifi - 在 Nifi 中运行 ExecuteScriptProcessor
- mongodb - 通过聚合其他集合来填充字段
- node.js - 无法在使用 Express 和 Typescript 的模块外部使用 import 语句,我对模块深感困惑