c# - The variable name '@ArrivedDate' has already been declared. Variable names must be unique within a query batch or stored procedure -


private void btnsave_click(object sender, eventargs e)     {         sqlconnection connection = new sqlconnection("data source=nla-hp;initial catalog=gtbdb;persist security info=false;user id=sa;password=dbadmin");          sqlcommand command = new sqlcommand("insert machine values (@branchname, @machinename, @arriveddate, @faultdetail, @returndate, @remark, @technician)", connection);         connection.open();          try         {             //variables declaration             string branchname = cbbranches.text;             string machinename = txtbmachine.text;             datetime arriveddate = dtparrive.value;             string faultdetail = rtxtbfault.text;             datetime returndate = dtpreturn.value;             string remark = rtxtbremark.text;             string technician = txtbtechnician.text;              //add values             command.parameters.addwithvalue("@branchname", branchname);             command.parameters.addwithvalue("@machinename", machinename);             command.parameters.addwithvalue("@arriveddate", arriveddate);             command.parameters.addwithvalue("@faultdetail", faultdetail);             command.parameters.addwithvalue("@returndate", returndate);             command.parameters.addwithvalue("@remark", remark);             command.parameters.addwithvalue("@technician", technician);              command.parameters.add("arriveddate", sqldbtype.datetime);             command.parameters.add("returndate", sqldbtype.datetime);              command.executenonquery();              messagebox.show("successfully write database", "write database", messageboxbuttons.ok, messageboxicon.information);             connection.close();         }         catch (exception ex) //catch exception         {             messagebox.show(ex.message);             connection.close();         }     } 

well, add 2 parameters twice:

first time

command.parameters.addwithvalue("@arriveddate", arriveddate); command.parameters.addwithvalue("@returndate", returndate); 

second time

command.parameters.add("arriveddate", sqldbtype.datetime); command.parameters.add("returndate", sqldbtype.datetime); 

to give parameter sql type , assign value in 1 go, use:

command.parameters.add("arriveddate", sqldbtype.datetime).value = arriveddate; command.parameters.add("returndate", sqldbtype.datetime).value = returndate; 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -