Delete records using GridView and SqlDataSource
GridViewDeleteRecords.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html>
<script runat="server">
void SqlDataSource1_Deleted(object sender, SqlDataSourceStatusEventArgs e)
{
if (e.Exception == null)
{
if (e.AffectedRows == 1)
{
Label1.Text = "One Record deleted successfully!";
}
else
{
Label1.Text = "An error occured!";
}
}
else
{
Label1.Text = "Error: " + e.Exception.Message;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>How to delete records (data) using GridView and SqlDataSource</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy; font-style:italic;">SqlDataSource and GridView Example: Delete Records</h2>
<!-- Remove relation between Products and OrderDetails tables for test this example -->
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="Select ProductID, ProductName, QuantityPerUnit, UnitPrice From products"
SelectCommandType="Text"
DeleteCommand="Delete From Products Where ProductID=@original_ProductID"
OldValuesParameterFormatString="original_{0}"
OnDeleted="SqlDataSource1_Deleted"
>
<SelectParameters>
<asp:Parameter Direction="Output" Name="Count" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Label
ID="Label1"
runat="server"
Font-Bold="true"
Font-Italic="true"
Font-Size="Large"
ForeColor="Crimson"
>
</asp:Label>
<br /><br />
<asp:GridView
ID="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="true"
DataKeyNames="ProductID"
AllowPaging="true"
PageSize="8"
BorderColor="Salmon"
AutoGenerateDeleteButton="true"
Font-Names="Comic Sans MS"
Width="850"
>
<HeaderStyle BackColor="OrangeRed" ForeColor="Snow" Height="45"/>
<RowStyle BackColor="DeepPink" ForeColor="Snow" Font-Italic="true" />
<PagerStyle
Height="45"
HorizontalAlign="Right"
BackColor="BurlyWood"
Font-Bold="true"
Font-Size="X-Large"
ForeColor="Snow"
/>
<PagerSettings Mode="Numeric" />
</asp:GridView>
</div>
</form>
</body>
</html>


