English 中文(简体)
标 标 字
原标题:Object reference not set to an instance of an object for combo box

我看到许多反对的提及并不涉及一个标的事例,但我看不出我的情况。

我有一个名为comboBox1的 com盒。 在装货单的同时,我还有人种化 com器的编码:

  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.S selectedValue.ToString()); line表示:

“未提及 com子的物体”。

但我感到惊讶的是,指数0的数值被设定为 com箱,而在这个物体参考点后形成装货。

最佳回答

您可以尝试将这一发言放在页数上,并确保 com子装上物品?

comboBox1.SelectedIndex = 0; 

如果你使用“双赢”模式,那么就试图将这一声明放在“协同”的初始功能中。

comboBox1.SelectedIndex = 0; 
问题回答

首先,你们不把选定的指数定在0。 违约时为零。 在执行读者之后,不可能从数据库中装载任何东西,因此 com子的数据来源是无效的。 在这种情况下,如果你试图将选定的指数设定为0,那么作为框架,就会把无效的参考例外放在试图检索数据来源中的第一个项目。 在这种情况下,你选定的指数应为-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”放在读者身上,但不是在哪里,你是否确信它不对案件敏感?

第二,由于你重新与数据库合作,一种比较容易的方法是将散射结果退回到可数据化功能中,然后使用数据约束力。

"Object reference not set to an instance of an object for combo box".

Means one or two things normally.
combobox not initialized to null;
or combobox not initalized to new

ComboBox combobox = null;
then inside of the try set the combobox variable to an instance of new like the following 
Try
{
  combobox = new Combobox();
}

在试图使用<代码>时,我也使用同样的错误信息。 SelectedValue。 我将其改为<代码>。 选定的项目和错误消失。 例:

string selection = comboBox1.SelectedItem.ToString();

try this instead

    if (ComboWeeks.SelectedItem as WeeksTable is null)  
 return;

这是我的工作。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签