Insert Save data in database using

Article Author : Tauseef Ahmad, Article Posted on : 8/2/2017 , Article Category :

Download full source code here: Insert Save data in database using
please share this article to help others :

Here i have explained with an example and attached code that how to save (insert) data in database using c#.

i am explaining with very easy step by step process with screenshots and code provided.

There are three steps to follow.

Step 1. Open SQL Server and Create Database with Table (student).

i created database with name TestDemoDB.



now create table with name student. To make ID filed primary key convert (Is Identity) to yes in column property as mention in screenshot.



Step 2. Now open visual studio ( i used vs 2013 ) and create new empty website just see the screenshot.



now give name to website and click OK.



Step 3.  now copy and add the following code in web.config file.


    <add name="conString" connectionString="Data Source=tauseef;database=TestDemoDB; Integrated Security=true"/>
  </connectionStrings >



in connectionstring give your datasource name as i gave my own and you give your datasource name and also in database give your database name.

in case your sql server need sql authentication (userid and password ) than just use the following code other wise in window authentication use above code.

    <add name="SqlConn" connectionString="Data Source=Tauseef;Initial Catalog=TestDemoDB;User Id=sa;Password=password" providerName="System.Data.SqlClient" />


Now add new file (aspx) 


give name to page.



Add the following code to aspx page this code is used for front end where user enter data.


<h1>Welcome to my first learning topic in</h1>
        <asp:Label ID="lblmessage" runat="server" Text=""></asp:Label><br />
        <asp:TextBox ID="txtname" runat="server"></asp:TextBox> <br />
        <asp:TextBox ID="txtdegree" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" />


now open the code behind page and use this code at the top (in namespace).


using System.Data.SqlClient;


here is the main code on submit button click.


 protected void btnsave_Click(object sender, EventArgs e)
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand("insert into student(Name,Degree)Values('" + txtname.Text + "','" + txtdegree.Text + "')", con);
// this code of line just show message and empty textboxes after submitting data..
        lblmessage.Text = "Record Successfully Saved:";
        txtname.Text = "";
        txtdegree.Text = "";


Use the ConnectionStrings property of the ConfigurationManager class to retrieve the connection string value from web.config.

Command statement for sql query.

when completting all the process save and Run the page give value in the field and check in database table.


Related Article

Add your valuable Comments

Success! Comment added successfully.

Name is required

Email is a required field Invalid Email
Message is required







insert data, save data, database,,, sql command, query,connectionstnig