---
title: "How to insert update delete data from database using c#."  
description: "How to insert update delete data from database using c#."  
author: "Anonymous User"  
published: 2016-09-28  
updated: 2016-09-28  
canonical: https://www.mindstick.com/forum/34184/how-to-insert-update-delete-data-from-database-using-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to insert update delete data from database using c#.

I want to [insert update delete](https://www.mindstick.com/blog/448/store-procedure-is-used-to-insert-and-update-data) [data from database](https://www.mindstick.com/forum/34370/how-to-populate-google-charts-using-data-from-database-created-via-data-first-asp-dot-net-mvc) using c#.please help me.

## Replies

### Reply by Anonymous User

.

```
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;
using System.Configuration;
namespace testing_all_controls
{
    public partial class Gridviewes : System.Web.UI.Page
    {
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                filldata();
            }
        }

        private void filldata()
        {
            string sqlrt = "";
            sqlrt = "select id,name,age from test";
            cn.Open();
            SqlDataAdapter da = new SqlDataAdapter(sqlrt, cn);
            DataSet dt = new DataSet();
            da.Fill(dt);
            if (dt.Tables[0].Rows.Count == 0)
            {
                DataRow dr = dt.Tables[0].NewRow();
                dr["id"] = "No Record";
                dr["name"] = "No Record";
                dr["Age"]="No Record";
                   dt.Tables [0].Rows .Add (dr);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
            cn.Close();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Add")
            {
                TextBox txtname = (TextBox)GridView1.FooterRow.FindControl("txtname") as TextBox;
                TextBox txtage = (TextBox)GridView1.FooterRow.FindControl("txtage") as TextBox;
                string sqlrt = "insert into test(id,name,age) values('" + Guid.NewGuid().ToString().Substring(1, 6) + "','" + txtname.Text + "','" + txtage.Text + "')";
                SqlCommand cmd = new SqlCommand();
                cn.Open();
                cmd.CommandText = sqlrt;
                cmd.Connection = cn;
                cmd.ExecuteNonQuery();
                cn.Close();
                filldata();
            }

            if (e.CommandName == "Change")
            {
                int index = Convert.ToInt32(e.CommandArgument.ToString());
                GridView1.EditIndex = index;
                filldata();
            }

            if (e.CommandName == "Cancels")
            {
                GridView1.EditIndex = 0;
                filldata();
            }

            if (e.CommandName == "Save")
            {
                int index = Convert.ToInt32(e.CommandArgument.ToString());
                TextBox txtname = (TextBox)GridView1.Rows[index].FindControl("txtename") as TextBox;
                TextBox txtage = (TextBox)GridView1.Rows[index].FindControl("txteage") as TextBox;
                string id=Convert .ToString ( GridView1.DataKeys[index ].Value );
                string sqlrt = "update   test set name='" + txtname.Text + "',age='" + txtage.Text + "' where id='"+id+"'";
                SqlCommand cmd = new SqlCommand();
                cn.Open();
                cmd.CommandText = sqlrt;
                cmd.Connection = cn;
                cmd.ExecuteNonQuery();
                cn.Close();

                GridView1.EditIndex =-1;
                filldata();
            }

             if (e.CommandName == "Remove")
            {
                int index = Convert.ToInt32(e.CommandArgument.ToString());
                string id=Convert .ToString ( GridView1.DataKeys[index ].Value );
                string sqlrt = "delete from test where id='"+id+"'";
                SqlCommand cmd = new SqlCommand();
                cn.Open();
                cmd.CommandText = sqlrt;
                cmd.Connection = cn;
                cmd.ExecuteNonQuery();
                cn.Close();
                filldata();
            }
        }
    }
}
```

![How to insert update delete data from database using c#.](https://www.mindstick.com/mindstickforums/b7589767-c95c-4bbf-b827-c30d297f3074/images/1d0e5fdd-3d5e-4a60-b78e-db370de0fd84.png)\
thanks.


---

Original Source: https://www.mindstick.com/forum/34184/how-to-insert-update-delete-data-from-database-using-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
