[Updated:2018] How to perform insert and update operation on single button

Save and update on same button in asp net : 

Welcome Buddy, Today I am going to show you about insert and update in one button c#. Actually, this is very simple to perform insert and update operation on single button, here is how you can do this. Just perform each and every step one by one.


How to perform insert and update operation on single button


Step #1:

Create store procedure for each insert,delete,update,edit


Create database Tutorial
use Tutorial


create table employee
(
eid int primary key identity,
ename varchar(50),
eadress varchar(50),
emob int
)


create proc employee_insert
@ename varchar(50),
@eadress varchar(50),
@emob int
as
begin
insert into employee(ename,eadress,emob)values(@ename,@eadress,@emob)
end

  
Create proc employee_grid
as
begin
select * from employee
end


Create proc employee_delete
@eid int
as
begin
delete  from employee where eid=@eid
select * from employee


Create proc employee_Edit
@eid int
as
begin
select * from employee where eid=@eid
end


Create proc employee_update
@eid int,
@ename varchar(50),
@eadress varchar(50),
@emob int
as
begin
   update employee set ename=@ename,eadress=@eadress,emob=@emob where eid=@eid
end


Step #2:

Add connectionstring in web.config file

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <connectionStrings>
    <add name="DBCS" connectionString="initial catalog =Tutorial; data source=MYPC\SQL2012; integrated security=true"/>
  </connectionStrings>
</configuration>


Step #3:

Add one web form in your project with respected name


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>Name:</td>
                    <td>
                        <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Address:
                    </td>
                    <td>
                        <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Phone:</td>
                    <td>
                        <asp:TextBox ID="txtphone" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <asp:Button ID="btninsert" runat="server" Text="Insert" OnClick="btninsert_Click"></asp:Button>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false" OnRowCommand="grd_RowCommand" >
                            <Columns>
                                <asp:TemplateField HeaderText="Name">

                                    <ItemTemplate>
                                        <%#Eval("ename") %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Address">
                                    <ItemTemplate>
                                        <%#Eval("eadress") %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Mobile">
                                    <ItemTemplate>
                                        <%#Eval("emob") %>
                                    </ItemTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lbn" runat="server" Text="Delete" CommandName="DLT" CommandArgument='<%#Eval("eid") %>'></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lbn1" runat="server" Text="Edit" CommandName="EDT" CommandArgument='<%#Eval("eid") %>'></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>


Step #4:

Come on .CS page and add necessary namespace

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;


public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            grid_view();
        }
    }
    void OpenConnection()
    {
        if (con.State == ConnectionState.Closed)
            con.Open();
    }
    void CloseConnection()
    {
        if (con.State == ConnectionState.Open)
            con.Close();
    }
    public void grid_view()
    {
        OpenConnection();
        SqlCommand cmd = new SqlCommand("employee_grid", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            grd.DataSource = ds;
            grd.DataBind();
        }
        CloseConnection();
    }
    protected void btninsert_Click(object sender, EventArgs e)
    {
        if (btninsert.Text == "Insert")
        {
            OpenConnection();
            SqlCommand cmd = new SqlCommand("employee_insert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ename", txtname.Text);
            cmd.Parameters.AddWithValue("@eadress", txtaddress.Text);
            cmd.Parameters.AddWithValue("@emob", txtphone.Text);
            cmd.ExecuteNonQuery();
            CloseConnection();
            grid_view();
        }
        else if (btninsert.Text == "Update")
        {
            OpenConnection();
            SqlCommand cmd = new SqlCommand("employee_update", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@eid", ViewState["abc"]);
            cmd.Parameters.AddWithValue("@ename", txtname.Text);
            cmd.Parameters.AddWithValue("@eadress", txtaddress.Text);
            cmd.Parameters.AddWithValue("@emob", txtphone.Text);
            cmd.ExecuteNonQuery();
            CloseConnection();
            grid_view();
        }
    }
    protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EDT")
        {
            OpenConnection();
            SqlCommand cmd = new SqlCommand("employee_Edit", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@eid", e.CommandArgument);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            CloseConnection();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                txtname.Text = ds.Tables[0].Rows[0]["ename"].ToString();
                txtaddress.Text = ds.Tables[0].Rows[0]["eadress"].ToString();
                txtphone.Text = ds.Tables[0].Rows[0]["emob"].ToString();
            }
            grid_view();
        }
        else
        {
            OpenConnection();
            SqlCommand cmd = new SqlCommand("employee_delete", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@eid", e.CommandArgument);
            cmd.ExecuteNonQuery();
            CloseConnection();
            grid_view();
        }
        btninsert.Text = "Update";
        ViewState["abc"] = e.CommandArgument;
    }
}





Previous
Next Post »