【问题标题】:Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified除非指定 DeleteCommand,否则数据源“SqlDataSource1”不支持删除
【发布时间】:2013-04-11 12:33:27
【问题描述】:

我在GridView1 中有一个 ASP 按钮链接,它应该在单击时从 db 表中删除一个项目,我收到以下错误消息:

Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified. 

这是代码:

VB.NET

Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand


    If e.CommandName.Equals("Delete") Then


        Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
        Dim rowID As String = e.CommandArgument.ToString()
        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True;")
        Dim cmd As New SqlCommand("DELETE content WHERE content_id=@rowID", conn)

        cmd.Parameters.AddWithValue("@rowID", rowID)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()


    End If

End Sub

平均售价

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" ViewStateMode="Enabled">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="content_name" HeaderText="Content Name" SortExpression="content_name">
            </asp:BoundField>
            <asp:BoundField DataField="content_type" HeaderText="Content Type" SortExpression="content_type">
            </asp:BoundField>
            <asp:buttonfield buttontype="Link" commandname="Delete" text="Delete"> 
             <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:buttonfield>
             <asp:TemplateField HeaderText=" ">
            <ItemTemplate>
            <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click" ></asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>

根据我的阅读,您需要为 SQLDataSource1 指定一个 DELETE 命令。

我该怎么做?

【问题讨论】:

    标签: asp.net vb.net gridview sql-server-2012 sqldatasource


    【解决方案1】:

    如果您将其更改为...CommandName="DeleteRow" 之类的...然后将其更改为if(e.CommandName == "DeleteRow"),您将绕过该问题。

    【讨论】:

    • 这对我有用,虽然我不知道为什么。我猜当 CommandName 等于“Delete”时,它会假设需要填写 Delete 命令?
    • 谢谢,当我将“删除”更改为其他内容时,它也对我有用。
    【解决方案2】:

    你必须输入asp代码:

    <asp:ButtonColumn CommandName="Delete" Text="Delete"></asp:ButtonColumn>
    

    真诚的。

    【讨论】:

    • 我收到此错误:Element 'ButtonColumn' is not a known element.
    • 抱歉,这是给DataGrid,给GridView,试试用这个:&lt;asp:CommandField DeleteText="Delete" ShowDeleteButton="True"&gt;&lt;/asp:CommandField&gt;。看看这篇文章的一些很好的例子:msdn.microsoft.com/en-us/library/ms972940.aspx
    【解决方案3】:

    简单回答一下我是如何做到的。

    如果您在选择字段的命令字段属性下选择“ShowDeleteButton”,“删除”将显示在gridview 的每一行的左侧。但是,当您这样做时,期望 sql 数据源将具有删除命令。如果没有,您会收到有关未指定删除命令的讨厌消息。

    由于我希望 BusinessObject 处理删除而不是 sql 数据源,因此我使用了 select,就像您选择一行一样,然后我将命令字段属性下的“SelectText”更改为“Delete” .这允许我使用 RowCommand 事件来捕获删除操作。从那里我只是抓取了我需要的行信息,并将其传递给业务对象以进行删除。

    请注意,当您这样做时,即使命令文本是“删除”,CommandName 仍然是“选择”。所以你检查如下:

     if (e.CommandName.ToString() == "Select")
    

    希望对某人有所帮助。祝你好运!

    【讨论】:

      【解决方案4】:

      你需要把它改成 Else Like

      <asp:buttonfield buttontype="Link" commandname="Del" text="Delete">
      

      然后改成(e.CommandName == "Del") 因为“DELETE”保留给 SQlDataSource 控件

      【讨论】:

        【解决方案5】:

        这个错误是因为你写了命令名delete。所以你写了像删除记录这样的东西。并且当您使用 SQLDataSource 时,该时间命令不应与删除或更新相同。请使用替代名称。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-10-15
          • 2017-10-23
          • 1970-01-01
          • 1970-01-01
          • 2012-08-31
          • 1970-01-01
          • 2014-07-26
          相关资源
          最近更新 更多