c# - Must declare the scalar variable "@uom" -


i having windows appliation suppose fill purchase order form. has field called uom. inserting using parameterized query. getting error

must declare scalar variable @uom.

please help.

private void btnsave_click(object sender, eventargs e) {     con.open();     if (isallvalid())     {         cmd.commandtext = "insert purchaseorder(req_no,purchaseorder_no,purchaseorder_date,costcenter_id,vendor_id,payment_terms,delivery_schedule,emp_id,commercial_name) values(@req_no,@purchaseorder_no,@purchaseorder_date,@costcenter_id,@vendor_id,@payment_terms,@delivery_schedule,@emp_id,@commercial_name)";         cmd.parameters.addwithvalue("@req_no", cmbreqno.selectedvalue);         cmd.parameters.addwithvalue("@purchaseorder_no", txtpno.text);         cmd.parameters.addwithvalue("@purchaseorder_date", dtpo.value);         cmd.parameters.addwithvalue("@costcenter_id", convert.tostring(txtcc.tag));         cmd.parameters.addwithvalue("@vendor_id", cmbvendorname.selectedvalue);         cmd.parameters.addwithvalue("@payment_terms", txtpaymentterms.text);         cmd.parameters.addwithvalue("@delivery_schedule", txtdeliveryschedule.text);         cmd.parameters.addwithvalue("@emp_id", cmbcontactperson.selectedvalue);         cmd.parameters.addwithvalue("@commercial_name", txtcommercial.text);         cmd.executescalar();           foreach (var item in poitemlist)         {             cmd.commandtext = "insert purchaseorderitem(uom,item_id,quantity,item_cost,total_cost)values(@uom,@item_id,@quantity,@item_cost,@total_cost)";             cmd.parameters.addwithvalue("@item_id", item.item_id.tostring());             cmd.parameters.addwithvalue("@uom", item.uom.tostring());             cmd.parameters.addwithvalue("@quantity", item.quantity.tostring());             cmd.parameters.addwithvalue("@item_cost", item.price.tostring());             cmd.parameters.addwithvalue("@total_cost", item.totalcost.tostring());             cmd.parameters.clear();             cmd.executescalar();         }          messagebox.show("purchase order saved", "purchase order", messageboxbuttons.ok, messageboxicon.information);      }     else     {         messagebox.show("error", "purchase order", messageboxbuttons.ok, messageboxicon.error);     }       con.close(); } 

move line clear parameter collection after executescalar

   cmd.executescalar();    cmd.parameters.clear(); 

by way, if have many items insert, better have storedprocedure, or little optimization moving creation of parameters outside loop , setting value inside loop

cmd.parameters.clear(); cmd.commandtext = "insert purchaseorderitem(uom,item_id,quantity,item_cost,total_cost)" +                    "values(@uom,@item_id,@quantity,@item_cost,@total_cost)";  // create parameters collection fake values  // better use add specific datatype,  // example use addwithvalue cmd.parameters.addwithvalue("@item_id", string.empty); cmd.parameters.addwithvalue("@uom", string.empty); cmd.parameters.addwithvalue("@quantity", string.empty); cmd.parameters.addwithvalue("@item_cost", string.empty); cmd.parameters.addwithvalue("@total_cost", string.empty); foreach (var item in poitemlist) {     cmd.parameters["@item_id"].value =, item.item_id.tostring();     cmd.parameters["@uom"].value = item.uom.tostring();     cmd.parameters["@quantity"].value = item.quantity.tostring();     cmd.parameters["@item_cost"].value = item.price.tostring();     cmd.parameters["@total_cost"].value = item.totalcost.tostring();     cmd.executescalar(); } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -