我的
异常DataTable ProbRead = SQLData.ExecuteDataSet("SELECT description FROM chk_maintenance_probremark where subheaderid = " + lblrefno.Text.ToString() + " and Type='Problem'").Tables[0];
System.IndexOutOfRangeException:找不到表0。
答案 0 :(得分:1)
似乎你的SQL查询没有结果 试试这个
var probRead = SQLData.ExecuteDataSet("SELECT description FROM chk_maintenance_probremark where subheaderid = " + lblrefno.Text.ToString() + " and Type='Problem'");
if(probRead.Tables.Count > 0)
{
var myTable = probRead.Tables[0];
// do some stuff
}
答案 1 :(得分:0)
使查询可读:
//TODO: get rid of hardcoding, but paramterize the query
//DONE: ToString() is redundant in lblrefno.Text
string sql =
$@"select Description
from Chk_Maintenance_ProbRemark
where SubHeaderId = '{lblrefno.Text}' and
Type = 'Problem'";
并且您会清楚地看到问题:lblrefno.Text.ToString()
应该包含'
。
不要忘记测试是否有任何数据返回:
var probRead = SQLData.ExecuteDataSet(sql);
if (probRead.Tables.Count > 0) {
// It's safe now to call probRead.Tables[0]
...
}