title: How to Establish database(sql) Connection to receive the data from textfields using c#?
body: <p>I want to establish the database connection for receiving the data from the Textfields and store that data in database records.
  For that, up to now I have tried:</p>
  
  <p>I have created the <code>.mdf</code> database files and in that I have created the table with name as <code>Table1</code> and I have placed the two textfields and submit button, with the following code :
  <strong>data.aspx</strong></p>
  
  <pre><code>&lt;b&gt;Username:&lt;asp:TextBox ID="TextBox1" runat="server" BackColor="AliceBlue"&gt;
  &lt;/asp:TextBox&gt;&lt;br/&gt;
  &lt;b&gt;Lastname:&lt;asp:TextBox ID="TextBox2" runat="server" BackColor="AliceBlue"&gt;
  &lt;/asp:TextBox&gt;&lt;br/&gt;
  &lt;asp:Button ID="Button1" runat="server" Text="Submit" OnClick="SubmitBtn_Click"/&gt;
  </code></pre>
  
  <p>and the code file is as follows:
  <strong>data.aspx.cs</strong></p>
  
  <pre><code>using System.Web.Configuration;
  using System.Data.SqlClient;
  protected void SubmitBtn_Click(object sender, EventArgs e)
  {
      string connectionStrings = "Data Source=|SQLEXPRESS;Integrated                       
     Security=True; Connect Timeout=30;User Instance=True;";
  
      using (SqlConnection sqlConnection = new SqlConnection(connectionStrings))
  {
      string insertStatement = "INSERT INTO Table1(column1,column2) 
                                VALUES (@col1, @col2)";
      SqlCommand sqlCommand = new SqlCommand(insertStatement, sqlConnection);
      sqlCommand.Parameters.AddWithValue("@col1", TextBox1.Text);
      sqlCommand.Parameters.AddWithValue("@col2", TextBox2.Text);
      sqlConnection.Open();
      sqlCommand.ExecuteNonQuery();
      try
      {
          sqlConnection.Open();
          sqlCommand.ExecuteNonQuery();
  
      }
      finally
      {
          sqlConnection.Close();
      }
  }
  
  
  }
  </code></pre>
  
  <p>and I have also the configuration files as follows to establish connection the code for that is as follows:
  <strong>web.config</strong></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
  &lt;system.web&gt;
      &lt;compilation debug="true" targetFramework="4.0" /&gt;
  
  &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>but for that code I am getting this error after clicking the submit button:</p>
  
  <blockquote>
    <p>A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)</p>
  </blockquote>
  
  <p>and also the exception:</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)</p>
  </blockquote>
  
  <p>Can any one help me?</p>