Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
im having this problem, whenever i try to delete something from my gridview, this error shows, inserting and modifying does not have any errors.

XML
<table style="width:100%;">
             <tr>
                    <td class="style24" colspan="2">
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                            ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
                            DeleteCommand="DELETE FROM [product] WHERE [productid] = @productid"
                            InsertCommand="INSERT INTO [product]([productid], [productname], [productdesc], [productprice]) VALUES (@productid, @productname, @productdesc, @productprice)"
                            SelectCommand="SELECT * FROM [product]"
                            UpdateCommand="UPDATE [product] SET [productid] = @productid, [productname] = @productname, [productdesc] = @productdesc, [productprice] = @productprice WHERE [productid] = @productid">
                            <DeleteParameters>
                                <asp:Parameter Name="productid" />
                            </DeleteParameters>
                            <UpdateParameters>
                                <asp:Parameter Name="productid" />
                                <asp:Parameter Name="productname" />
                                <asp:Parameter Name="productdesc" />
                                <asp:Parameter Name="productprice" />
                            </UpdateParameters>
                            <InsertParameters>
                                <asp:Parameter Name="productid" />
                                <asp:Parameter Name="productname" />
                                <asp:Parameter Name="productdesc" />
                                <asp:Parameter Name="productprice" />
                            </InsertParameters>
                        </asp:SqlDataSource>
                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onDeleteCommand="GridView1_onDeleteCommand"
                            DataSourceID="SqlDataSource1">
                            <Columns>
                                <asp:BoundField DataField="productid" HeaderText="productid" InsertVisible="False" ReadOnly="True"
                                    SortExpression="productid" />
                                    <asp:ImageField DataImageUrlField="productid" DataImageUrlFormatString="~/images/product/{0}.jpg">
                    </asp:ImageField>
                                <asp:BoundField DataField="productname" HeaderText="productname"
                                    SortExpression="productname" />
                                <asp:BoundField DataField="productdesc" HeaderText="productdesc"
                                    SortExpression="productdesc" />
                                <asp:BoundField DataField="productprice" HeaderText="productprice"
                                    SortExpression="productprice" />
                                <asp:CommandField ShowEditButton="True" />
                                <asp:CommandField DeleteImageUrl="~/images/product/{0}.jpg"
                                    ShowDeleteButton="True" />
                                    </Columns>
                        </asp:GridView>
                        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1"
                Height="51px" Width="348px" DefaultMode="Insert">
                <Fields>
                    <asp:TemplateField HeaderText="Image">
                        <InsertItemTemplate>
                            <asp:FileUpload ID="FileUpload2" runat="server" Width="196px" />&nbsp;
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="productid" HeaderText="productid"
                        SortExpression="productid" />
                    <asp:BoundField DataField="productname" HeaderText="productname"
                        SortExpression="productname" />
                    <asp:BoundField DataField="productdesc" HeaderText="productdesc"
                        SortExpression="productdesc" />
                    <asp:BoundField DataField="productprice" HeaderText="productprice"
                        SortExpression="productprice" />
                    <asp:CommandField NewText="Insert" ShowInsertButton="True" ShowCancelButton="False" CancelText="" />
                </Fields>
            </asp:DetailsView>
                    </td>
                </tr>
            </table>


thks in advance
Posted

1 solution

problem solved, add in to the gridview, where

The code is underlined

<asp:removed id="GridView1" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">DataKeyNames="productid" onDeleteCommand="GridView1_onDeleteCommand"
                            DataSourceID="SqlDataSource1"></asp:removed>


The delete command gives this error because it could not find the variable '@productid' that it could pass to its DELETE query.

So , when the command DataKeyNames="productid" is inserted, it will allow the delete command to work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900