How to edit update insert delete and cancel using button in form view.

In this blog we will know how to edit update insert delete and cancel using button in form view.

 

Web.config file

 

<connectionStrings>

            <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Test.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

      </connectionStrings>

 

 

 

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

 

<!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>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:FormView ID="FormView1" runat="server" CellPadding="4" ForeColor="#333333"

     DataKeyNames="AutoID" DataSourceID="SqlDataSource1" AllowPaging="true">

        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

        <RowStyle BackColor="#EFF3FB" />

        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

        <ItemTemplate>

            <table border="1">

                <tr>

                    <td>AutoID</td>

                    <td><%# Eval("AutoID") %></td>

                </tr>

                <tr>

                    <td>Name</td>

                    <td><%# Eval("Name") %></td>

                </tr>

                <tr>

                    <td>Address</td>

                    <td><%# Eval("Address") %></td>

                </tr>

                <tr>

                    <td>Phone</td>

                    <td><%# Eval("Phone") %></td>

                </tr>

                <tr>

                    <td>City</td>

                    <td><%# Eval("City") %></td>

                </tr>

                <tr>

                    <td> </td>

                    <td>

                        <asp:Button ID="btnEdit" runat="Server" CommandName="Edit" Text="Edit" />

                        <asp:Button ID="btnInsert" runat="Server" CommandName="New" Text="New" />

                        <asp:Button ID="btnDelete" runat="Server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure to Delete?');" />

                    </td>

                </tr>

            </table>

        </ItemTemplate>

        <EditItemTemplate>

            <table border="1">

                <tr>

                    <td>AutoID</td>

                    <td><%# Eval("AutoID") %></td>

                </tr>

                <tr>

                    <td>Name</td>

                    <td><asp:TextBox ID="TextBox1" runat="Server" Text='<%# Bind("Name")%>'></asp:TextBox></td>

                </tr>

                <tr>

                    <td>Address</td>

                    <td><asp:TextBox ID="TextBox2" runat="Server" Text='<%# Bind("Address")%>'></asp:TextBox></td>

                </tr>

                <tr>

                    <td>Phone</td>

                    <td><asp:TextBox ID="TextBox3" runat="Server" Text='<%# Bind("Phone")%>'></asp:TextBox></td>

                </tr>

                <tr>

                    <td>City</td>

                    <td><asp:TextBox ID="TextBox4" runat="Server" Text='<%# Bind("City")%>'></asp:TextBox></td>

                </tr>

                <tr>

                    <td> </td>

                    <td>

                        <asp:Button ID="btnUpdate" runat="Server" CommandName="Update" Text="Update" />

                        <asp:Button ID="Button1" runat="Server" CommandName="Cancel" Text="Cancel" />

                    </td>

                </tr>

            </table>

        </EditItemTemplate>

        <InsertItemTemplate>

            <table border="1">

                <tr>

                    <td>AutoID</td>

                    <td><%# Eval("AutoID") %></td>

                </tr>

                <tr>

                    <td>Name</td>

                    <td><asp:TextBox ID="TextBox1" runat="Server" Text='<%# Bind("Name")%>'></asp:TextBox></td>

                </tr>

                <tr>

                    <td>Address</td>

                    <td><asp:TextBox ID="TextBox2" runat="Server" Text='<%# Bind("Address")%>'></asp:TextBox></td>

                </tr>

                <tr>

                    <td>Phone</td>

                    <td><asp:TextBox ID="TextBox3" runat="Server" Text='<%# Bind("Phone")%>'></asp:TextBox></td>

                </tr>

                <tr>

                    <td>City</td>

                    <td><asp:TextBox ID="TextBox4" runat="Server" Text='<%# Bind("City")%>'></asp:TextBox></td>

                </tr>

                <tr>

                    <td> </td>

                    <td>

                        <asp:Button ID="btnSave" runat="Server" CommandName="insert" Text="Insert" />

                        <asp:Button ID="Button1" runat="Server" CommandName="Cancel" Text="Cancel" />

                    </td>

                </tr>

            </table>

        </InsertItemTemplate>

    </asp:FormView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:ConnectionString %>'

     SelectCommand="Select * FROM employee ORDER BY [Name]"

     DeleteCommand="Delete FROM employee WHERE AutoID = @AutoID"

     UpdateCommand="UPDATE employee SET Name = @Name, Address = @Address, Phone = @Phone, City = @City WHERE AutoID = @AutoID"

     InsertCommand="INSERT INTO employee (Name, Address, Phone, City) VALUES (@Name, @Address, @Phone, @City)">

        <DeleteParameters>

            <asp:Parameter Name="AutoID" />

        </DeleteParameters>

        <UpdateParameters>

            <asp:Parameter Name="AutoID" Type="Int32" />

            <asp:Parameter Name="Name" Type="string" Size="50" />

            <asp:Parameter Name="Address" Type="string" Size="200" />

            <asp:Parameter Name="Phone"  Type="string" Size="50" />

            <asp:Parameter Name="City" Type="string" Size="20" />

        </UpdateParameters>

        <InsertParameters>

            <asp:Parameter Name="Name" Type="string" Size="50" />

            <asp:Parameter Name="Address" Type="string" Size="200" />

            <asp:Parameter Name="Phone" Type="string" Size="50" />

            <asp:Parameter Name="City" Type="string" Size="20" />

            <asp:Parameter Name="AutoID" Type="Int32" />

        </InsertParameters>

     </asp:SqlDataSource>

 

 

    </div>

    </form>

</body>

</html>