---
title: "How to Add, Edit, Update Records In asp.net"  
description: "How to Add, Edit, Update Records In asp.net"  
author: "Simond Gear"  
published: 2017-01-03  
updated: 2017-01-03  
canonical: https://www.mindstick.com/forum/34228/how-to-add-edit-update-records-in-asp-dot-net  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# How to Add, Edit, Update Records In asp.net

Hi Guys\
I am [beginner](https://www.mindstick.com/forum/23159/need-beginner-project-ideas) in c# and currently work one of the project.I [am facing](https://answers.mindstick.com/qa/37099/if-i-am-facing-my-thoughts-while-meditating-am-i-meditating-wrong) a [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) to show records in [gridview](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net) and edit and [update records](https://www.mindstick.com/forum/158986/how-to-update-records-using-entity-framework-core) without refersing.Please help me.\
I would really appreciate your help.\
Thanks\
\
\

## Replies

### Reply by Manish Kumar

![How to Add, Edit, Update Records In asp.net](https://www.mindstick.com/mindstickforums/c04dcde8-e0a7-4575-b9cc-df59aa6b27e4/images/bcd7c318-769c-47c6-8b8e-fbd100eaf3d5.png)**make sql table taking id as primary key and identity specification yes****\****then design html form and add ajax code like this**

```
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title><link href="Content/Site.css" rel="stylesheet" />    <script src="Scripts/jquery-1.7.1.min.js"></script>   <style>       span {           width:300px;                 }       h3 {           color:blue;           font-family:Garamond;       }   </style></head><body>        <form id="form1" runat="server">
```

```
        <asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
```

```
  <!--if you want that your page should not flikr during record inseting
```

```
 updating and deleting then you can add this code and form should be inside update panel-->    <asp:UpdatePanel ID="panel1" runat="server"><ContentTemplate>    <div  class="content-wrapper">        <table>            <tr>                <td>                    <span id="s1" runat="server"><asp:Label ID="label1" runat="server">User
Name</asp:Label></span><br />                    <asp:TextBox ID="EmpName" runat="server"/>                     <span id="Span1" runat="server"><asp:Label ID="label2" runat="server">Father
Name</asp:Label></span>                    <asp:TextBox ID="FName" runat="server" />                </td>             </tr>             <tr>                <td>                    <span id="Span2" runat="server"> <asp:Label ID="label3"  runat="server">Eamil</asp:Label></span><br />                    <asp:TextBox ID="email" TextMode="Email" runat="server"  />                    <span id="Span3" runat="server"> <asp:Label ID="label4"  runat="server">Mobile Number</asp:Label></span>                    <asp:TextBox ID="mobile" TextMode="Number" runat="server" />                </td>             </tr>            <tr>                <td>            <asp:Button ID="submit" runat="server" OnClick="submit_Click1" Text="Submit"/><asp:Label ID="id" runat="server" Visible="false"></asp:Label>   <asp:Button ID="update" runat="server" OnClick="update_Click" Text="Update" Visible="false"/><asp:Label ID="message" runat="server"></asp:Label>                </td>                </tr>        </table>        <hr />        <h3><asp:Label ID="Employe" runat="server" Text="Employee
Detail"></asp:Label></h3>        <asp:GridView ID="grid1" DataKeyNames="Id" runat="server" AutoGenerateColumns="false" Width="1024" OnRowDeleting="grid1_RowDeleting" OnRowUpdating="grid1_RowUpdating">                       <Columns>                               <asp:TemplateField HeaderText="Name">                     <ItemTemplate>                         <asp:Label ID="s1" runat="server" Text='<%#Eval("UserName") %>'></asp:Label>                     </ItemTemplate>                 </asp:TemplateField>                  <asp:TemplateField HeaderText="Father Name">                     <ItemTemplate>                         <asp:Label ID="s2" runat="server" Text='<%#Eval("FName")%>'></asp:Label>                     </ItemTemplate>                 </asp:TemplateField>                  <asp:TemplateField HeaderText="Email">                     <ItemTemplate>                         <asp:Label ID="s3" runat="server" Text='<%#Eval("Email")%>'></asp:Label>                     </ItemTemplate>                 </asp:TemplateField>                  <asp:TemplateField HeaderText="Mobile">                     <ItemTemplate>                         <asp:Label ID="s4" runat="server" Text='<%#Eval("Mobile")%>'></asp:Label>                     </ItemTemplate>                 </asp:TemplateField>                                       <asp:TemplateField>                    <ItemTemplate>                        <asp:Button ID="b1" runat="server" Text="Delete" CommandName="Delete" CausesValidation="false" />                                           </ItemTemplate>                </asp:TemplateField>                  <asp:TemplateField>                    <ItemTemplate>                                               <asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Update" CausesValidation="false" />                    </ItemTemplate>                </asp:TemplateField>            </Columns>        </asp:GridView>                 </div>        </ContentTemplate>        </asp:UpdatePanel>    </form></body></html> 
```

\
**for bind grid use this code**

```
void bind()        {            DataSet ds = new DataSet();            using (SqlConnection con = new SqlConnection(@"Data
Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;Initial
Catalog = testdata"))            {                con.Open();                SqlCommand cmd = new SqlCommand("select * from
EmpDetail", con);                SqlDataAdapter da = new SqlDataAdapter(cmd);                da.Fill(ds);                con.Close();                grid1.DataSource = ds;                grid1.DataBind();            }        }
```

```
For saving records in database
```

```
  protected void submit_Click1(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = @"Data
Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;Initial
Catalog = testdata;"Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;InitialCatalog = testdata;";
            cnn.Open();
 
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "insert into
EmpDetail values('"EmpDetail values('"+ EmpName.Text + "','"+ FName.Text + "','"+ email.Text + "','"+ mobile.Text + "')";
            cmd.Connection = cnn;
            int i = cmd.ExecuteNonQuery();
            if (i == 1)
                message.Text = "Saved
Succesfully!!"Succesfully!!";
            else
                message.Text = "Error in
saveing!!! try again!!"saveing!!! try again!!";
 
           
            bind();
 
        }
```

```

```

```
for deleting and eding record throuh grid view
```

```
  protected void grid1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
 
            try
            {
                string Id =
grid1.DataKeys[e.RowIndex].Values[grid1.DataKeys[e.RowIndex].Values["Id"].ToString();
                SqlConnection cnn = new SqlConnection();
                cnn.ConnectionString = @"Data
Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;Initial
Catalog = testdata;"Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;InitialCatalog = testdata;";
                cnn.Open();
 
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText="delete from
EmpDetail where Id='"EmpDetail where Id='"+Id+"' ";
                cmd.Connection = cnn;
 
                int i =
cmd.ExecuteNonQuery();cmd.ExecuteNonQuery();
                if (i == 1)
                {
                    bind();
                    message.Text = "Record
deleted"deleted";
                }
                else
                {
                    message.Text = "error";
                }
 
            }
            catch (Exception ex)
            {
                message.Text =ex.Message.ToString();
            }
 
        }
 <--for clearing textboxes after submitting records-->
        protected void ClearInputs(ControlCollection ctrls)
        {
            foreach (Control ctrl in ctrls)
            {
                if (ctrl is TextBox)
               
                    ((TextBox)ctrl).Text = string.Empty;
                ClearInputs(ctrl.Controls);
               
            }
        }
    
 
     
 
        protected void grid1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            submit.Visible = false;
            update.Visible = true;
            string Id = grid1.DataKeys[e.RowIndex].Values["Id"].ToString();
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = @"Data
Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;Initial
Catalog = testdata;"Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;InitialCatalog = testdata;";
            cnn.Open();
            SqlCommand cmd = new SqlCommand("select *
from EmpDetail where Id="from EmpDetail where Id=" + Id, cnn);
           
            SqlDataReader dr =
cmd.ExecuteReader();cmd.ExecuteReader();
            if (dr.HasRows == true)
            {
                dr.Read();
                id.Text = dr["Id"].ToString();
                EmpName.Text = dr["UserName"].ToString();
                FName.Text = dr["FName"].ToString();
                email.Text = dr["Email"].ToString();
                mobile.Text = dr["Mobile"].ToString();
 
                dr.Close();
                cnn.Close();
            }
        }
 
 
```


---

Original Source: https://www.mindstick.com/forum/34228/how-to-add-edit-update-records-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
