c# - SELECT query using SqlParameter isn't working -
i'm working on select query sql in c#. trying use sqlparameter
, when debug, throw exception:
must declare scalar variable "@p1".
i'm passing values in via textbox properties in class customer
. in event handler:
var newcustomer = new customer(); newcustomer.firstname = textboxfirstname.text; newcustomer.lastname = textboxlastname.text; newcustomer.address = textboxaddress.text; newcustomer.city = textboxcity.text; newcustomer.state = textboxstate.text; newcustomer.zip = int32.parse(textboxzip.text); newcustomer.phone = int64.parse(textboxphone1.text); newcustomer.notes = textboxnotes.text; var crochetdatahandler = new crochetdatahandler(); crochetdatahandler.insertnewcustomer(newcustomer);
and method i'm calling:
public void insertnewcustomer(customer customer) { var insertcustomerstring = "insert customer1 (firstname, lastname, address, city, state, zip, phone, notes) values (@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8)"; sqlcommand newcust = new sqlcommand(insertcustomerstring); newcust.parameters.add(new sqlparameter("@p1", customer.firstname)); newcust.parameters.add(new sqlparameter("@p2", customer.lastname)); newcust.parameters.add(new sqlparameter("@p3", customer.address)); newcust.parameters.add(new sqlparameter("@p4", customer.city)); newcust.parameters.add(new sqlparameter("@p5", customer.state)); newcust.parameters.add(new sqlparameter("@p6", customer.zip)); newcust.parameters.add(new sqlparameter("@p7", customer.phone)); newcust.parameters.add(new sqlparameter("@p8", customer.notes)); newcust.commandtext.tostring(); insertrecord(insertcustomerstring);
i have seen few different constructors used sqlparameter
in other questions, when try them same exception.
i'm not sure insertrecord method doing, you're passing original string insertcustomerstring rather sqlcommand object. string knows nothing you've done parameters, if insertrecord expecting string know parameters' values it'll disappointed.
Comments
Post a Comment