I am trying to use a command button to delete a row from a gridview. When I click the command button though, the page posts but my record is still there... no errors, just nothing seems to happen. Below is what I think is the relevant code, let me know if you need to see anything else.
I'm kinda new to working with gridview objects, it would be nice if I could see what SQL command is acctually ran... maybe there are other things I can check too, I'm just not sure how.
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None"
BorderWidth="1px" CellPadding="4" DataSourceID="cnnToolbar" ForeColor="Black"
GridLines="Vertical" PageSize="50" Width="100%"
<asp:BoundField DataField="FunctionID" HeaderText="FunctionID" InsertVisible="False" ReadOnly="True" SortExpression="FunctionID" Visible="False" />
<asp:CommandField HeaderText="Action" ShowCancelButton="False" ShowDeleteButton="True">
<ItemStyle HorizontalAlign="Center" />
<asp:SqlDataSource ID="cnnToolbar" runat="server" ConnectionString="<%$ ConnectionStrings:CMDB2ConnectionString %>"
DeleteCommand="DELETE FROM dbo.Toolbar WHERE (FunctionID = @FunctionID)"
<asp:Parameter Name="FunctionID" />
http://www.krissteele.net - http://www.steeleits.com - http://www.evilscale.com - http://www.starcraftlive.net
You need to add your Database field to your GridView's DataKeys property for UPDATING and DELETING data with SQL. Set your DataKeys to FunctionID and it should work perfectly.
If you get an answer to your question, please mark it as 'Answer'
www.lymanlax.com | www.possumslax.com
I have this problem too, and it was NOT fixed by setting the DataKeyNames property of the gridview. The DeleteQuery on the data source for my gridview is:
DELETE FROM dbo.bhp_servicecostentry WHERE (servicecostentryid = @Pid)
The servicecostentryid field is definitely part of the data displayed in the grid
I note that in the Commands and Parameters editor, the source for the parameter Pid above is "None". I tried setting it to the gridview's SelectedValue property, but that didn't change anything either. Any idea what else it can be? I have a lot of gridviews in my app that will not delete records!