Wednesday, March 7, 2012

How to Use Delete Parameters

Hey gang I am trying to use the delete feature of a sqldatasource and having issues with the delete feature. My code is below the issue is if I just call the delete and don't have a paramter in my delete statement then it deletes the records in my table. However when I add a parameter to my delete query (@.maID) and try to set it then it doesn't delete the record. What am I doing wrong? The e.CommandArgument is passing the correct record ID (which is an Integer)....can someone please help?

PS I also tried without the @. symbol before my parameter and no luck either.

DELETE FROM tempMbrAccounts WHERE (maID = @.maID) is my delete query

Public Sub OnDeleteButtonClick(ByVal senderAs Object,ByVal eAs GridViewCommandEventArgs)Handles gvEnterAcct.RowCommandDim RecIDAs Integer If e.CommandName ="Delete"Then RecID = e.CommandArgument dsInsert.DeleteParameters.Add("@.maID", RecID) dsInsert.Delete() tempAcctTable()End If End Sub
If maID is your table's primary key, SetDataKeyNames="maID" for your gridview. You should be able to use the default DelectCommand of SqlDataSource without adding extra code.|||

I should add that I an not using the standard delete button that is implemented in a GridView. I am using an imagebutton.

I do have the datakey set to maID but that isn't working either.

|||

By using an ImageButton in a TemplateField, you can still use the standard way to do delete. Here is a sample which is working . ID is the Primary Key of the test table.

<div>

<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataKeyNames="ID"DataSourceID="SqlDataSource1"><Columns><asp:CommandFieldShowEditButton="True"/><asp:BoundFieldDataField="ID"HeaderText="ID" ReadOnly="True"SortExpression="ID"/><asp:BoundFieldDataField="myCol"HeaderText="myCol"SortExpression="myCol"/><asp:TemplateField><ItemTemplate><asp:ImageButtonid="DeleteImgBtn"CommandName="Delete"ImageUrl="="~/images/delete.gif"runat="server"/></ItemTemplate>

</

asp:TemplateField>

</Columns></asp:GridView><asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:mytestConnectionString %>"DeleteCommand="DELETE FROM [test] WHERE [ID] = @.ID"SelectCommand="SELECT [ID], [myCol] FROM [test]"><DeleteParameters><asp:ParameterName="ID"Type="Int32"/></DeleteParameters></asp:SqlDataSource></div>

No comments:

Post a Comment