以下函数给出了一个错误:
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没有行。
任何人都可以对此有所了解吗?
答案 0 :(得分: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];
答案 1 :(得分: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();
}
}