title: Insert Data from text box to database asp.net MVC4
body: <p>i am using an MVC4 view page and i am trying to insert data to my MS SQL database from text boxes by clicking a button. I am struggling to do so and i guess that the problem is "con.open()",but i do not know where to declare it. Here is my code:</p>
  
  <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
  Inherits="System.Web.Mvc.ViewPage" %&gt;
  
  &lt;script runat="server"&gt;
  
  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["GreenEnergyConnectionString1"].ConnectionString);
  
     protected void Button1_Click(object sender, EventArgs e)
  {
      con.Open();
      SqlCommand cmd = new SqlCommand("insert into 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 = "";
  
  }
  </code></pre>
  
  <p></p>
  
  <p>And at my web.config i declared the following:</p>
  
  <pre><code>&lt;add namespace="System.Data.SqlClient"/&gt;
  &lt;add namespace="System.Configuration"/&gt;
  </code></pre>