Soner Gönül
Soner Gönül

Reputation: 98740

Gridview Delete Button in ASP.NET

I just asked a question about this subject; All Row has a Delete Button in Gridview

I have a simple table AVUKAT

Columns --> HESAP, MUSTERI, AVUKAT

And I show the data in a Gridview.

I activate AutoGenerateDeleteButton.

enter image description here

But when I click the Delete button for a row, I am getting an error like this.

Server Error in '/' Application.
Deleting is not supported by data source 'GridviewDataSource' unless DeleteCommand is specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: Deleting is not supported by data source 'GridviewDataSource' unless DeleteCommand is specified.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Which Function is activated when I click the delete button?

What should be the code of this function?

Upvotes: 2

Views: 9258

Answers (3)

Abhi
Abhi

Reputation: 11

Apart from this remember to use a primary key in your table on which you will be running this Delete command. Cause in the absence of Primary key , delete command will throw an exception as the query generated might be ambiguous if more than one row will have the same value (In fact you wont be able to attach In-built DELETE command until unless your Table has a primary key. )

Upvotes: 1

NakedBrunch
NakedBrunch

Reputation: 49413

Take a look at this MSDN article: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.deletecommand.aspx#Y725

It describes setting up the SQLDataSource to delete data from the database.

Update

Your SQLDataSource is missing "Delete Parameters" (MSDN Link).

Update your SQLDataSource to include DeleteParameters as follows (you'll need to update things like the ConnectionString and ControlIDs of the ControlParameters):

<asp:SqlDataSource
   id="SqlDataSource1"
   runat="server"
   ConnectionString="myConnectionString"
   SelectCommand="SELECT * FROM [AVUKAT] ORDER BY [MUSTERI]" 
   DeleteCommand="DELETE FROM [AVUKAT] WHERE MUSTERI = @MUSTERI AND AVUKAT = @AVUKAT AND HESAP = @HESAP">
   <DeleteParameters>
      <asp:ControlParameter Name="MUSTERI" ControlId="DropDownListID" PropertyName="SelectedValue" />
      <asp:ControlParameter Name="AVUKAT" ControlId="DropDownListID" PropertyName="SelectedValue" />
      <asp:ControlParameter Name="HESAP" ControlId="DropDownListID" PropertyName="SelectedValue" />
   </DeleteParameters>
</asp:SqlDataSource>

Upvotes: 2

Joel Etherton
Joel Etherton

Reputation: 37523

You need to implement the RowDeleting event as described in this tutorial.

Upvotes: 0

Related Questions