-1

以下功能给了我一个错误:

FillFields("select Housemcode,Name, HP,Rateperhour ,Resource_H_Code FROM House_Machinery where Housemcode like '" + sSearch + "'");


public void FillFields(string sSQL)
    {
        sCommands.setSqldbCommand(sVariables.sDataSet, sVariables.sSqlDbDataAdapter, sSQL, "House_Machinery");
        DataRow sDataRow = sVariables.sDataSet.Tables["House_Machinery"].Rows[0];
        txtItemName.Text = sDataRow["Name"].ToString();
        txtrate.Text = sDataRow["HP"].ToString();
        txtrate.Text = sDataRow["Rateperhour"].ToString();
        Variables.StrResourceHeaderCode = sDataRow["Resource_H_Code"].ToString();

    }

错误是:位置 0 没有行。

任何人都可以对此有所了解吗?

4

2 回答 2

1

您的查询根本没有返回任何行。尝试直接在 SQL Management Studio 中运行 SQL 查询以确认返回数据。

顺便说一句,您可以通过计算返回的行数来检查是否在运行时返回了任何数据:

sCommands.setSqldbCommand(sVariables.sDataSet, sVariables.sSqlDbDataAdapter, sSQL, "House_Machinery");
if(sVariables.sDataSet.Tables["House_Machinery"].Rows.Count == 0)
   throw new Exception("No matching rows found");
DataRow sDataRow = sVariables.sDataSet.Tables["House_Machinery"].Rows[0];
于 2013-01-18T06:47:22.323 回答
0
1.Just check with Breakpoints if it works well,
2.Does your Sql query working in sql server ? check that it may circle it out.
3.Check your wildcard "%like%",this may have issues.


 void somewhereelse()
{
  string qry = "select Housemcode,Name, HP,Rateperhour ,Resource_H_Code FROM House_Machinery    where Housemcode like '" + sSearch + "'";
  filldetails(qry);
}


protected void filldetails(string someqry)
{
  Sqlconnection conn = new SqlConnection("Connectionstring");      
  Datatable dt = new Datatable();
  try
   { 
    conn.Open();
    SqlDataAdapter dap = new SqlDataAdapter(someqry,conn);
    dap.fill(dt);
    if(dt.rows.count >0)
      {
       txtItemName.Text = dt.rows.[0]["Name"].ToString();
       txtrate.Text = dt.rows.[0]["HP"].ToString();
       txtrate.Text = dt.rows.[0]["Rateperhour"].ToString();
      }

   }
 catch
   {
     throw;
   }
 finally
   {
     if(conn!= null)
      {
        conn.close();
       }
   }
于 2013-01-18T06:57:12.967 回答