-11
我用三層架構 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
這個例外很明顯。您正嘗試從不存在的位置讀取數據。很可能是因爲您嘗試從6行讀取數據,而不是隻讀取一行。 – waka
如果你有一個數組包含三個像'string [3]'這樣的項目,並且你試圖訪問第四個位置,你會得到一個'索引超出範圍'的錯誤。你也是這樣做的,你試圖訪問一個不存在的索引。我們無法知道您提供的代碼的位置,甚至不會告訴我們錯誤發生在哪條線上...... – Equalsk
謝謝您的工作 – sangeetha