c# - Insert Data from text box to database asp.net MVC4 -
i using mvc4 view page , trying insert data ms sql database text boxes clicking button. struggling , guess problem "con.open()",but not know declare it. here code:
<%@ page language="c#" masterpagefile="~/views/shared/site.master" inherits="system.web.mvc.viewpage" %> <script runat="server"> sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["greenenergyconnectionstring1"].connectionstring); protected void button1_click(object sender, eventargs e) { con.open(); sqlcommand cmd = new sqlcommand("insert customer values ('"+textbox2.text+"', '"+textbox3.text+"', '"+textbox4.text+"','"+textbox5.text+"','"+textbox6.text+"', '"+textbox7.text+"', '"+textbox8.text+"')",con); cmd.executenonquery(); con.close(); textbox8.text = ""; textbox7.text = ""; textbox6.text = ""; textbox5.text = ""; textbox4.text = ""; textbox3.text = ""; textbox2.text = ""; }
and @ web.config declared following:
<add namespace="system.data.sqlclient"/> <add namespace="system.configuration"/>
what have above not mvc. it's webforms. <script runat="server">
tag dead give away. code should in controller action. when user clicks html submit in html form mvc route request controller action. within action write code execute sql.
i highly recommend razor on webforms syntax. it's cleaner , easier work with.
Comments
Post a Comment