comboBox.SelectedValue空错误
我在Access 2010数据库中使用C#VS 2010,当我试图基于所选的DropDownList组合框填充列表框时,我不断收到一个空错误(对象引用未设置为对象的实例)。例如,用户在say Westerns的组合框中选择电影类型。然后,列表框应填充该类型的标题 我试着通过简单的使用comboBox.SelectedValue空错误,combobox,null,selecteditem,Combobox,Null,Selecteditem,我在Access 2010数据库中使用C#VS 2010,当我试图基于所选的DropDownList组合框填充列表框时,我不断收到一个空错误(对象引用未设置为对象的实例)。例如,用户在say Westerns的组合框中选择电影类型。然后,列表框应填充该类型的标题 我试着通过简单的使用 ComboBox c = New ComboBox(); c = comboBox1; 但是我的列表框中不会填充任何内容。我可以在查询中手动将我的流派设置为Western,但我不希望使用该方法,这样我就可以将其应
ComboBox c = New ComboBox();
c = comboBox1;
但是我的列表框中不会填充任何内容。我可以在查询中手动将我的流派设置为Western,但我不希望使用该方法,这样我就可以将其应用于大型场景
public partial class Form1 : Form
{
OleDbConnection cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data source = c:\\users\\Nick\\Desktop\\DatabaseTest.accdb");
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
try
{
cn.Open();
}
catch (ObjectDisposedException ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
OleDbCommand cm = new OleDbCommand("SELECT Genre FROM Genre", cn);
try
{
OleDbDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr["Genre"]);
}
dr.Close();
dr.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//private void button1_Click(object sender, EventArgs e)
//{
//}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
OleDbCommand cf = new OleDbCommand("SELECT Title FROM Movies WHERE mGenreID=@Genre", cn);
cf.Parameters.Add("@Genre", comboBox.SelectedValue.ToString());
try
{
OleDbDataReader dr = cf.ExecuteReader();
while (dr.Read())
{
listBox1.Items.Add(dr["Title"]);
}
dr.Close();
dr.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
您正在将项目添加到
组合框1
,但正在从组合框
读取值。请注意缺少1