c# - How to update a bound combo box after updates are made to database -


i have sql server express database containing medication info. have created windows form display info on selected medication. combobox on form bound db using following:

string sqlquery = "select * meds"; cmd = new sqlcommand(sqlquery, conn);  sqlda.selectcommand = cmd; sqlda.fill(medsdt);  cbomeds.datasource = medsdt; cbomeds.displaymember = "medname"; cbomeds.valuemember = "id"; 

the form has textbox ("adjamt") allows user change quantity ("supply") of selected med entering new quantity , clicking on "change" button.

the database updated following code:

sqlcommand cmdupdate = new sqlcommand(); string cmdtxt = "update meds set supply = '"; cmdtxt += adjamt.tostring() + "' id = '"; cmdtxt += lblid.text.tostring() + "';"; cmdupdate.commandtext = cmdtxt; cmdupdate.connection = conn;   cmdupdate.executenonquery(); 

at point working designed, e.g. database updated. after update, return user form , expect find updated value on form doesn't. checking database, find new "supply" value. tried several options found on stackoverflow none seem work.

this should simple operation reason, isn't. of options i've tried include clearing , reloading combobox:

cbomeds.datasource = null; string sqlquery = "select * meds"; cmd = new sqlcommand(sqlquery, conn); sqlda.selectcommand = cmd; sqlda.fill(medsdt); cbomeds.datasource = medsdt; cbomeds.displaymember = "medname"; cbomeds.valuemember = "id"; 

this fails because there selectedindexchanged event triggered when try change cbomeds.datasource property. event setup repopulate form when item selected in combobox when try delete cbomeds.datasource, fires event throws error: "indexoutofrangeexception" because there nothing in combobox after setting combobox.datasource null. closing form , re-executing resets combobox expected.

isn't there simple way update combobox when bindingsource changes?


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -