我使用了三层架构 System.IndexOutOfRangeException: 在这里输入代码位置1没有行。这里我粘贴我的代码
数据访问层
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
答案 0 :(得分:0)
您还需要检查查询是否返回数据,您将面临问题,因为您正在尝试访问不存在的数据行数据。我希望你明白我的观点。
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/>";
}
存储过程存在问题
SELECT * FROM product04 where product_id=345 and category_id=2346
而不是这行查询,它应该是
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
你的代码中还有其他东西,你应该使用&#34;使用&#34;连接处理它。
public DataSet getRecordDisplay(int product_id,int category_id)
{
...
using( SqlConnection con = new SqlConnection(connectionstring))
{
}
...
}