收到错误“对象引用未设置为对象的实例”

时间:2013-10-10 11:02:22

标签: c# wpf

我收到错误:

  

对象引用未设置为对象的实例。

我在cbPart.Items.Clear();上收到错误,当我评论此行时错误消失但cbPart组合框重复现有条目。

清除按钮:

private void btClear1_Click(object sender, RoutedEventArgs e)
{
    txtID.Text = String.Empty;
    txtName.Text = String.Empty;
    txtDe.Text = String.Empty;
    txtty.Text = String.Empty;
    ima.Source = null;

    cbType.IsEnabled = true;
    btAdd.IsEnabled = true;

    display();
}

display()

public void display()
{      
    try
    {
        cbPart.Items.Clear();

        sc.Open();
        string Query = "select * from Part";
        SqlCommand createCommand = new SqlCommand(Query, sc);
        SqlDataReader dr = createCommand.ExecuteReader();
        while (dr.Read())
        {
            string Name = dr.GetString(1);

            cbPart.Items.Add(Name);//Displaying a list in the Combo Box
        }
        sc.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

0 个答案:

没有答案