c# - Insert dropdownlist value in database -


i have dropdownlist , have bound table. problem when trying insert selected value of dropdownlist control database, not getting correct values instead getting integer data 2 , 1.

this dropdownlist control:

<asp:dropdownlist   id="ddlproductname"   runat="server"   datatextfield="prod_name"   datavaluefield= "product_id"   > </asp:dropdownlist> 

the code-behind:

protected void page_load(object sender, eventargs e) {   ddlproductname.selectedindex = 0;    if (!ispostback)   {     tbdate.text = datetime.today.toshortdatestring();     string cs = configurationmanager.connectionstrings["dbcs"].connectionstring;     using (sqlconnection con = new sqlconnection(cs))     {       sqlcommand cmd = new sqlcommand("select product_id,prod_name add_product", con);       con.open();       sqldatareader rdr = cmd.executereader();       ddlproductname.datasource = rdr;       ddlproductname.databind();     }   } }  protected void btnpurchase_click(object sender, eventargs e) {   try   {     string custname = tbcustname.text;     string prodname = ddlproductname.selectedvalue;     int quantity = convert.toint32(tbquantity.text);     string date = tbdate.text;     string voucherno = tbvoucher.text;     double amount = convert.todouble(tbamount.text);      purchproduct purch = new purchproduct(custname, prodname, quantity, date, voucherno, amount);     connectionclass.purchaseprod(purch);     lblpurchase.text = "submitted succesfully";   } 

and purchaseprod method follows:

public static void purchaseprod(purchproduct purchase) {   cmd = new sqlcommand("insert tblpurchase(custname, prodname, quantity, date,voucherno,amount) values (@custname, @prodname, @quantity, @date,@voucherno,@amount)", con);    cmd.parameters.addwithvalue("@custname", purchase.custname);   cmd.parameters.addwithvalue("@prodname", purchase.prodname);   cmd.parameters.addwithvalue("@quantity", purchase.quantity);   cmd.parameters.addwithvalue("@date", purchase.date);   cmd.parameters.addwithvalue("@voucherno", purchase.voucherno);   cmd.parameters.addwithvalue("@amount",purchase.amount);    try   {     con.open();     cmd.executenonquery();   }     {     con.close();     cmd.parameters.clear();   } } 

if want product name selected value , should bind datavaluefield not datatextfield


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -