How to generate the needed SQL statements to Updat

2019-03-01 10:51发布

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>

1条回答
来,给爷笑一个
2楼-- · 2019-03-01 11:30

Add the DeleteCommand to your SqlDataSource:

DeleteCommand="Delete from yourTable WHERE (ID = @ID)"

Like this:

<asp:SqlDataSource runat="server" DeleteCommand="Delete from yourTable WHERE (ID = @ID)">
    <DeleteParameters>
        <asp:Parameter Name="ID"></asp:Parameter>
    </DeleteParameters>
</asp:SqlDataSource>

EDIT: To make ASP.NET generate the DELETE command automatically for you without any code do the following steps:

  1. In the tasks window of your SqlDataSource, click Configure Data Source.
  2. Choose your data connection and Next.
  3. Make sure Specify a custom SQL statement or stored procedure checked and click Next.
  4. 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:

    Delete from yourTable WHERE (ID = @ID)
    
  5. And finally click Next And Finish.

查看更多
登录 后发表回答