c# - Inserting multiple listitems of dropdownlist into multiple rows of the database -


i trying insert multiple selected values of asp.dropdownlist multiple rows of database. using silvio montero bootstrap select css acheive multiple selection.

i using foreach() loop first selected item getting inserted database. asp.dropdownlist populated database.

<asp:listbox runat="server" cssclass="selectpicker form-control" multiple data-live-search="true" id="dropdownlist1" datatextfield="username" datavaluefield="username" >      </asp:listbox> 

here c# code -

protected void page_load(object sender, eventargs e)     {         if (!ispostback)         {             binddropdownlist();         }     }     private void binddropdownlist()     {          using (sqlconnection con = new sqlconnection(cs))              using (sqlcommand cmd = new sqlcommand("select distinct [username], [email] [login]", con))             {                 sqldataadapter da = new sqldataadapter(cmd);                 dataset ds = new dataset();                 da.fill(ds);                  dropdownlist1.datasource = ds;                 dropdownlist1.datatextfield = "username";                 dropdownlist1.datavaluefield = "username";                 dropdownlist1.databind();             }      }       protected void sendbutton_click(object sender, eventargs e)     {         foreach (listitem item in dropdownlist1.items)         {              if (item.selected)             {                 using (sqlconnection sqlcon = new sqlconnection(cs))                 {                     sqlcon.open();                     using (sqlcommand cmd = new sqlcommand())                     {                         cmd.connection = sqlcon;                         cmd.commandtype = commandtype.text;                         cmd.commandtext = "insert db (schdlby, schdldate, schdltime, sub, event, participants ) values (@schdlby, @schdldate, @schdltime, @sub, @event, @participants)";                         cmd.parameters.addwithvalue("@schdlby", page.user.identity.name.tostring());                         cmd.parameters.addwithvalue("@schdldate", cl.selecteddate.toshortdatestring());                         cmd.parameters.addwithvalue("@sub", txtsub.text);                         cmd.parameters.addwithvalue("@schdltime", schdltime.text.trim());                         cmd.parameters.addwithvalue("@event", txtevent.text);                         cmd.parameters.addwithvalue("@parti", item.value.tostring());                         cmd.executenonquery();  // code send automated smtp mail each dropdown selection  }                 }             }               }          response.redirect("second.aspx");     } 

edit: found works in asp:checkboxlist not work when asp:listbox used


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -