2017-03-06 10 views
0

SQLから特定の行を対象行として宣言する行で、「インデックス0は負またはそれ以上の行数です」というエラーメッセージが表示され続けます。私は他の例を見てみましたが、この特定の問題を解決する方法はありません。私のPOVからは、SQLは問題がありません。なぜなら、すべてが正しく読み込まれるからです。インデックス0は負またはそれ以上の行数です

コード:

​​

サーバーエラー:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Index 0 is either negative or above rows count.

スタックトレース:

[IndexOutOfRangeException: Index 0 is either negative or above rows count.] 
    System.Data.DataView.GetRow(Int32 index) +1788553 
    System.Data.DataView.get_Item(Int32 recordIndex) +13 
    Order.GetSelectedProduct() in f:\Year 2\Internet Applications Programming\Assignment 2\Lab 3 - 06-03-17\Ex04Cart\Order.aspx.cs:23 
    Order.Page_Load(Object sender, EventArgs e) in f:\Year 2\Internet Applications Programming\Assignment 2\Lab 3 - 06-03-17\Ex04Cart\Order.aspx.cs:12 
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51 
    System.Web.UI.Control.OnLoad(EventArgs e) +95 
    System.Web.UI.Control.LoadRecursive() +59 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2952 
+2

'productsTable'には何も含まれていません。 'productsTable [0]'が範囲外です。 –

答えて

1

productsTableは何も含まれていません。したがってproductsTable[0]が範囲外です。

これで、レコードがあれば、フィルタリングをチェックしましょう。

private Product GetSelectedProduct() 
{ 
    DataView productsTable = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty); 
    productsTable.RowFilter = string.Format("ProductID = '{0}'", ddlProducts.SelectedValue); 

    if (productsTable.Count > 0) 
    { 
     DataRowView row = (DataRowView)productsTable[0]; 

     Product p = new Product(); 
     p.ProductID = row["ProductID"].ToString(); 
     p.Name = row["Name"].ToString(); 
     p.ShortDescription = row["ShortDescription"].ToString(); 
     p.LongDescription = row["LongDescription"].ToString(); 
     p.UnitPrice = (decimal)row["UnitPrice"]; 
     p.ImageFile = row["ImageFile"].ToString(); 
     return p; 
    } 
    else 
    { 
     // Or throw an exception, if your logic dictates that this method SHOULD return a record. 
     return null; 
    } 
} 
+1

@CodeCaster、gotcha –

関連する問題