c# - Adding from a Listbox to database -


i have text box introduce number , button named add introduce number in list box. after adding numbers wanted when click on button finalize add in list box in database.

my database table number id idnumber name.

idnumber = when add multiple numbers list box in database want numbers have same id.

for example in list have

  • 45341
  • 5466
  • 4646
  • 464

and in database have

     id   idnumber     name                          1        1        45341                        1        1         5466                   1        1        4646                    1        1         464 

and next time add list box , next number have idnumber 2.

    <asp:textbox id="txtnrshipment" runat="server" height="16px"  width="100px"></asp:textbox> <asp:button id="btnaddso" runat="server" text="add" onclick="btnaddso_click" causesvalidation="false" />  <asp:listbox id="listboxso" runat="server">                                </asp:listbox>   <asp:button id="btnfinalizeso" runat="server" text="finalize" onclick="btnfinalizeso_click" />      protected void btnaddso_click(object sender, eventargs e)     {         listboxso.items.add(txtnrshipment.text);         txtnrshipment.text = " ";     } 

you have not provided actual code of btnfinalizeso's click event. if not have yet, first step must implement connection database, example entity framework. if connect entity framework create objects of table database inject them db. example:

protected void btnfinalizeso_click(object sender, eventargs e) {     using(mydatabaseentities db = new mydatabaseentities())    {        number lastnumber = (from c in db.number orderby idnumber select c).lastordefaul();        foreach(var item in listboxso.items)        {              number n = new number();             n.idnumber = lastnumber.idnumber +1;              n.id = lastnumber.id + 1;             n.name = listboxso.getitemtext(item);             db.number.add(n);        }       db.savechanges();    } } 

this more or less need. may catch exceptions.


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -