asp.netvb.netgridviewsqldatasource

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


I have an ASP button link in GridView1 that should delete an item from a db table on click, and I am getting this error message:

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

This is the code:

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

<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>

From what I read you need to specify a DELETE command for SQLDataSource1.

How do I do that in my case?


Solution

  • You have to put this in your ASP code:

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