1

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?

5 Answers 5

5

if you change it to something like... CommandName="DeleteRow" or something.. and then change it to if(e.CommandName == "DeleteRow") you will bypass that problem.

Sign up to request clarification or add additional context in comments.

2 Comments

This did the trick for me although I have no idea why. I guess when the CommandName equals "Delete" it does some assumptions about requiring a Delete command to be filled in?
Thank you, it worked for me, too when I changed "Delete" to something other.
1

You have to put this in your ASP code:

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

2 Comments

I get this error: Element 'ButtonColumn' is not a known element.
Oups sorry, this was for a DataGrid, for a GridView, try using this : <asp:CommandField DeleteText="Delete" ShowDeleteButton="True"></asp:CommandField> . Look at this article for some nice examples : msdn.microsoft.com/en-us/library/ms972940.aspx
0

you need to change it to something Else Like

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

and then change it to (e.CommandName == "Del") because "DELETE" Reserved to SQlDataSource Control

Comments

0

If you choose "ShowDeleteButton" under the command field properties of the Select field "Delete" will show on the left of each row on the GridView. However, when you do that there is an expectation that the SQL Data Source will have a a delete command. If not, you get the nasty message about the delete command not being specified.

Since I wanted the business Oobject to handle the delete rather than the SQL data source, I instead used the select, just like you would to select a row, then I changed the "SelectText" under command field properties to say "Delete". That allowed me to use the RowCommand event to catch the delete action. From there I just grabbed the row info I needed, and passed it to the business object for the delete.

Note that when you do it that way the CommandName is still "Select" even though the commandtext is "Delete". So you check as follows:

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

Comments

-1

This error occurs because of you write command name delete. so you write something like Delete Record. and when you use SQLDataSource that time command should be not same as delete or update. Please use alternative name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.