我正在尝试根据组合框中选择的项目填充文本框。问题是它在加载时引发以下错误,我在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: