I'm using GridView
along with SqlDataSource
, for selecting, updating and deleting,
and DetailsView
for Inserting, the problem is, when I click one of these buttons, I get this error for the Delete
order (and the same error with Insert
and Update
..):
Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified.
it is very exhausting to add these statements myself, and in some cases I need to define stored procedures. I'm asking because I read(but not remember where, unfortunately) a tutorial, which Visual Studio generate all these automatically.
source code:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Donor.aspx.cs" Inherits="Donor" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="Id" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display."
ForeColor="#333333" GridLines="None" AllowSorting="True">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ItemStyle-Width="120px"/>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ItemStyle-Width="150px"/>
<asp:BoundField DataField="BloodGroup" HeaderText="BloodGroup" SortExpression="BloodGroup" ItemStyle-Width="120px"/>
<asp:BoundField DataField="Disease" HeaderText="Disease" SortExpression="Disease" ItemStyle-Width="120px"/>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Name], [BloodGroup], [Disease], [Id] FROM [Patient]">
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" CellPadding="4"
DataSourceID="SqlDataSource2" ForeColor="#333333"
GridLines="None" Height="50px" Width="125px"
DefaultMode="Insert">
<AlternatingRowStyle BackColor="White" />
<CommandRowStyle BackColor="#FFFFC0" Font-Bold="True" />
<FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />
<Fields>
<asp:CommandField ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Name], [BloodGroup], [Disease] FROM [Patient]" InsertCommand="INSERT INTO [Patient] ([Name],[BloodGroup],[Disease]) VALUES (@Name, @BloodGroup, @Disease)">
<InsertParameters>
<asp:Parameter Name="Name" />
<asp:Parameter Name="BloodGroup" />
<asp:Parameter Name="Disease" />
</InsertParameters>
</asp:SqlDataSource>
</asp:Content>
Add the
DeleteCommand
to yourSqlDataSource
:Like this:
EDIT: To make ASP.NET generate the
DELETE
command automatically for you without any code do the following steps:SqlDataSource
, click Configure Data Source.Select DELETE Tab and then click on the Query Builder, Select your table and then build your query by helping the Query Builder window. Your query should be look like this finally:
And finally click Next And Finish.