ComboBox leave event .net 1.1
Hi All
I'm trying to develop a windows based application and i'm stuck at the minute
I have a update item form where it displays the item details.
in the onload event i have the following
private void UpdateItem_Load(object sender, System.EventArgs e)
{
//to get all suppliers
update_changeSuppCB.Enabled = false;
string getItemSupp = "SELECT suppliers_name FROM Suppliers";
//SqlCommand getItemSuppCmd = new SqlCommand(getItemSupp, conn);
SqlDataAdapter getItemSuppAdp = new SqlDataAdapter(getItemSupp, conn);
DataSet getItemSuppDs = new DataSet();
try
{
conn.Open();
//getItemSuppCmd.ExecuteReader();
//update_changeSuppCB.
getItemSuppAdp.Fill(getItemSuppDs, "Suppliers");
update_changeSuppCB.DataSource = getItemSuppDs.Tables[0];
update_changeSuppCB.DisplayMember = "suppliers_name";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}//end of load event
in the form i have a combo box with a leave event with the following code
private void update_changeSuppCB_Leave(object sender, System.EventArgs e)
{
string suppName = update_changeSuppCB.Text;
//now to get the id of the suppliers name (note i'm calling another class to get the suppliers id)
int suppId = getCatSuppId.getSupplierId(suppName);
//if the user selected a different supplier then go ahead with the change else do nothing
if (suppName != update_suppliersTxt.Text)
{
string updateSupp = "UPDATE Items SET supplier_id = " + suppId + " WHERE id = " + update_itemIdTxt.Text;
SqlCommand updateSuppCmd = new SqlCommand(updateSupp, conn);
try
{
conn.Open();
updateSuppCmd.ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}
}
the problem is i'm getting "Invalid Operation Exception- The connection is already open"
now this is happening because after executing this line update_changeSuppCB.DataSource = getItemSuppDs.Tables[0]; The leave event (private void UpdateItem_Load(object sender, System.EventArgs e)) is being called.
Why is it being called.. shouldnt only be called after leaving away from the combobox.
Thank you in advance
Nilanka DharmadasaPosted Oct 16, 2009, 5:12 AM
this.update_changeSuppCB.Leave -= new System.EventHandler(this.update_changeSuppCB_Leave);
update_changeSuppCB.DataSource = getItemSuppDs.Tables[0];
update_changeSuppCB.DisplayMember = "suppliers_name";
this.update_changeSuppCB.Leave += new System.EventHandler(this.update_changeSuppCB_Leave);
Hope this will help you.
Yasmine AminPosted Oct 16, 2009, 6:59 AM