Hi

I have a windows form application connecting to a sql compact 3.5 database. I can add and delete records via buttons I have created. When I add a record I update the combo box and it works fine. But if I delete a record, I first have to close the application and restart it before the deleted record is removed from the combo box and I think memory or cache or where ever it is. The whole row is stored somewhere when I delete it.

How can I remove the record from the combobox en the other textboxes where I show the data. I include the code for create and delete

#region Create Product

        private void btCreateProd_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbProdName.Text == string.Empty)
                {
                    MessageBox.Show("Please Enter a New Product name");
                }

                string searchFor = cbProdName.Text;
                int results = 0;
                DataRow[] returnedRows;
                returnedRows = spitKeyDataBaseDataSet3.Tables["ProductSetup"].Select("ProductName='" + searchFor + "'");

                results = returnedRows.Length;

                if (results > 0)
                {
                    MessageBox.Show("Product name already exist, please select another one");
                }
                else
                {
                    int m_rowPosition = 0;
                    DataRow drNewRow = spitKeyDataBaseDataSet3.ProductSetup.NewRow();
                    drNewRow["ProductName"] = cbProdName.Text;
                    drNewRow["Email1"] = txtEmail1.Text;
                    drNewRow["Email2"] = txtEmail2.Text;
                    drNewRow["Email3"] = txtEmail3.Text;
                    drNewRow["KeyNo"] = cbKey.Text;
                    drNewRow["SerialNo"] = txtSerNo.Text;
                    
                    spitKeyDataBaseDataSet3.ProductSetup.Rows.Add(drNewRow);
                    productSetupTableAdapter.Update(spitKeyDataBaseDataSet3.ProductSetup);
                    m_rowPosition = spitKeyDataBaseDataSet3.ProductSetup.Rows.Count - 1;
                    MessageBox.Show("Product created");
                    this.cbProdName.Refresh();
                    this.comboBox3.Refresh();
                }
            }
            catch
            {

            }
            try
            {
                //ensure the box is cleared
                cbProdName.Items.Clear();
                comboBox3.Items.Clear();
                //userDataTableAdapter.GetData();
                //put the data from the dataset into the combobox
                DataTable oDataTable = spitKeyDataBaseDataSet3.Tables["ProductSetup"];
                if (oDataTable == null)
                    throw new Exception("ProductSetup table not found in database.");

                foreach (DataRow oRow in oDataTable.Rows)
                {
                    cbProdName.Items.Add(oRow["ProductName"]);
                    comboBox3.Items.Add(oRow["ProductName"]);
                }
            }
            catch (Exception oE)
            {
                MessageBox.Show("Problem Populating comboBox:[" + oE.ToString() + "]");
            }
            cbProdName.Text = string.Empty;
            txtEmail1.Text = string.Empty;
            txtEmail2.Text = string.Empty;
            txtEmail3.Text = string.Empty;
            txtSerNo.Text = string.Empty;
        }
        #endregion

#region Delete Product
        private void button2_Click(object sender, EventArgs e)
        {
            string searchFor = cbProdName.Text;
            int results = 0;
            DataRow[] returnedRows;
            returnedRows = spitKeyDataBaseDataSet3.Tables["ProductSetup"].Select("ProductName='" + searchFor + "'");
            results = returnedRows.Length;
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result = MessageBox.Show("Are you sure you want to delete this Product", "Delete Product", buttons);
            if (result == DialogResult.Yes)
            {
                try
                {
                    if (results > 0)
                    {
                        productSetupTableAdapter.DeleteQuery(cbProdName.Text);
                        productSetupTableAdapter.Update(spitKeyDataBaseDataSet3.ProductSetup);
                        this.cbProdName.Refresh();
                        this.comboBox3.Refresh();
                    }

                    //ensure the box is cleared
                    cbProdName.Items.Clear();
                    comboBox3.Items.Clear();
                    //userDataTableAdapter.GetData();
                    //put the data from the dataset into the combobox
                    DataTable oDataTable = spitKeyDataBaseDataSet3.Tables["ProductSetup"];
                    if (oDataTable == null)
                        throw new Exception("Product name table not found in database.");

                    foreach (DataRow oRow in oDataTable.Rows)
                    {
                        cbProdName.Items.Add(oRow["ProductName"]);
                        comboBox3.Items.Add(oRow["ProductName"]);
                    }

                    //the finally clause will tidy up for us.
                    cbProdName.Text = string.Empty;
                    txtEmail1.Text = string.Empty;
                    txtEmail2.Text = string.Empty;
                    txtEmail3.Text = string.Empty;
                    txtSerNo.Text = string.Empty;
                }

                catch (Exception oE)
                {
                    MessageBox.Show("Problem Populating comboBox:[" + oE.ToString() + "]");
                }
                
                
            }
            else
            {

            }
        }
        #endregion

>But if I delete a record, I first have to close the application...

After the deletion, re-bind combo.

This article has been dead for over six months. Start a new discussion instead.