2017-09-13 9 views
-11

を私は3層アーキテクチャ System.IndexOutOfRangeExceptionを使用: 私はここwromそれを修正する方法を教えてください:

データ層私のコードを貼り付けて何行はここに位置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

例外はかなり明確です。存在しない位置からデータを読み取ろうとしています。ほとんどの場合、1行だけではなく6行からデータを読み取ろうとするためです。 – waka

+0

この 'string [3]'のような3つの項目を含む配列を持っていて、4番目の位置にアクセスしようとすると、 'index out of range'エラーが発生します。あなたは同じことをしています、あなたは存在しないインデックスにアクセスしようとしています。あなたが提供したコードのどこにエラーが発生したのかを教えてもらえません... – 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

ありがとうございます – sangeetha

関連する問題