0

我正在尝试根据组合框中选择的项目填充文本框。问题是它在加载时引发以下错误,我在visual studio中按下next ..next ..它做了我真正想做的事情。

在此处输入图像描述 如何解决负载问题。

表单加载代码是

private void UpdateProduct_Load(object sender, EventArgs e)
        {
            DataSet ds = GetAllItems();
            comboBox2.DataSource = ds.Tables[0];
            comboBox2.DisplayMember = "Product Name";
            comboBox2.SelectedIndex = 0;


        }

组合框选定索引的代码是

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
           // string selectedText = this.comboBox2.GetItemText(this.comboBox2.SelectedItem);
            DataSet d = GetProductInfo(comboBox2.Text);
            if (d.Tables.Count > 0)
            {
                textBox2.Text = d.Tables[0].Rows[0]["Quantity"].ToString();
                textBox3.Text = d.Tables[0].Rows[0]["Color"].ToString();
                textBox4.Text = d.Tables[0].Rows[0]["Size"].ToString();
                textBox5.Text = d.Tables[0].Rows[0]["Price"].ToString();
            }

        }

我只有在第一次加载表单时遇到问题。

获取产品信息代码

  public DataSet GetProductInfo(string product)
        {
            DataSet dataSet = new DataSet();
            OleDbConnection oleConn = new OleDbConnection(connString);

            try
            {
                oleConn.Open();
                string sql = "SELECT [Quantity], [Color], [Size], [Price] FROM [Product] WHERE [Product Name]= '" + product + "'";
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, oleConn);
                dataAdapter.Fill(dataSet, "Product");
            }
            catch (Exception ex)
            {
                MessageBox.Show("An exception has been occured\n" + ex.ToString());
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                oleConn.Close();
            }
            if (dataSet.Tables["Product"].Rows.Count <= 0)
                return null;

            return dataSet;
        }

堆栈跟踪

System.NullReferenceException occurred
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=Purchase Management
  StackTrace:
       at Purchase_Management.UpdateProduct.comboBox2_SelectedIndexChanged(Object sender, EventArgs e) in c:\Users\Amrit\Desktop\Purchase Management\Purchase Management\UpdateProduct.cs:line 99
  InnerException: 
4

4 回答 4

2

不要使用 SelectedIndexChanged,而是使用SelectionChangeCommitted事件。这避免了所有这些问题,因为它仅在用户更改所选项目时触发,而不是在您初始化组合框时触发。

于 2013-03-02T15:06:08.767 回答
0

你的方法返回null。这就是为什么抛出错误

DataSet dstProduct = new DataSet();
dstProduct = GetProductInfo(comboBox2.Text);

然后尝试

if(dstProduct.Tables.Count>0 && dstProduct.Tables[0].Rows.Count >0)
{
 // then do stuff. 
}
于 2013-03-02T14:28:31.287 回答
0

因为.Countd = null.

宣布DataSet d = new DataSet();

于 2013-03-02T14:29:27.453 回答
0

只需添加if (!string.IsNullOrEmpty(productName))语句,您就可以了:

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    var productName = comboBox2.Text;
    if (!string.IsNullOrEmpty(productName)) {

        DataSet d = GetProductInfo(productName);
        if (d.Tables.Count > 0)
        {
            textBox2.Text = d.Tables[0].Rows[0]["Quantity"].ToString();
            textBox3.Text = d.Tables[0].Rows[0]["Color"].ToString();
            textBox4.Text = d.Tables[0].Rows[0]["Size"].ToString();
            textBox5.Text = d.Tables[0].Rows[0]["Price"].ToString();
        }
    }
}
于 2013-03-02T14:39:14.343 回答