Tuesday 7 March 2017

code for gridview insert update and delete

gridview insert update and delete code 





Source code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridview.aspx.cs" Inherits="gridview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 
            AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="id" 
            onrowcancelingedit="gv_RowCancelingEdit" onrowdeleting="gv_RowDeleting" 
            onrowediting="gv_RowEditing" onrowupdating="gv_RowUpdating">
            <Columns>
                <asp:TemplateField HeaderText="productname">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("productname") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("productname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="productprice">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("productprice") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Eval("productprice") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="insert" />
&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBox4" runat="server" Width="73px"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBox5" runat="server" Width="73px"></asp:TextBox>
    </form>
</body>
</html>


Code

Insert update and delete in gridview
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 gridview : System.Web.UI.Page
{
    string constr = "data source=ravi;database=ravi;integrated security=yes";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            binddata();
        }
    }
    private void binddata()
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(constr))
        {
            
            SqlDataAdapter adp = new SqlDataAdapter("select *from service", con);
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                gv.DataSource = dt;
                gv.DataBind();
            }
        }
    }

    protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gv.EditIndex = -1;
        binddata();

    }

    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id=int.Parse(gv.DataKeys[e.RowIndex].Value.ToString());
        deleteservice(id);
        binddata();
    }

    protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gv.EditIndex = e.NewEditIndex;
    }

    protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(gv.DataKeys[e.RowIndex].Value.ToString());
        TextBox textbox2 = (TextBox)gv.Rows[e.RowIndex].FindControl("textbox2");
        TextBox textbox3 = (TextBox)gv.Rows[e.RowIndex].FindControl("textbox3");
        updateservice(id,textbox2.Text,int.Parse(textbox3.Text));
        gv.EditIndex=-1;
        binddata();
     
    }
    private void updateservice(int id,string productname,int productprice)
    {
        using (SqlConnection con = new SqlConnection(constr))
        {
            string query="update service set productname='"+productname+"',productprice="+productprice+" where id="+id+"";
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            cmd.ExecuteNonQuery();
        }

    }
    private void deleteservice(int id)
    {
        using (SqlConnection con = new SqlConnection(constr))
        {
            string query = "delete from service where id=" + id + "";
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            cmd.ExecuteNonQuery();
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string productname = TextBox4.Text;
        decimal productprice =decimal.Parse(TextBox5.Text);

        using (SqlConnection con = new SqlConnection(constr))
        {
            string query="insert into service values('"+TextBox4.Text+"',"+TextBox5.Text+")";
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            cmd.ExecuteNonQuery();
            gv.DataBind();
            con.Close();
            binddata();
        }
    }
}

6 comments:

  1. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using BLL;
    using DAL;
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
    CLSBussLayer objBLL = new CLSBussLayer();
    objBLL.InsertUser(TxtName.Text, TxtCourse.Text, LblCourseId.Text);
    GridView1.DataSource = objBLL.SelectUser();
    GridView1.DataBind();
    }
    }

    ReplyDelete
  2. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using DAL;

    namespace BLL
    {
    public class Class1
    {
    }
    public class CLSBussLayer
    {
    ClsDataLayer objDAL = new ClsDataLayer();
    public void InsertUser(String Name, String Course, string CourseId)
    {
    objDAL.InsertData(Name,Course, CourseId);
    }
    public object SelectUser()
    {
    return objDAL.SelectData();
    }
    }
    }

    ReplyDelete
  3. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    using System.Data.SqlClient;
    namespace DAL
    {
    public class Class1
    {
    }
    public class ClsDataLayer
    {
    string constr="Data Source=192.168.137.1;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=Sun@Admin;";
    public void InsertData(string Name, String Course, String CourseId)
    {
    //SqlDataAdapter adp = new SqlDataAdapter("insert into tbl_course values('" + Name + "','" + Course + "','" + CourseId + "')", con1);
    //DataTable dt = new DataTable();
    //adp.Fill(dt);

    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText= "sp_insertintocourse";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@Name", Name);
    cmd.Parameters.AddWithValue("@Course", Course);
    cmd.Parameters.AddWithValue("@CourseId", CourseId);
    cmd.Connection = con;
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    }
    public object SelectData()
    {
    SqlDataAdapter adp = new SqlDataAdapter("select *from tbl_course", constr);
    DataTable dt = new DataTable();
    adp.Fill(dt);
    return dt;
    }
    }
    }

    ReplyDelete
  4. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using BLL;
    using DAL;
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
    CLSBussLayer objBLL = new CLSBussLayer();
    objBLL.InsertUser(TxtName.Text, TxtCourse.Text, LblCourseId.Text);
    GridView1.DataSource = objBLL.SelectUser();
    GridView1.DataBind();
    }


    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

    }


    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }


    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
    }

    ReplyDelete
  5. http://www.c-sharpcorner.com/UploadFile/009464/how-to-bind-gridview-in-3-tier-in-Asp-Net-using-C-Sharp/

    ReplyDelete
  6. https://www.echallan.org/publicview/

    ReplyDelete