aspx.net指定的参数超出了有效值的范围

时间:2017-01-27 16:56:41

标签: c# asp.net

错误为strProductId = row0.Cells[1].Text;。它显示:

  

指定的参数超出了有效值的范围。

有人可以解释代码有什么问题吗?谢谢。

//retrieve the product ID from the first row of the DetailsView
DetailsViewRow row0 = DetailsView2.Rows[0];
strProductId = row0.Cells[1].Text;

//extract the QuantityOnHand from the database - based on the product ID 

strSQLSelect = "SELECT pQty FROM Products WHERE pItemID = @ProductId"; 
cmd = new OleDbCommand(strSQLSelect, mDB);
cmd.Parameters.Add("@ProductId", OleDbType.VarChar).Value = strProductId; 
object oQty = cmd.ExecuteScalar(); 
intQuantityOnHand = (int)oQty

1 个答案:

答案 0 :(得分:1)

请检查row0.Cells[1].Text row0的值.Cells没有数组索引1.请调试以下代码。

DetailsViewRow row0 = DetailsView2.Rows[0];
if(row0.cell.count > 0)
{
  strProductId = row0.Cells[1].Text; 
}