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
.
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?
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>
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. )
You need to implement the RowDeleting event as described in this tutorial.
精彩评论