FormView Control in ASP.Net: Part 5

Introduction

In Part 4 of this article series we have discussed how to Insert New Record with FormView Control. Now in this article we will discuss how Delete Data or say Record with FormView Control.

Deleting Data with FormView Control

We can use the FormView control to delete database records too. For example, the page given below enables us to delete records from the BOOK_LIST database table.

22.JPG
 

<%@ Page Language="VB" %>

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

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">

    <style type="text/css">
    html
    {
        background-color:silver;
    }
    #content
    {
        margin:auto;
        width:600px;
        padding:10px;
        background-color:white;
        font:14px Georgia,Serif;
    }
    a
    {
        color:blue;
    }
    </style>
 
    <title></title>
</head>
<
body>
    <form id="form1" runat="server">

    <div id="content">

    <asp:FormView
        id="frmBooks"
        DataSourceID="SqlDataSource1"
        DataKeyNames="ID"
        AllowPaging="True"
        Runat="server">
        <ItemTemplate>
        <b><u><%# Eval("TITLE")%></u><b>
        <br />
        <b>Serial Number:</b>
        <%# Eval("ID")%>
        <br />
        <b>Author:</b>
        <%# Eval("AUTHOR")%>
        <br />
        <b>Price:</b>
        <%#Eval("PRICE") %>
        <hr />
        <asp:LinkButton
            id="lnkDelete"
            Text="Delete Book"
            CommandName="Delete"
            OnClientClick="return confirm('Are you sure want to Delete?');"
            Runat="server" />
        </ItemTemplate>
    </asp:FormView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
            SelectCommand="SELECT [ID], [TITLE], [AUTHOR], [PRICE] FROM [BOOK_LIST]"
            DeleteCommand="DELETE FROM BOOK_LIST WHERE ID=@ID">
        </asp:SqlDataSource>

     </div>
    </form>
</body>
</
html>

In above example, notice that the FormView control includes a DataKeyNames property, which contains the name of the primary key column from the data source. When deleting records with the FormView control, we need to indicate the primary key column.

Furthermore, notice that the ItemTemplate includes a LinkButton for deleting a record. The LinkButton looks like this:

<asp:LinkButton
            id="lnkDelete"
            Text="Delete Book"
            CommandName="Delete"
            OnClientClick="return confirm('Are you sure want to Delete?');"
            Runat="server" />


This LinkButton includes a CommandName property that has the value Delete. When we click the LinkButton, the SQL command represented by the SqlDataSource control's DeleteCommand property is executed. The LinkButton includes an OnClientClick property that calls the JavaScript confirm() method to display a confirmation dialog box. This extra script prevents users from accidentally deleting database records.

Note: This is my last article of this FormView Control series. I hope you will enjoy it.

HAVE A GREAT CODING!


Similar Articles