c# - Insert more than one item from checkedlistbox to AccessDB -
c# - Insert more than one item from checkedlistbox to AccessDB -
i have listcheckedbox lists employee numbers , want able add together each employee attended training class show under training session. however, when seek submit info insert db, add together 1 of employees selected. how can have submit more 1 employee class session?
try { string cmdstring = "insert [session] (principlename, comments, sessiondate, sessionname, tellernum) values (@principle, @comments, @date, @session, @teller)"; using (oledbcommand cmd = new oledbcommand(cmdstring, con)) { cmd.parameters.addwithvalue("@principle", combobox12.text); cmd.parameters.addwithvalue("@comments", textbox3.text); cmd.parameters.addwithvalue("@date", datetimepicker1.value); cmd.parameters.addwithvalue("@session", combobox1.text); cmd.parameters.addwithvalue("@teller", checkedlistbox1.text); con.open(); cmd.executenonquery(); con.close(); messagebox.show("submitted successfully"); } } grab (exception ex) { messagebox.show("failed due " + ex.message); }
here updated code after reply larstech answer
con.open(); string cmdstring = "insert [session] (principlename, comments, sessiondate, sessionname, tellernum) values (@principle, @comments, @date, @session, @teller)"; foreach (int s in checkedlistbox1.checkeditems) { using (oledbcommand cmd = new oledbcommand(cmdstring, con)) { cmd.parameters.addwithvalue("@principle", combobox12.text); cmd.parameters.addwithvalue("@comments", textbox3.text); cmd.parameters.addwithvalue("@date", datetimepicker1.value.toshortdatestring()); cmd.parameters.addwithvalue("@session", combobox1.text); cmd.parameters.addwithvalue("@teller", s); cmd.executenonquery(); } } con.close(); messagebox.show("submitted successfully"); textbox3.clear(); checkedlistbox1.clearselected(); combobox1.refresh(); combobox12.refresh(); datetimepicker1.refresh();
you have iterate on checkeditems collection:
foreach (string s in checkedlistbox1.checkeditems) { using (oledbcommand cmd = new oledbcommand(cmdstring, con)) { cmd.parameters.addwithvalue("@principle", combobox12.text); cmd.parameters.addwithvalue("@comments", textbox3.text); cmd.parameters.addwithvalue("@date", datetimepicker1.value); cmd.parameters.addwithvalue("@session", combobox1.text); cmd.parameters.addwithvalue("@teller", s); cmd.executenonquery(); } }
c# sql
Comments
Post a Comment