C# 请告诉我我在哪里,如何更正它:
我使用了三层架构 System.IndexOutOfRangeException: 在此处输入代码位置1没有行。我把代码贴在这里 数据层 数据访问层C# 请告诉我我在哪里,如何更正它:,c#,C#,我使用了三层架构 System.IndexOutOfRangeException: 在此处输入代码位置1没有行。我把代码贴在这里 数据层 数据访问层 public DataSet getRecordDisplay(int product_id,int category_id) { string constring = string.Empty; SqlConnection conn;
public DataSet getRecordDisplay(int product_id,int category_id)
{
string constring = string.Empty;
SqlConnection conn;
constring = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
conn = new SqlConnection(constring);
conn.Open();
SqlCommand cmd = new SqlCommand("sp_Productselect", conn);
cmd.CommandText = "sp_Productselect";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@product_id", DbType.Int32).Value = product_id;
cmd.Parameters.AddWithValue("@category_id", DbType.Int32).Value = category_id;
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet dst = new DataSet();
adpt.Fill(dst);
return dst;
}
#Business logic
业务逻辑层包含在这里
public DataSet getRecordDisplay(int product_id,int category_id)
{
DataSet ds = new DataSet();
ds = dal.getRecordDisplay(product_id,category_id);
return ds;
}
# code behind
int product_id = Convert.ToInt32(txtPId.Text);
int category_id = Convert.ToInt32(txtctid.Text);
DataSet dsOrderDetail = new DataSet();
dsOrderDetail = bal.getRecordDisplay(product_id, category_id);
lblProdName.Text = dsOrderDetail.Tables[0].Rows[0]["Product_Name"].ToString() + "<br/>";
lblProdId.Text = dsOrderDetail.Tables[0].Rows[1]["Product_Id"].ToString() + "<br/>";
lblProdDesc.Text = dsOrderDetail.Tables[0].Rows[2]["Description"].ToString() + "<br/>";
lblCtyId.Text = dsOrderDetail.Tables[0].Rows[3]["Category_id"].ToString() + "<br/>";
lblPrice.Text = dsOrderDetail.Tables[0].Rows[4]["Price"].ToString() + "<br/>";
lblAblty.Text = dsOrderDetail.Tables[0].Rows[5]["Availability"].ToString() + "<br/>";
#my stored procedure
stored procedure is added here
GO
CREATE PROCEDURE [dbo].[sp_Productselect]
@product_id as int,
@category_id as int
AS
BEGIN
SELECT * FROM product04 where product_id=345 and category_id=2346
END
GO
您还需要检查查询是否返回数据,因为您正试图访问不存在的datarow数据,所以您将面临这个问题。我希望你明白我的意思
dsOrderDetail = bal.getRecordDisplay(product_id, category_id);
if(dsOrderDetail !=null && dsOrderDetail.Table.Count > 0 && dsOrderDetail.Table.Rows.Count >0)
{
lblProdName.Text = dsOrderDetail.Tables[0].Rows[0]["Product_Name"].ToString() + "<br/>";
lblProdId.Text = dsOrderDetail.Tables[0].Rows[1]["Product_Id"].ToString() + "<br/>";
lblProdDesc.Text = dsOrderDetail.Tables[0].Rows[2]["Description"].ToString() + "<br/>";
lblCtyId.Text = dsOrderDetail.Tables[0].Rows[3]["Category_id"].ToString() + "<br/>";
lblPrice.Text = dsOrderDetail.Tables[0].Rows[4]["Price"].ToString() + "<br/>";
lblAblty.Text = dsOrderDetail.Tables[0].Rows[5]["Availability"].ToString() + "<br/>";
}
这行查询应该是
CREATE PROCEDURE [dbo].[sp_Productselect]
@product_id as int,
@category_id as int
AS
BEGIN
SELECT * FROM product04 where product_id=@product_id and category_id=@category_id
END
GO
代码中还有其他东西,您应该使用for connection来处理它
public DataSet getRecordDisplay(int product_id,int category_id)
{
...
using( SqlConnection con = new SqlConnection(connectionstring))
{
}
...
}
例外情况很明显。您正在尝试从不存在的位置读取数据。最有可能是因为您尝试从6行而不是仅从一行读取数据。如果您的数组包含三项,如字符串[3],并且您尝试访问第4个位置,则会出现索引超出范围错误。你也在做同样的事情,你试图访问一个不存在的索引。我们不能告诉你从哪里提供的代码,你甚至没有告诉我们错误发生在哪一行…谢谢你注意到你应该高度考虑重新命名你的存储过程。调用以sp_uuu前缀命名的过程,无论当前目标数据库是什么,都会首先检查主数据库中的该过程,然后查看目标数据库。谢谢
public DataSet getRecordDisplay(int product_id,int category_id)
{
...
using( SqlConnection con = new SqlConnection(connectionstring))
{
}
...
}