2013-02-21 91 views
3

在我的winform中,我試圖通過傳遞項目的名稱來從數據庫中獲取項目的價格。 用戶可以在組合框中看到從數據庫填充項目的項目。 當用戶從組合框中選擇項目並單擊添加時,它會將該特定項目添加到數據庫中。索引超出範圍異常visual studio

在用戶添加項目的同時,其中一個文本框添加了價格。如果用戶添加五個項目,文本框顯示五個項目價格的總和。直到這裏一切正常。

現在當用戶想要從列表框中刪除該項目時,用戶選擇該項目並點擊刪除項目,visual studio會拋出錯誤「索引超出範圍」。

我使用相同的代碼用於獲取價格都添加方法和。減去方法,但不知道爲什麼只加方法的作品,而不是。減去

代碼中加入價格

public int addPrice() 
     { 

      DataSet ds = searchforPrice(comboBox2.Text); 
      int sum; 
      bool success = int.TryParse(maskedTextBox10.Text, out sum); 
      int price = Convert.ToInt32(ds.Tables[0].Rows[0]["Price"]); 
      return sum + price; 

     } 

代碼for substract price

public int subPrice() 
     { 

      DataSet ds = searchforPrice(listBox1.GetItemText(listBox1.SelectedItem)); 
      int sum; 
      bool success = int.TryParse(maskedTextBox10.Text, out sum); 
      int price = Convert.ToInt32(ds.Tables[0].Rows[0]["Price"]); 
      return sum - price; 

     } 

代碼從數據庫獲得價格

public DataSet searchforPrice(string itemName) 
{ 
    DataSet dataSet = new DataSet(); 

    // Create connection object 
    OleDbConnection oleConn = new OleDbConnection(connString); 
    try 
    { 
     oleConn.Open(); 
     string sql = "SELECT [Price] FROM [Product] WHERE [Product Name] ='" + itemName + "'"; 
     OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, oleConn); 
     dataAdapter.Fill(dataSet, "Product"); 

    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.ToString()); 
    } 
    finally 
    { 
     oleConn.Close(); 
    } 
    return dataSet; 
} 

enter image description here

+3

在這種情況下,這意味着你的表是空的。並幫助你一個忙,並看看參數化的查詢。 – 2013-02-21 13:37:05

+0

在代碼中放置一個斷點,看看你在哪裏得到異常。如果你得到的索引超出範圍,它往往意味着你正在訪問一個數組大於它的長度的數組。 – TYY 2013-02-21 13:37:56

+0

listBox1.GetItemText(listBox1.SelectedItem)即將作爲null不知道爲什麼 – 2013-02-21 13:42:36

回答

1

要麼有你的結果集的表或沒有行。這意味着訪問第一個條目(索引0)將不起作用,因爲沒有第一個條目。你得到一個IndexOutOfRangeException。檢查您的請求和數據源。

+0

,但它適用於完美的添加方法,爲什麼不減去,因爲我通過項目名稱作爲字符串從兩個方法 – 2013-02-21 13:38:28

0

searchforPrice方法返回沒有行的數據集。所以,行[0]不存在。檢查您的方法並將其適用於searchforPrice不返回任何數據(未找到任何價格)的情況。

if (ds.Tables[0].Rows.Count != 0 

{...}

1

這是失敗,因爲searchForPrice沒有在表中返回任何行。因此,錯誤

沒有排在位置0

所以,如果你改變線路從列表框中獲取值或其他地方的形式,將返回數據你的錯誤將被修復。

真正的潛在問題是價值被傳遞到searchForPrice

0

問題是因爲你的數據集正在返回0行。這是因爲項目名稱未傳遞給SQL查詢。 你只需要在你的subPrice()函數的微小變化,

public int subPrice() 
     { 

      DataSet ds = searchforPrice(listBox1.SelectedItem.ToString()); 
      int sum; 
      bool success = int.TryParse(maskedTextBox10.Text, out sum); 
      int price = Convert.ToInt32(ds.Tables[0].Rows[0]["Price"]); 
      return sum - price; 

     } 
+0

Visual Studio doesnot識別ListBoxItem – 2013-02-21 13:46:15

+0

http://stackoverflow.com/questions/7887829/listbox-selected-item- content-to-textblock – Ankit 2013-02-21 13:48:51

+0

直接使用listBox1.SelectedValue.ToString。如果所選項目是複雜對象,則可以執行((CAST)ListBox.SelectedItem).THE_PROPERTY_YOU_NEED。 – TYY 2013-02-21 13:50:18

0

已提供應該工作的代碼。

您需要檢查實際數據源中的列是否與通過ListBox項目傳遞的內容匹配。在我看來,你的命名約定很可能有差異。

如果通過列表框匹配傳遞的列名稱那些在您的數據源,然後下面的代碼應該工作: -

public int subPrice() 
     { 
      if (listBox1.SelectedItems.Count > 0) 
      { 
       string SearchString = listBox1.SelectedItem.ToString().Trim(); 
       DataSet ds = searchforPrice(SearchString); 

       if (ds.Tables["Product"].Rows.Count > 0) 
       { 
        bool success = int.TryParse(maskedTextBox10.Text, out sum); 
        int price = Convert.ToInt32(ds.Tables["Product"].Rows[0]["Price"]); 
        return sum - price; 
       }     
      } 

      return null; 
     } 
+0

給我錯誤「不是所有的代碼路徑都返回一個值」 – 2013-02-21 15:05:52

+0

請參閱我的更新。 – Derek 2013-02-21 15:30:56