Easy Steps to Insert Data into SQL Server using ASP.Net(c#)

1. Open SQL Server management studio and create database named TEST. Then add a table with two columns named name and fathername. we'll add data into name and fathername using asp.net.
2. Now open visual studio and create new website. Then add new webform.
3. Now add two text boxes and a button from toolbox.
4. Double click on button .it'll open codebehind file of webform.
5. Now we have to add connection string of the database. Save your connection string in web.config file

<configuration>

  <connectionStrings>

    <add name="myconectionstring"

    connectionString="data source=.\SQLEXPRESS;initial catalogue=your database name;Integrated Security=SSPI";providerName="System.Data.SqlClient" />

  </connectionStrings>

  <system.web>

    <compilation debug="false" targetFramework="4.0" />

    <authentication mode="Forms">

      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />

    </authentication>

    <membership>

6. Now we'll get this connection string in our codebehind file.

string cs = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;

7. Now we'll create sqlconnection .Add namespace using System.data.sqlclient at top.Now write following code in button click event.

string cs=System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;

SqlConnection con = new SqlConnection(cs);

SqlCommand cmd = new SqlCommand("INSERTINTO TEST (name,fathername) VALUES('" + TextBox1.Text + "','" + TextBox1.Text + "')", con);

con.Open();

cmd.ExecuteNonQuery();

con.Close();

8. Its done.