Thursday, 16 May 2013

Working Insert, Update and Delete rows in ASP.NET GridView Control


Insert, Update and Delete rows in ASP.NET GridView Control



In this Article you can learn how to edit, update, delete, and cancel in gridview.First drag and drop Gridview.In gridview properties set AutoGenarateColumns to False.Next open Default.aspx source code. To make a columns in Gridview use  <asp:TemplateField>Here first I created

C# (355.7 KB)

Introduction

In this Article you can learn how to edit, update, delete, and cancel in gridview.First drag and drop Gridview.In gridview properties set AutoGenarateColumns to False.Next open Default.aspx source code. To make a columns in Gridview use  <asp:TemplateField>Here first I created a table name 'Product' in my database.it contains 3 colomns as ProductId,ProductName,ProdcutPrice

1)Download My Sample.
2)Inside that folder person.sql file will be there execute it in your database.  




SQL
/****** Object:  Table [dbo].[Product]    Script Date: 07/07/2012 02:18:07 ******/ 
SET ANSI_NULLS ON 
GO 
 
SET QUOTED_IDENTIFIER ON 
GO 
 
SET ANSI_PADDING ON 
GO 
 
CREATE TABLE [dbo].[Product]( 
    [pk_id] [intIDENTITY(1,1NOT NULL, 
    [ProductId] [varchar](5NULL, 
    [ProductName] [varchar](50NULL, 
    [ProductPrice] [numeric](82NULLPRIMARY KEY CLUSTERED  
( 
    [pk_idASC 
)WITH (PAD_INDEX  = OFFSTATISTICS_NORECOMPUTE  = OFFIGNORE_DUP_KEY = OFFALLOW_ROW_LOCKS  = ONALLOW_PAGE_LOCKS  = ONON [PRIMARY] 
) ON [PRIMARY] 
 
GO 
 
SET ANSI_PADDING OFF 
GO 
 3)Change the connetion string in web.config file.


C#
<configuration> 
  <connectionStrings> 
    <add name="MyConnection" 
         connectionString="Data Source=MONISH-PC\MONISH;Initial Catalog=master;Persist Security Info=True;User ID=saty;Password=123" 
         providerName="System.Data.SqlClient" /> 
  </connectionStrings>
 
Building the Sample
This sample wrote by three tier architecture so you have to add references.
UI->BusinessLogic
BusinessLogic->DataAccess back to BL
BusinessLogic->Commonfunctioon back to BL
BusinessLogic -> UI finally BL return data to UI



Description
In Default.aspx source code we have to add
This is used to Edit the Row in Gridview.Here I am going to Edit only two columns name and marks.
C#
<asp:GridView ID="GridView1" runat="server" CellPadding="4"  
            ForeColor="#333333"  AutoGenerateColumns="False"  
            onrowcancelingedit="GridView1_RowCancelingEdit"  
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"  
            onrowupdating="GridView1_RowUpdating" > 
            <AlternatingRowStyle BackColor="White" /> 
            <Columns> 
                <asp:TemplateField Visible="False">   
                                 
                    <ItemTemplate> 
                        <asp:Label ID="lblPk_id" runat="server"  Text='<%#Eval("pk_id")%>'></asp:Label> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField Headertext="ProductId"> 
                    <EditItemTemplate> 
                        <asp:TextBox ID="txtProductId" runat="server"  Text='<%#Eval("ProductId")%>'></asp:TextBox> 
                    </EditItemTemplate> 
                    <FooterTemplate> 
                    <asp:TextBox ID="txtProductId" runat="server"  Text='<%#Eval("ProductId")%>'></asp:TextBox> 
                    </FooterTemplate> 
                    <ItemTemplate> 
                        <asp:Label ID="lblProductId" runat="server"  Text='<%#Eval("ProductId")%>'></asp:Label> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="ProductName"> 
                    <EditItemTemplate> 
                        <asp:TextBox ID="txtProductName" runat="server" Text='<%#Eval("ProductName")%>'></asp:TextBox> 
                    </EditItemTemplate> 
                    <FooterTemplate> 
                    <asp:TextBox ID="txtProductName" runat="server" Text='<%#Eval("ProductName")%>'></asp:TextBox> 
                    </FooterTemplate> 
                    <ItemTemplate> 
                        <asp:Label ID="lblProductName" runat="server"  Text='<%#Eval("ProductName")%>'></asp:Label> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="ProductPrice"> 
                    <EditItemTemplate> 
                        <asp:TextBox ID="txtProductPrice" runat="server"  Text='<%#Eval("ProductPrice")%>'></asp:TextBox> 
                    </EditItemTemplate> 
                    <FooterTemplate> 
                    <asp:TextBox ID="txtProductPrice" runat="server"  Text='<%#Eval("ProductPrice")%>'></asp:TextBox> 
                    </FooterTemplate> 
                    <ItemTemplate> 
                        <asp:Label ID="lblProductPrice" runat="server"    Text='<%#Eval("ProductPrice")%>'></asp:Label> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> 
                <asp:TemplateField> 
                <FooterTemplate> 
 
                <asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" UseSubmitBehavior="False" /> 
 
                </FooterTemplate> 
                </asp:TemplateField> 
 
            </Columns> 
            <EditRowStyle BackColor="#2461BF" /> 
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
            <RowStyle BackColor="#EFF3FB" /> 
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
            <SortedAscendingCellStyle BackColor="#F5F7FB" /> 
            <SortedAscendingHeaderStyle BackColor="#6D95E1" /> 
            <SortedDescendingCellStyle BackColor="#E9EBEF" /> 
            <SortedDescendingHeaderStyle BackColor="#4870BE" /> 
        </asp:GridView> 
       

Source Code Files

  • product.sql

Note:I have used SqlServer 2008
For this article we are going to fill GridView by data from database.
We are going to make perform following operations on data using GridView
  1. Add New record into database; here data will directly be added into database table
  2. Update Records into database, using edit link.
  3. Delete Records using delete link

No comments:

Post a Comment