请告诉我,我在哪里wrom如何纠正它:
问题描述:
我用三层架构 System.IndexOutOfRangeException: 输入代码在这里没有列在位置1在这里与我贴我的代码在这里请告诉我,我在哪里wrom如何纠正它:
数据层
数据接取层
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
答
您还需要检查查询是否正在返回数据,因为您正尝试访问不存在的数据行数据,所以您正面临问题。我希望你明白我的观点。
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 [email protected]_id and [email protected]_id
END
GO
有在你的代码的其他东西,你应该使用“使用“连接来处置它。
public DataSet getRecordDisplay(int product_id,int category_id)
{
...
using(SqlConnection con = new SqlConnection(connectionstring))
{
}
...
}
+0
谢谢你igot吧 – sangeetha
这个例外很明显。您正尝试从不存在的位置读取数据。很可能是因为您尝试从6行读取数据,而不是只读取一行。 – waka
如果你有一个数组包含三个像'string [3]'这样的项目,并且你试图访问第四个位置,你会得到一个'索引超出范围'的错误。你也是这样做的,你试图访问一个不存在的索引。我们无法知道您提供的代码的位置,甚至不会告诉我们错误发生在哪条线上...... – Equalsk
谢谢您的工作 – sangeetha