2017-09-13 89 views
-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 
+0

這個例外很明顯。您正嘗試從不存在的位置讀取數據。很可能是因爲您嘗試從6行讀取數據,而不是隻讀取一行。 – waka

+0

如果你有一個數組包含三個像'string [3]'這樣的項目,並且你試圖訪問第四個位置,你會得到一個'索引超出範圍'的錯誤。你也是這樣做的,你試圖訪問一個不存在的索引。我們無法知道您提供的代碼的位置,甚至不會告訴我們錯誤發生在哪條線上...... – Equalsk

+0

謝謝您的工作 – sangeetha

回答

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 [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