Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > updating a combobox
 

updating a combobox

Hello,

I'm currently developping a vb.net application (vs2008) which works on an Access data base.
I add my data base using the wizard
Afterwards, I drag and drop some fields on my form (on on this field called product is a combobox)
in my combobox I can select which product I want includ and it works without problem
I add a button besides this combobox called "add an product" which open an other form to add a new product
but when I close this form, the new product created does not appear in this combobox which is normal.
But I have tried many times to update, this combo, dataset... on close form events, click on a button "update".... in vain
Someone has an idea how to figure out this issue?

best regards



yo57  Wednesday, March 11, 2009 2:39 PM
Hi yo57,

First, when you add one product in the Form, please check out your database if the record has been added to the database? If not, make sure you call tableadapter.Update method to update the changes to the database. If the record has been added to the database, but the comboBox doesn't show the newly added records, please use the tableadapter.Fill method to re-fill the datatable or dataset which is bound to the ComboBox.

If you have anything unclear, please feel free to let me know.

Best regards,
Bruce Zhou

Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Thursday, March 12, 2009 2:22 PM
Hi yo57,

First, when you add one product in the Form, please check out your database if the record has been added to the database? If not, make sure you call tableadapter.Update method to update the changes to the database. If the record has been added to the database, but the comboBox doesn't show the newly added records, please use the tableadapter.Fill method to re-fill the datatable or dataset which is bound to the ComboBox.

If you have anything unclear, please feel free to let me know.

Best regards,
Bruce Zhou

Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Thursday, March 12, 2009 2:22 PM
thanks for your help,
Indeed, I manage to figure out this issues by clearing the dataset and refill the table again.
thanks again.
yo57  Friday, March 13, 2009 10:39 AM
Hi yo57,

You are welcome. Glad to help you.

Best regards,
Bruce Zhou

Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, March 13, 2009 10:45 AM
hi. i am having a similar problem but the problem is i cannot even add in new items to the combobox. how do you do it?
clueless123  Wednesday, July 08, 2009 5:17 AM
Hi,

Actually, I bound item to a table of a dataset, but if you want to add item manually, on the combobx, there is a small arrow, click on it and it is written "edit items"
If you want to add items with some codes type:

ComboBox1.Items.Add(

"test")

supposing that your combobox is named combobox1

yo57  Wednesday, July 08, 2009 3:20 PM
hmm thanks for the help . how do you bound item to a table of dataset ? sorry i am very new in this. ermm actually i tried using combobox1.items.add but the error i got was i bind it to datasource so i was not allow to add in new item.

i am doing this interface which allow user to add into the combobox by clicking a add new item something like that . do you have any idea?
clueless123  Thursday, July 09, 2009 4:02 AM

Check out this video, it presents how to create a lookup list based on a data set
http://msdn.microsoft.com/en-us/vbasic/bb643829.aspx

Then, if I want to add new item, I have to add new record in my data base and then update my data set.

So create the lookup as it is described,
create a button besides "add new value" then create a pop up form,
-this pop up form will insert new value in the data base
-besides the combobox create a button "update list" and put the code

MyDataSet.Clear()

MyTableAdapter.Fill(MyDataSet.Tables(0))

at least, it is how I've done

good luck

yo57  Thursday, July 09, 2009 9:21 AM

Not sure if this will answer your question, but this is how you would use a DataTable to fill a ComboBox:

MyComboBox.DisplayMember = "MyDescription";
MyComboBox.ValueMember = "Code"
MyComboBox.DataSource = MyDataSet.Tables["MyCodeTable"];

Does this help?


~~Bonnie Berent [C# MVP]
BonnieB  Sunday, July 12, 2009 4:54 AM
hi yo57, thanks i tried using INSERT INTO and values to insert into database but any idea why it does not show any changes when i check into database. and BonnieB, i tried using that before but now i want to update the combobox by adding new thing and at the same time update my database.
clueless123  Monday, July 13, 2009 1:49 AM
Clueless, you're going to have to post some of your code. It will help us figure out what you're doing wrong. Thanks.
~~Bonnie Berent [C# MVP]
BonnieB  Monday, July 13, 2009 4:23 AM
okay this is what i did
i have a textbox which shows my column2 which is bind to my column1 in database. Thus when i edit my textbox, i realise that my database does not update after putting the codes below and not only that when i close my form and open again everything goes back to before i edit.

oledbcommand cmd = new oledbcommand();
this.oledbdataAdapter.Updatecommand.commandText = UPDATE Table Set Description = @Descrip WHERE ID=@ID;
cmd.parameters.Add("@Descrip", Oledbtype.VarChar).Value = Textbox1.text;
cmd.parameters.Add("@ID", Oledbtype.VarChar).Value = Textbox2.text;
this.oledbconnection1.open();
this.oledbdataAdapter.Updatecommand.Connection = oledbconnection1;
this.oledbdataAdapter.updatecommand.ExecuteNonQuery();
this.Oledbconnection1.close()

is the way i put my code wrong? thanks for the help
clueless123  Monday, July 13, 2009 7:54 AM
You don't even need a DataAdapter here (those are primarily used for Filling DataSets). Mixing in the command with the DataAdapter, you probably missed something ... it looks like you never got the parameter hooked in correctly. Anyway, try this instead:

OleDbCommand cmd = new OleDbCommand();
cmd.Connection = this.oledbconnection1;
cmd.CommandText = UPDATE Table Set Description = @Descrip WHERE ID=@ID;
cmd.Parameters.AddWithValue("@Descrip", Textbox1.text);
cmd.Parameters.AddWithValue("@ID", Textbox2.text);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close()


~~Bonnie Berent [C# MVP]
BonnieB  Tuesday, July 14, 2009 3:50 AM

You can use google to search for other answers

Custom Search

More Threads

• Set and Get Files
• BindingSource Dispose() error Custom Handler cannot be converted
• How do I disable BindingSource (ControlUpdate) based on a criteria
• datagridtablestyles
• Datagrid
• C# Rename a DataTable Column (viewed by a DataGridView)
• Binding Control to Composite Key
• Need help updating data to multiple tables
• How to set forecolor to individual cells of datagridview ctrl based on their val
• DataGridView SelectedRows