Gridview Insert Update and Delete



.aspx CODE
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="managingState.aspx.cs" Inherits="ShoppingCarts.managingState" %>

<!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">
    <asp:GridView ID="grdcity" runat="server" Width="501px" ShowFooter ="true" 
        AutoGenerateColumns ="false" onrowediting="grdcity_RowEditing" 
        onrowdeleting="grdcity_RowDeleting" DataKeyNames ="cityCode" 
        onrowcancelingedit="grdcity_RowCancelingEdit" 
        onselectedindexchanged="grdcity_SelectedIndexChanged" 
        onrowupdating="grdcity_RowUpdating" 
        onpageindexchanging="grdcity_PageIndexChanging" 
        onrowcommand="grdcity_RowCommand" >
    <Columns >
    <asp:TemplateField HeaderText ="City Code" >
    <ItemTemplate >
    <asp:Label ID="lblcitycode" runat ="server" Text='<%#Eval("cityCode") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
                    <asp:TextBox ID="txtcityCode" runat="server" Columns="3" MaxLength="3"></asp:TextBox>
                </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText ="State Code">
    <ItemTemplate >
    <asp:Label ID="lblstatecode" runat="server"  Text ='<%#Eval("stateCode")%>'></asp:Label> 
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtstateCode" runat ="server" Text='<%#Bind("stateCode")%>' ></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate >
    <asp:TextBox ID="txtstateCode" runat ="server" MaxLength ="3" Columns ="3"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField  >
    <asp:TemplateField HeaderText ="State Name">
    <ItemTemplate >
    <asp:Label runat="server" ID="lblcityName" Text ='<%#Eval("cityName") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate >
    <asp:TextBox ID="txtcityName" runat="server" Text='<%#Bind("cityName") %>'></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate >
    <asp:TextBox runat ="server" ID="txtcityName"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField >
    <ItemTemplate >
    <asp:LinkButton runat ="server" ID="lnkedit" Text ="Edit" CommandName="Edit"></asp:LinkButton>
    </ItemTemplate>
    <EditItemTemplate >
    <asp:LinkButton runat ="server" ID="lnkUpdate" Text ="Update" CommandName="Update"></asp:LinkButton>
    <asp:LinkButton runat="server" ID="lnkCancel" Text ="Cancel" CommandName ="Cancel"></asp:LinkButton>

    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField >
    <ItemTemplate >
    <asp:LinkButton runat="server" ID="lnkDelete" Text="Delete" CommandName ="Delete" OnClientClick ="return confirm('are you sure y want to delete this record')" ></asp:LinkButton>
    </ItemTemplate>
    <FooterTemplate >
    <asp:LinkButton runat="server" ID="lnkInsert"  Text ="Insert" CommandName ="Insert"></asp:LinkButton>
    </FooterTemplate>
    </asp:TemplateField>
    </Columns>
    
    </asp:GridView>
        
    
    </form>
</body>
</html>


CS CODE

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace ShoppingCarts
{
    public partial class managingState : System.Web.UI.Page
    {
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ShoppingCart"].ConnectionString);

     
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack )
            databind();
         
        }
        void databind()
        {
            sqlcon.Open();
            SqlCommand sqlCmd = new SqlCommand();
            sqlCmd.CommandText="usp_city_select";
            sqlCmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = sqlCmd;
            sqlCmd.Connection = sqlcon;
            DataTable dt = new DataTable();
            da.Fill(dt);
            grdcity.DataSource = dt;
            grdcity.DataBind();
            sqlcon.Close();
        }

        protected void grdcity_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdcity.EditIndex = e.NewEditIndex;
            databind();
        }

        protected void grdcity_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
           sqlcon.Open();
            SqlCommand sqlCmd = new SqlCommand();
            sqlCmd.CommandText = "usp_city_delete";
            sqlCmd.CommandType = CommandType.StoredProcedure;
           sqlCmd .Parameters.AddWithValue("@cityCode", grdcity.DataKeys[e.RowIndex]["cityCode"].ToString());
           sqlCmd.Connection = sqlcon;
            sqlCmd.ExecuteNonQuery();
            sqlcon.Close();
            databind();
        }

        protected void grdcity_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void grdcity_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdcity.EditIndex = -1;
            databind();
        }

        protected void grdcity_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
         
         
            sqlcon.Open();
            SqlCommand sqlCmd = new SqlCommand();
            sqlCmd.CommandText = "usp_city_update";
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.AddWithValue("@cityCode", grdcity.DataKeys[e.RowIndex]["cityCode"].ToString());
            TextBox txtstateCode  =(TextBox)grdcity.Rows[e.RowIndex].FindControl("txtstateCode");
            TextBox txtcityName = (TextBox)grdcity.Rows[e.RowIndex].FindControl("txtcityName");
            sqlCmd.Parameters.AddWithValue("@stateCode",txtstateCode .Text.Trim());
            sqlCmd.Parameters.AddWithValue("@cityName",txtcityName.Text.Trim());
            sqlCmd.Connection = sqlcon;
            sqlCmd.ExecuteNonQuery();
            sqlcon.Close();
            grdcity.EditIndex = -1;
            databind();
        }

        protected void grdcity_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdcity.PageIndex = e.NewPageIndex;
            databind();
        }

        protected void grdcity_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToString() == "Insert")
            {
                TextBox txtcityCode = (TextBox)grdcity.FooterRow.FindControl("txtcityCode");
                TextBox txtstateCode = (TextBox)grdcity.FooterRow.FindControl("txtstateCode");
                TextBox txtcityName = (TextBox)grdcity.FooterRow.FindControl("txtcityName");
                sqlcon.Open();
                SqlCommand sqlCmd = new SqlCommand();
                sqlCmd.CommandText = "usp_city_insert";
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.Parameters.AddWithValue("@cityCode", txtcityCode.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@stateCode", txtstateCode.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@cityName", txtcityName.Text.Trim());
                sqlCmd.Connection = sqlcon;
                sqlCmd.ExecuteNonQuery();
                sqlcon.Close();
            }
        }        
     
    }

}

Comments