Insert Save data in database using asp.net


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


Download full source code here: Insert Save data in database using asp.net
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 asp.net 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.

 

<connectionStrings>
    <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.

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

 

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 asp.net</h1>
        <asp:Label ID="lblmessage" runat="server" Text=""></asp:Label><br />
        Name:
        <asp:TextBox ID="txtname" runat="server"></asp:TextBox> <br />
        Degree:
        <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);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
// 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

Comments


{{cmt.Item1}}


{{cmt.Item2}}

{{cmt.Item3.slice(0,9)}}


ADS

Tags:

insert data, save data, database, ado.net, asp.net, sql command, query,connectionstnig