Hi,
I have an Add Order form called frmAddOrder with a customer combobox. I am using a DataReader object in a DataAccessObject class with a getAllCustomers() method to retrieve the list of customers from the database. In the frmAddOrder i have a method called fillCustomerComboBox() to populate the combo box using the list of customers. However when i load the form the list of customers does not appear in the ComboBox. Any suggestions for rectifying this problem? Below is the code for the Add Order form
public partial class frmAddOrder : Form
{
// Add a parent form to refer to the parent form
private frmMain parentForm;
private Customer aCustomer;
private Customer[] customerSet;
private string resultMsg = "";
/******Define Constructors******/
public frmAddOrder()
{
InitializeComponent();
}
public frmAddOrder(Customer aCustomer)
{
InitializeComponent();
this.aCustomer = aCustomer;
customerSet = aCustomer.viewCustomers(ref resultMsg);
if (!fillCustomerComboBox())
{
MessageBox.Show("Unable to Load Customers for Selection from ComboBox");
this.Close();
}
}
public frmAddOrder(frmMain parentForm)
{
InitializeComponent();
this.parentForm = parentForm;
}
/******* End of Constructors definitions *******/
private void frmAddOrder_Load(object sender, EventArgs e)
{
this.BackColor = Color.Azure;
this.parentForm = new frmMain();
this.parentForm.Hide();
Cursor.Current = Cursors.Default;
}
private bool fillCustomerComboBox()
{
try
{
// clear the combo box
cmbCustomer.Items.Clear();
for (int i = 0; i < customerSet.Length; i++)
{
// populate the combo box
cmbCustomer.Items.Add(new KeyValuePair<int, String>
(customerSet[i].CustomerID, customerSet[i].CustomerName));
}
// store the customer's ID as a hidden value in the combo box and
// make the customer's first name visible for selection.
cmbCustomer.ValueMember = "Key";
cmbCustomer.DisplayMember = "Value";
// Automatically select the first customer
cmbCustomer.SelectedIndex = 0;
return (true);
}
catch (Exception ex)
{
MessageBox.Show(" Error filling the customer ComboBox on New Orders Form" + ex.Message);
return (false);
}
} // end of fillCustomerComboBox() method