Stupid Question!!

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

I am having a heck of a time trying to refresh data in a combobox. I have a
combo box that periodically needs to have its bound data refreshed. I can
refresh the data, but every time I refresh it the updated contents of my
data source is added to the combo and the existing list is not cleared.

I have tried calling Items.Clear() before the bind and this didn't work. It
told me I couldn't call it when .DataSource was set, so I set it to Nothing
and then called Items.Clear(). Still nothing.

I know that I am probably missing something blatantly obvious! Does anybody
have any suggestions?

Thank You

Brian
 
Brian,

Can you show us some code (including the method declaration) where you do
this?

Cor
 
The following lines should clear it:

myCombo.DataSource = Nothing
myCombo.ValueMember = String.Empty
myCombo.DisplayMember = String.Empty


After that just rebind it again. If this doesn't work
you'll need to show your code.

/claes
 
Thank you for the replies!

As it turns out, I don't think it is my combo causing the problem. I think
it is my dataset.

Here is the scenario. I have a form with a combo on it. Beside the combo is
a button that displays a dialog that will add a record to the table that my
combo is bound to. On closing the dialog I Insert the new record into the
database using an Insert statement and .ExecuteNonQuery().

When the dialog is close, I refresh my dataset and rebind the combo. When I
refresh the dataset, the database table contents is appended to the end of
the dataset table. The second set of data does contain the new record.

Here is an example:

myCnn.Open()
cmd = new SQLCommand("Select * from tblUOM Order By fldName")
adapt = new SQLDataAdapter(cmd, myCnn)
adapt.fill(myDataSet, "tblUOM")
myCnn.Close()

MsgBox(myDataSet.Tables("tblUOM").Rows.Count.toString())

The actual code I was working on is at home, so that is off the top of my
head. It is pretty straight forward, nothing out of the ordinary. When it
runs through the first time, My message box displays 4, but the second time
it displays 9.
The contents of my combo go from {1,2,3,4} to {1,2,3,4,1,2,3,4,5}. I have
done this same thing in ASP.Net using C#, and the dataset updates like I
would expect. This time I am using VBExpress and SQLExpress. Is that were
the bug lies or is it something I am doing?

Shouldn't the dataset just update on the second fill or do I have to clear
the table in the dataset and refill it (which does work by the way)?

Anyway, being a bit long winded and probably rambling at this point.

Thanks

Brian
 
Brian,

What we don't see is how you bind that dataset to the combobox.

Normal this will be

myCombo.DataSource = myDataset.Tables("tblUOM")
myCombo.ValueMember = "TheValueColumn"
myCombo.DisplayMember = "TheDisplayColumn"

Do you do it like this, and than where

Cor
 
I am binding the combo box right after the msgbox in my last example and
doing so exactly as you showed.

I have refocused my attention on the dataset however. The msgbox is telling
my I have 9 rows and not 4, my combobox is displaying the bound source
correctly.

Not sure why it isn't refreshing the dataset properly
 
Once when the form loads and then again when the database changes. (Which is
triggered by user action on the form, popping up a dialog to add a new Unit
type. Which may or may not happen at all)
 
Brian,

Can you try what happens clear the dataset before that fill than.

ds.clear

Cor
 
Cor,

I have done a ds.tables["tblUOM"].Clear() and it works.
Maybe this is the route that I have to go, even thought it is supposed to
work the other way too!

Thanks,

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top