Click here to Skip to main content
16,011,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a gridrow with a button in a template field. I want this button to delete from SQL but cannot get it working. Only getting error messages. Here is my code.

ASP.NET
<asp:GridView ID="gvBasket" runat="server" AutoGenerateColumns="False">
     <Columns>
            <asp:BoundField DataField="ProductName" HeaderText="Product Name" SortExpression="ProductName" />
            <asp:BoundField DataField="qty" HeaderText="Quantity" SortExpression="qty" />
            <asp:BoundField DataField="UnitPrice" HeaderText="Unit Prce" SortExpression="unitPrice"
                HtmlEncode="false" DataFormatString="{0:c}" />
            <asp:TemplateField HeaderText="Total" SortExpression="Total">
                <ItemTemplate>
                    <asp:Label ID="lblTotal" runat="server" Text='<%# Eval("Total", "{0:c}") %>'  />
                </ItemTemplate>
                <FooterTemplate>
                <asp:Label ID="lblsubTotal" runat="server" />

                </FooterTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="sessionID" HeaderText="Session" 
                SortExpression="sessionID" visible="false"/>
            <asp:BoundField DataField="UserID" HeaderText="user" SortExpression="UserID" Visible="false" />
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                SortExpression="ProductID" visible="false"/>
            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                    <asp:Button ID="Delete" runat="server" 
                        CommandArgument='<%# Bind("productID") %>' onclick="Delete_Click" 
                        Text="Delete" CommandName="DeleteItem" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#003399" />
    </asp:GridView>


my code behind is
C#
protected void Delete_Click(object sender, EventArgs e)
   {
       if (!Page.IsPostBack)
       {

           Button btn = (Button)sender;
           int id = int.Parse(btn.CommandArgument);

           string Query = "DELETE FROM ShoppingBasket WHERE Date IN (SELECT TOP 1 Date FROM shoppingBasket WHERE  ProductID = " + id + " AND UserID = '" + (string)Session["UserName"] + "' ORDER BY Date DESC)";

           ConnectionHandler objHandler = new ConnectionHandler();
           DataTable dt = objHandler.ExecuteSelect(Query);
       }


   }


the error message is:

Invalid postback or callback argument. Event validation is enabled using <pages enableeventvalidation="true"> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

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.
Posted

1 solution

make it false in your aspx page..
HTML
Page EnableEventValidation="false"
 
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