You don't have to do any of that when deleting records, as the Gridview supports deleting records directly through the datasource to which it is bound.
Let's say you had a list of products, and the primary key is called ProductID. By specifying the DataKeyNames="ProductID" in the Gridview, you can configure it to automatically call the Delete method of the datasource, passing the primary key as the parameter.
So, your GridView may look something like this:
Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
DataKeyNames="ProductID" DataSourceID="SqlDataSource1" AutoGenerateDeleteButton="True">
</asp:GridView>
and your datasource would look like this:
Code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [ProductName], [QuantityPerUnit], [ProductID], [UnitPrice] FROM [Products]"
DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID"
<DeleteParameters>
<asp:Parameter Name="ProductID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
This will automatically create the deletebutton, which will automatically call the delete method in the datasource when clicked.
_________________________________
Visit my blog at
http://leedumond.com