2017-05-15 39 views
-1

我收到例外不能在C#中找到表0 ExecuteDataSet

DataTable ProbRead = SQLData.ExecuteDataSet("SELECT description FROM chk_maintenance_probremark where subheaderid = " + lblrefno.Text.ToString() + " and Type='Problem'").Tables[0]; 

System.IndexOutOfRangeException:无法找到表0

+0

https://stackoverflow.com/help/how-to-ask – g4s8

+0

容易。你没有table [0]。也许你需要在''+ lblrefno.Text.ToString()+“'之前和之后的''',这取决于'subheaderid'的类型。这是不使用参数化查询的问题之一 – Pikoh

+0

@Pikoh Got Yaa ..感谢您的帮助。 –

回答

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 
} 
0

查询可读

//TODO: get rid of hardcoding, but paramterize the query 
//DONE: ToString() is redundant in lblrefno.Text 
string sql = 
    [email protected]"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] 
    ... 
}