DetailsView Control in ASP.NET - Part 9
Introduction
In Part 8 of this article series we have discussed how to Insert Data with
DetailsView Control. Now in this article we will discuss how Delete Data with
DetailsView Control.
Deleting Data with DetailsView Control
We can delete records with the DetailsView control
by enabling its AutoGenerateDeleteButton property.
The page given below enables us to both insert and delete records in the Movies
database table. Take a look here.
<%@ 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">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView
id="dtlBooks"
AllowPaging="True"
DataSourceID="SqlDataSource1"
AutoGenerateInsertButton="True"
AutoGenerateDeleteButton="True"
AutoGenerateRows="False"
DataKeyNames="ID"
Runat="server" >
<Fields>
<asp:BoundField
DataField="ID"
HeaderText="ID"/>
<asp:BoundField
DataField="TITLE"
HeaderText="TITLE"/>
<asp:BoundField
DataField="AUTHOR"
HeaderText="AUTHOR"/>
<asp:BoundField
DataField="PRICE"
HeaderText="PRICE"/>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
InsertCommand="INSERT INTO BOOK_LIST(ID, TITLE, AUTHOR, PRICE) VALUES (@ID,
@TITLE, @AUTHOR, @PRICE)"
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>
When we delete record, we need to supply a value for the DetailsView control's DataKeyNames property.
Notice that a parameter named @ID is used
to represent the value of the ID column in the DeleteCommand property.
If we not do so then will get error when delete record.
Note: Continue
in Next Part.
Comments
Post a Comment