Insert,Update and Delete data in GridView using the Database (SQL)

Step 1:  Firstly open the Sql server and create the database and table in it,The table must have a primary key for which we can update and delete the data in gridview.

SQL.jpg

Step 2 : Now open the Visual Studio 2010 and click File->New->Web site->Select the ASP.net website and then click ok.

starting_project.jpg

Step 3: In The designing view add buttons,textboxes,gridview,labels as shown in the following figure.

Design_page.jpg

Step 4: Double click on the page and write the following code in it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=.;database=db;uid=sa;pwd=wintellect");
    SqlDataAdapter
da;
    SqlCommand
cmd;
    DataSet
ds = new DataSet();
    SqlDataReader
dr;
    protected
void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
       show();
      }
    }
    public
void show()
    {
      da = new SqlDataAdapter("select * from employee", con);
      da.Fill(ds);
      GridView1.DataSource = ds;
      GridView1.DataBind();
     }
}

Step 5: Double click on the insert button and write the following code in the button click event.

protected void Button1_Click(object sender, EventArgs e)
{
    string
str = "insert into employee values(@emp_id,@emp_name,@emp_salary)";
    cmd = new SqlCommand(str, con);
    cmd.Parameters.AddWithValue("@emp_id", TextBox1.Text);
    cmd.Parameters.AddWithValue("@emp_name", TextBox2.Text);
    cmd.Parameters.AddWithValue("@emp_salary", TextBox3.Text);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    show();
    Response.Write("Record Inserted");
}

Step 6: Double click on the update button and write the following code in the button click event.

protected void Button2_Click(object sender, EventArgs e)
{
    string
str = "update employee set emp_name=@emp_name,emp_salary=@emp_salary where emp_id=@emp_id";
    cmd = new SqlCommand(str, con);
    cmd.Parameters.AddWithValue("@emp_id", TextBox1.Text);
    cmd.Parameters.AddWithValue("@emp_name", TextBox2.Text);
    cmd.Parameters.AddWithValue("@emp_salary", TextBox3.Text);
    con.Open();
    dr = cmd.ExecuteReader();
    con.Close();
    show();
    Response.Write("Record Updated");
}

Step 7: Double click on the Delete button and write the following code in the button click event.

protected void Button3_Click(object sender, EventArgs e)
{
    string
str = "delete from employee where emp_id=@emp_id";
    cmd = new SqlCommand(str, con);
    cmd.Parameters.AddWithValue("@emp_id", TextBox1.Text);
    con.Open();
    dr = cmd.ExecuteReader();
    con.Close();
    show();
    Response.Write("Record Deleted");
}

Step 8: Press F5 and run the application,Insert the values in the textboxes,and click Insert Button, the values are inserted.click on the Update button,the values are updated based on id.Click on the Delete button the record is deleted again based on id in which I applay the primary key.


The Output is as follow.

Output.jpg

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now