I have a parent form that has a combobox. Whn the form loads I get the data from the db and bind to the combobox. that works fine and here is the code I use in form load to cause this to happen:

conn.Open()
        cmd.Connection = conn
        da = New SqlCeDataAdapter(cmd1, conn)

        cmd.CommandText = "SELECT * FROM AccountType ORDER BY AcctType"
        da2 = New SqlCeDataAdapter(cmd)
        da2.Fill(dtAcctType)

        cmd.CommandText = chkSQL
        daCk = New SqlCeDataAdapter(chkSQL, conn)
        daCk.Fill(dtChk)



        cboType.DataSource = dtAcctType
        cboType.DisplayMember = "AcctType"
        cboType.ValueMember = "AcctTypeID"
        cboType.SelectedIndex = 0

Now if there is not an Account Type, I have a button that will pull up a small form to allow the addition of an account type. The popup form adds the data back to the database just fine, but I am now trying to get the combobox to refresh its data. So I call a public Sub that is located in the parent form to from the popup form to accomplish this task and I have walked the code and the sub is called and appears to work correctly, but the new data never appears in the combobox. Here is my public sub that i call to try and refresh the combobox on the parent form.

Dim dtType As New DataTable
        Dim daType As SqlCeDataAdapter


        Try
            conn = New SqlCeConnection("Data Source=C:\Program Files\HomeComplete\Data\HomeComplete.sdf")
        Catch
        End Try

        Dim cmd As New SqlServerCe.SqlCeCommand

        cmd.Connection = conn

        cmd.CommandText = "SELECT * FROM AccountType ORDER BY AcctType"
        daType = New SqlCeDataAdapter(cmd)

        daType.Fill(dtType)
        cboType.Items.Clear()
        cboType.DataSource = dtType
        cboType.DisplayMember = "AcctType"
        cboType.ValueMember = "AcctTypeID"
        cboType.SelectedIndex = 0
        cboType.Refresh()


        conn.Close()

But when I go back to the parent form, the combobox is not updated with the new account. How can i get the combobox to refresh from the database with the new data?

Thank You, Chester

Dani AI

Generated

A few things are commonly missed in this scenario. As describes, the popup writes the new AccountType to the database but the parent combobox never shows it. As pointed out, calling a refresh routine is fine — the problem is usually how that refresh is invoked or how the combobox is rebound.

A robust pattern is to have the popup notify the parent (rather than the popup locating and calling a parent method directly). Raise an event in the popup after a successful insert, subscribe to it from the parent, and re-query/rebind when it fires. Example (conceptual):

' In popup:
Public Event AccountTypeAdded()

' Call after insert succeeds:
RaiseEvent AccountTypeAdded()

' In parent before showing popup:
Dim f As New AddTypeForm()
AddHandler f.AccountTypeAdded, AddressOf RefreshAccountTypes
f.ShowDialog(Me)

When rebinding, avoid Items.Clear() on a data-bound combobox — that can leave the control in an inconsistent state. Instead set cboType.DataSource = Nothing (or use a BindingSource) and then assign a fresh DataTable/BindingSource. Preserve selection by saving SelectedValue before rebind and restoring it afterward if needed.

Quick checklist:

  • Make sure the popup actually commits and closes the DB connection before the parent re-queries.
  • Confirm you are calling the refresh on the displayed parent instance (use Owner, pass the parent in the constructor, or use the event pattern above). Creating a new parent instance and calling its method will not update the visible form.
  • If the insert runs on a background thread, marshal the rebind call to the UI thread (Invoke/BeginInvoke).
  • If you prefer modal flow, use If popup.ShowDialog() = DialogResult.OK Then RefreshAccountTypes() so the parent refreshes after the dialog closes.

Following the event/owner approach and reassigning DataSource cleanly will solve the usual causes.

Recommended Answers

All 2 Replies

i have used something as easy as in the click event where you add your data on the second form just add

frmOther.refresh

I tried that and also on the Public sub that is in the parent form I put it there as well, but it is still not working...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.