索引超出范围异常visual studio

问题描述:

在我的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

要么有你的结果集的表或没有行。这意味着访问第一个条目(索引0)将不起作用,因为没有第一个条目。你得到一个IndexOutOfRangeException。检查您的请求和数据源。

+0

,但它适用于完美的添加方法,为什么不减去,因为我通过项目名称作为字符串从两个方法 – 2013-02-21 13:38:28

searchforPrice方法返回没有行的数据集。所以,行[0]不存在。检查您的方法并将其适用于searchforPrice不返回任何数据(未找到任何价格)的情况。

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

{...}

这是失败,因为searchForPrice没有在表中返回任何行。因此,错误

没有排在位置0

所以,如果你改变线路从列表框中获取值或其他地方的形式,将返回数据你的错误将被修复。

真正的潜在问题是价值被传递到searchForPrice

问题是因为你的数据集正在返回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://*.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

已提供应该工作的代码。

您需要检查实际数据源中的列是否与通过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