c# - Unable to insert mutiple values to database -
i need on this: trying insert multiple values database using webpage interface. when try save error
"incorrect syntax near keyword 'values'."
below c# code reference.
using system; using system.configuration; using system.data; using system.linq; using system.web; using system.web.security; using system.web.ui; using system.web.ui.htmlcontrols; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.xml.linq; using system.data.sqlclient; public partial class _default : system.web.ui.page { sqlconnection con = new sqlconnection("data source=inhrpsm1d7c;initial catalog=controller_forecast;integrated security=true;"); protected void page_load(object sender, eventargs e) { } public void refress() { textbox1.text = ""; textbox2.text = ""; textbox3.text = ""; textbox4.text = ""; textbox5.text = ""; textbox6.text = ""; textbox7.text = ""; textbox8.text = ""; textbox9.text = ""; textbox10.text = ""; textbox11.text = ""; textbox12.text = ""; dropdownlist1.text = ""; dropdownlist2.text = ""; } protected void btnsave_click(object sender, eventargs e) { sqlcommand cmd = new sqlcommand("insert controller_forecast(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13) values('" + textbox1.text + "','" + textbox2.text + "','" + textbox3.text + "','" + textbox4.text + "','" + textbox5.text + "','" + textbox6.text + "','" + textbox7.text + "','" + textbox8.text + "','" + textbox9.text + "','" + textbox10.text + "','" + textbox11.text + "','" + textbox12.text + "','" + dropdownlist1.text + "','" + dropdownlist2.text + "','" + dropdownlist3.text + "'),values('" + textbox13.text + "','" + textbox14.text + "','" + textbox15.text + "','" + textbox16.text + "','" + textbox17.text + "','" + textbox18.text + "','" + textbox19.text + "','" + textbox20.text + "','" + textbox21.text + "','" + textbox22.text + "','" + textbox23.text + "','" + textbox24.text + "','" + dropdownlist4.text + "','" + dropdownlist5.text + "','" + dropdownlist6.text + "')",con); cmd.commandtype = commandtype.text; try { con.open(); cmd.executenonquery(); literal1.text = "data updated successfully!!!"; con.close(); refress(); } catch (exception ex) { literal1.text = ex.message; } } protected void button1_click(object sender, eventargs e) { refress(); literal1.text = ""; } }
you can't 2 sets of values trying insert statement. doing:
insert controller_forecast(c1,c2...) values(...loads of values...) values(...loads of more values...)
this isn't valid. insert 2 sets of data, looks you're trying can 2 insert statements or split list of values commas so:
multiple insert statements
insert controller_forecast(c1,c2...) values(...your first set of values...) insert controller_forecast(c1,c2...) values(...your second set of values...)
separating commas
insert controller_forecast(c1,c2...) values(...your first set of values...), (...second set of values...)
however, seems inefficient , you'd better off writing in store procedure , parameterising values avoid sql injection.
Comments
Post a Comment