How to insert Records in Database using C# Language

Technology

How to insert Records in Database using C# Language

How to insert Records in Database using C# Language
How to insert Records in Database using C# Language

This tutorial is related to insert data in database using web form in c-sharp language. For beginners this tutorial given step by step information about creating web form, write code in c-sharp language,  create a database in SQL server and connect database with your web form.
Let’s start this tutorial, first we know about to requirement for insert records.

Requirement:

Visual Studio 2012 or greater version,
SQL Server 2008 or greater version

Step1: Open Visual Studio 2012. Go to File Menu in the menu bar, Click on New Projects and click on Visual C#, select the web form. Name the project and save this to your computer drive location through click on the browse option in visual studio. You Can Change project name and location later also.

Step 2: After open project in visual studio you have to right click on the solution explorer and add item select web form and name it as you want.

How to create a Database in SQL Server 2008.

Step 3: Open SQL Server 2008, Connect it by servername and username and password, You can also connect your sql server by window authentication service without using username and password.

Step 4: Click on new query and create database. You can create database using command and manual option in sql server. Here I create a database named ‘Test_Demo’. Check the query given below:

create database Test_Demo
use Test_Demo

After create database you must execute query ‘use Test_Demo’.

Step 5: Now i can create a table which holds my records. I create a table named ‘Employee’ having four columns e.g empid, name, city and age. Command is given for create table below.

create table Employee
(
empid int primary key identity,
name varchar(50),
city varchar(50),
age int
)

Step 6: Now we code in web form, we have two pages like aspx and cs page, Aspx page regarding the design page and cs page for c-sharp code. Here we have three textbox control of asp.net and on button for saving records.

Code: 

Employe.aspx

<form id=”form1″ runat=”server”>
    <div>
   Name : <asp:TextBox ID=”txtname” runat=”server”></asp:TextBox>
   City : <asp:TextBox ID=”txtcity” runat=”server”></asp:TextBox>
    Age : <asp:TextBox ID=”txtage” runat=”server”></asp:TextBox>
        <asp:Button ID=”btnsubmit” runat=”server” Text=”Save” OnClick=”btnsubmit_Click” />
    </div>
    </form>

Place this code under head tag of aspx page.

Employe.aspx.cs:

namespace Test11918
{
    public partial class Employee : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(“data source=(server_name);initial catalog=Test_Demo;integrated security=true”);
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand(“insert into Employee(name,city,age)values(‘”+txtname.Text+”‘,'”+txtcity.Text+”‘,'”+txtage.Text+”‘)”,con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

Put this code under head tag of aspx.cs page.

Step 7: Now you can check your web form in browser and start insert records in your database. Also check data in database by execute below query.

“select * from Employee”

Leave a Reply

Your email address will not be published. Required fields are marked *