未将对象引用设置为组合框的对象实例
我看到许多对象引用未设置为对象问题的实例,但我在任何情况下都找不到我的场景。
我有一个名为 comboBox1
的组合框。表单加载时,我有代码来填充组合框:
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the
// 'tenderDBDataSet.tbl_Tender_To_Details' table.
// You can move, or remove it, as needed.
OleDbCommand cmd = new OleDbCommand("SELECT DISTINCT
tbl_Tender_To_Details.To_Name, tbl_Tender_To_Details.To_Address1,
tbl_Tender_To_Details.To_Address2,
tbl_Tender_To_Details.To_City, tbl_Tender_To_Details.To_PinCode "+
"FROM tbl_Tender_To_Details "+
"WHERE to_Name IS NOT NULL ", conn);
try
{
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["To_Name"]);
// listBox1.Items.Add(reader[0].ToString());
// MessageBox.Show(reader[0].ToString());
}
reader.Close();
comboBox1.SelectedIndex = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
}
MessageBox.Show(comboBox1.SelectedValue.ToString());
行显示:
“未将对象引用设置为组合框的对象实例”。
但令我惊讶的是,索引 0 处的值被设置为组合框,而表单在此对象引用消息框之后加载。
I have seen many object reference not set to an instance of an object questions, but I couldn't find my scenario in any.
I have a combo box named comboBox1
. While form loads I have code to populate combobox:
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the
// 'tenderDBDataSet.tbl_Tender_To_Details' table.
// You can move, or remove it, as needed.
OleDbCommand cmd = new OleDbCommand("SELECT DISTINCT
tbl_Tender_To_Details.To_Name, tbl_Tender_To_Details.To_Address1,
tbl_Tender_To_Details.To_Address2,
tbl_Tender_To_Details.To_City, tbl_Tender_To_Details.To_PinCode "+
"FROM tbl_Tender_To_Details "+
"WHERE to_Name IS NOT NULL ", conn);
try
{
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["To_Name"]);
// listBox1.Items.Add(reader[0].ToString());
// MessageBox.Show(reader[0].ToString());
}
reader.Close();
comboBox1.SelectedIndex = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
}
The MessageBox.Show(comboBox1.SelectedValue.ToString());
line shows:
"Object reference not set to an instance of an object for combo box".
But my surprise is the value at index 0 is set to combo box while form loads after this object reference msg box.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以尝试将此语句放在页面加载时并确保组合框是否已加载项目?
如果您使用的是winforms,请尝试将该语句放入initializeComponent()函数中
you can try put this statement at page load and make sure the combobox has loaded with items or not??
if you are using winforms then try put that statement in initializecomponent() function
首先,不要将所选索引显式设置为 0。默认为 0。执行阅读器后,有可能不会从数据库加载任何内容,因此组合框的数据源为空。在这种情况下,如果您尝试将所选索引设置为 0,则当框架尝试检索数据源中第一个为 null 的项目时,将引发空引用异常。在这种情况下,您选择的索引应该是-1。
因此,如果您希望所选索引成为列表中的第一项,我不会显式设置所选项目。这是组合框的默认行为。
First of all you do not to set the selected index to 0 explicitly. It is 0 by default. There are chances that nothing is loaded from database after executing the reader and hence the DataSource of combobox is null. In that scenario, if you try to set the selected index to 0, null reference exception would be thrown as framework tries to retrieve the first item in data source which is null. In this scenario, your selected index should be -1.
So, if you want the selected index to be the first item in the list, I would not set the selected item explicitly. That is the default behavior for combobox.
首先,您是否尝试使用调试器来检查读者是否实际上放置了任何内容?
我注意到您在阅读器中大写了“To_Name”,但在 where 子句中没有大写 - 您确定它不区分大小写吗?
其次,由于您正在使用数据库,因此更简单的方法是将函数的数据库结果返回到数据表中,然后使用数据绑定。
Firstly, did you try to use a debugger to check whether the reader is actually putting anything?
I noticed that you capitalised "To_Name" in the reader but not in the where clause - are you sure its not case-sensitive?
Secondly, since you're working with databases, an easier method would be to return the db results the function into a DataTable and then use Databinding.
尝试使用
.SelectedValue
时,我收到了同样的错误消息。我将其更改为.SelectedItem
并且错误消失了。例子:I got this same error message when trying to use
.SelectedValue
. I changed it to.SelectedItem
and the error went away. Example:试试这个,
这对我有用
try this instead
this is work for me