【发布时间】:2016-04-04 10:04:31
【问题描述】:
我将GridView 与SqlDataSource 一起用于选择、更新和删除,
和DetailsView 用于插入,问题是,当我单击其中一个按钮时,Delete 订单出现此错误(Insert 和Update 出现相同错误。 .):
数据源“SqlDataSource1”不支持删除,除非 已指定 DeleteCommand。
自己添加这些语句很累,而且在某些情况下我需要定义存储过程。我之所以问是因为我读过(但不幸的是不记得在哪里)一个教程,Visual Studio 会自动生成所有这些。
源代码:
<%@ 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>
【问题讨论】:
-
你能告诉我们你所做的代码吗?
-
参见 SQLdatasource1 仅包含 Selectcommand,您必须包含所有其他命令(插入、更新、删除),如 @user2946329 答案。
-
这不能通过为 SqlDataSouce 启用插入、删除和更新来自动完成吗?
-
@MohammedElshawaf...您也可以通过单击
SqlDataSource中的配置数据源来执行此操作。 -
@MohammedElshawaf...正如我所说:要让 ASP.NET 无需任何代码自动为您生成 DELETE 命令,您应该使用配置数据源。我用完整的演练更新了我的答案。
标签: c# asp.net sql-server gridview sqldatasource