Click here to Skip to main content
11,574,473 members (59,049 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB SQL Gridview
I have a gridview that has an add row button in the footer row with 3 of the columns using dropdownlist and the rest textboxes. I have tested it and everything is working. When I add a search text box and button to allow the user to search the gridview, the code works and it repopulates the gridview with records that match the users input. The proplem i am having is that after i added the search box I get an error when I click the add record button.

The error I get states:
[ArgumentException: Invalid postback or callback argument. Event validation is enabled using 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.]
 System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +144 System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +111
 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +29
 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
My html code is:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
<div>
 
        <br />
    <asp:TextBox ID="TextBox15" runat="server" ></asp:TextBox>
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <br />
        <br />
        <br />
        <br />
        <br />
 
        <asp:GridView ID="Certificate" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CertificateID" DataSourceID="SqlDataSource2" ShowFooter="True">
            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:Button ID="AddRow" runat="server" CommandName="Insert" Text="Add" />
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Customs Entry" SortExpression="Customs_Entry">
                    <HeaderTemplate>
 
                    </HeaderTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Customs_Entry")%>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TxtCustoms_Entry" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Customs_Entry")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date Certificate" SortExpression="Date_Certificate">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Date_Certificate")%>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtDate_Certificate" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Date_Certificate")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Invoice Number" SortExpression="Invoice_Number">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Invoice_Number")%>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtInvoice_Number" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Invoice_Number")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Product Number" SortExpression="Product_Number">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Product_Number")%>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtProduct_Number" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("Product_Number")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Product Name" SortExpression="Product_Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Product_Name")%>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtProduct_Name" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("Product_Name")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Product Category" SortExpression="Product_Category">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("Product_Category") %>'>
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString2 %>" SelectCommand="SELECT [Name] FROM [Category]"></asp:SqlDataSource>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="txtProduct_Category" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Name">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString2 %>" SelectCommand="SELECT [Name] FROM [Category]"></asp:SqlDataSource>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label6" runat="server" Text='<%# Bind("Product_Category")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CPSC Regulation" SortExpression="CPSC_Regulation">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("CPSC_Regulation")%>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TxtCPSC_Regulation" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label7" runat="server" Text='<%# Bind("CPSC_Regulation")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date Manufactured" SortExpression="Date_Manufactured">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Date_Manufactured")%>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TxtDate_Manufactured" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label8" runat="server" Text='<%# Bind("Date_Manufactured")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Manufacturer" SortExpression="Manufacturer">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3" DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("Manufacturer")%>'>
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString2 %>" SelectCommand="SELECT [Name] FROM [Manufacturer]"></asp:SqlDataSource>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="txtManufacturer" runat="server" DataSourceID="SqlDataSource3" DataTextField="Name" DataValueField="Name">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString2 %>" SelectCommand="SELECT [Name] FROM [Manufacturer]"></asp:SqlDataSource>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label9" runat="server" Text='<%# Bind("Manufacturer")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="Email" SortExpression="Date_Tested">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("Date_Tested")%>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtDate_Tested" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label10" runat="server" Text='<%# Bind("Date_Tested")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="Test Lot ID" SortExpression="Test_Lot_ID">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox11" runat="server" Text='<%# Bind("Test_Lot_ID")%>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TxtTest_Lot_ID" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label11" runat="server" Text='<%# Bind("Test_Lot_ID")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="Name Laboratory" SortExpression="Name_Laboratory">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource4" DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("Name_Laboratory")%>'>
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString2 %>" SelectCommand="SELECT [Name] FROM [Laboratory]"></asp:SqlDataSource>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="txtName_Laboratory" runat="server" DataSourceID="SqlDataSource4" DataTextField="Name" DataValueField="Name">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString2 %>" SelectCommand="SELECT [Name] FROM [Laboratory]"></asp:SqlDataSource>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label12" runat="server" Text='<%# Bind("Name_Laboratory")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString %>" DeleteCommand="DELETE FROM [Certificate] WHERE [CertificateID] = @CertificateID" InsertCommand="INSERT INTO [Certificate] ([Customs_Entry], [Date_Certificate], [Invoice_Number], [Product_Number], [Product_Name], [Product_Category], [CPSC_Regulation], [Date_Manufactured], [Manufacturer], [Date_Tested], [Test_Lot_ID], [Name_Laboratory]) VALUES (@Customs_Entry, @Date_Certificate, @Invoice_Number, @Product_Number, @Product_Name, @Product_Category, @CPSC_Regulation, @Date_Manufactured, @Manufacturer, @Date_Tested, @Test_Lot_ID, @Name_Laboratory)" SelectCommand="SELECT * FROM [Certificate]" UpdateCommand="UPDATE [Certificate] SET [Customs_Entry] = @Customs_Entry, [Date_Certificate] = @Date_Certificate, [Invoice_Number] = @Invoice_Number, [Product_Number] = @Product_Number, [Product_Name] = @Product_Name, [Product_Category] = @Product_Category, [CPSC_Regulation] = @CPSC_Regulation, [Date_Manufactured] = @Date_Manufactured, [Manufacturer] = @Manufacturer, [Date_Tested] = @DatE_Tested, [Test_Lot_ID] = @Test_lot_ID, [Name_Laboratory] = @Name_Laboratory WHERE [CertificateID] = @CertificateID">
            <DeleteParameters>
                <asp:Parameter Name="CertificateID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Customs_Entry" Type="String" />
                <asp:Parameter Name="Date_Certificate" Type="String" />
                <asp:Parameter Name="Invoice_Number" Type="String" />
                <asp:Parameter Name="Product_Number" Type="String" />
                <asp:Parameter Name="Product_Name" Type="String" />
                <asp:Parameter Name="Product_Category" Type="String" />
                <asp:Parameter Name="CPSC_Regulation" Type="String" />
                <asp:Parameter Name="Date_Manufactured" Type="String" />
                <asp:Parameter Name="Manufacturer" Type="String" />
                <asp:Parameter Name="Date_Tested" Type="String" />
                <asp:Parameter Name="Test_Lot_ID" Type="String" />
                <asp:Parameter Name="Name_Laboratory" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Customs_Entry" Type="String" />
                <asp:Parameter Name="Date_Certificate" Type="String" />
                <asp:Parameter Name="Invoice_Number" Type="String" />
                <asp:Parameter Name="Product_Number" Type="String" />
                <asp:Parameter Name="Product_Name" Type="String" />
                <asp:Parameter Name="Product_Category" Type="String" />
                <asp:Parameter Name="CPSC_Regulation" Type="String" />
                <asp:Parameter Name="Date_Manufactured" Type="String" />
                <asp:Parameter Name="Manufacturer" Type="String" />
                <asp:Parameter Name="Date_Tested" Type="String" />
                <asp:Parameter Name="Test_Lot_ID" Type="String" />
                <asp:Parameter Name="Name_Laboratory" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
 
    </div>
    </form>
</body>
</html>
My vb code is:
Imports System
Imports System.Data
Imports System.Data.SqlClient
 
Public Class WebForm3
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If TextBox15.Text = "" Then
            Certificate.DataSourceID = "SqlDataSource2"
            SqlDataSource2.SelectCommand = "select * from Certificate"
            Certificate.DataBind()
        Else
            Certificate.DataSourceID = "SqlDataSource2"
            SqlDataSource2.SelectCommand = "select * from Certificate where Customs_Entry='" & TextBox15.Text & "'"
            Certificate.DataBind()
        End If
 
    End Sub
 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Certificate.DataSourceID = "SqlDataSource2"
        SqlDataSource2.SelectCommand = "select * from Certificate where Customs_Entry='" & TextBox15.Text & "'"
        Certificate.DataBind()
    End Sub
 
    Private Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles Certificate.RowCommand
        ' Insert data if the CommandName == "Insert"
        ' and the validation controls indicate valid data...
        If e.CommandName = "Insert" AndAlso Page.IsValid Then
            ' Insert new record...
            SqlDataSource2.Insert()
        End If
    End Sub
 
    Protected Sub SqlDataSource2_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource2.Inserting
        ' Programmatically reference Web controls in the inserting interface...
        Dim NewCustoms_Entry As TextBox = Certificate.FooterRow.FindControl("txtCustoms_Entry")
        Dim NewDate_Certificate As TextBox = Certificate.FooterRow.FindControl("txtDate_Certificate")
        Dim NewInvoice_Number As TextBox = Certificate.FooterRow.FindControl("txtInvoice_Number")
        Dim NewProduct_Number As TextBox = Certificate.FooterRow.FindControl("txtProduct_Number")
        Dim NewProduct_Name As TextBox = Certificate.FooterRow.FindControl("txtProduct_Name")
        Dim NewProduct_Category As DropDownList = Certificate.FooterRow.FindControl("txtProduct_Category")
        Dim NewCPSC_Regulation As TextBox = Certificate.FooterRow.FindControl("txtCPSC_Regulation")
        Dim NewDate_Manufactured As TextBox = Certificate.FooterRow.FindControl("txtDate_Manufactured")
        Dim NewManufacturer As DropDownList = Certificate.FooterRow.FindControl("txtManufacturer")
        Dim NewDate_Tested As TextBox = Certificate.FooterRow.FindControl("txtDate_Tested")
        Dim NewTest_Lot_ID As TextBox = Certificate.FooterRow.FindControl("txtTest_Lot_ID")
        Dim NewName_Laboratory As DropDownList = Certificate.FooterRow.FindControl("txtName_Laboratory")
 
        ' Set the ObjectDataSource's InsertParameters values...
        e.Command.Parameters("@Customs_Entry").Value = NewCustoms_Entry.Text
        e.Command.Parameters("@Date_Certificate").Value = NewDate_Certificate.Text
        e.Command.Parameters("@Invoice_Number").Value = NewInvoice_Number.Text
        e.Command.Parameters("@Product_Number").Value = NewProduct_Number.Text
        e.Command.Parameters("@Product_Name").Value = NewProduct_Name.Text
        e.Command.Parameters("@Product_Category").Value = Convert.ToString(NewProduct_Category.SelectedValue)
        e.Command.Parameters("@CPSC_Regulation").Value = NewCPSC_Regulation.Text
        e.Command.Parameters("@Date_Manufactured").Value = NewDate_Manufactured.Text
        e.Command.Parameters("@Manufacturer").Value = Convert.ToString(NewManufacturer.SelectedValue)
        e.Command.Parameters("@Date_Tested").Value = NewDate_Tested.Text
        e.Command.Parameters("@Test_Lot_ID").Value = NewTest_Lot_ID.Text
        e.Command.Parameters("@Name_Laboratory").Value = Convert.ToString(NewName_Laboratory.SelectedValue)
 
    End Sub
 

End Class
Posted 25-Sep-12 4:43am
Edited 25-Sep-12 4:49am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Now, there could be few reasons why you are getting this issue. Easy/direct solution can be adding enableEventValidation="false" in your Page directive or Web.config file.

But, it is always good to hit the root cause. It might be that you are rebinding your grid on every postback. Thus, before RowCommand fire your GridView will rebind and all control within it will be assigned to new id. So RowCommand could not get the item which have fired the event. To avoid this, all you need to do is put your grid bind line in IsPostBack condition.
if (!IsPostBack)
{
      //Your code for Bind data
}

Detailed explanation here: Invalid postback or callback argument solution. Event validation...[^]

Interesting to read one blog entry related to same: “Invalid postback or callback argument” in ASP.NET[^] - It exposes few other reasons on why one can get the same error. Nice read.
  Permalink  

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

  Print Answers RSS


Advertise | Privacy | Mobile
Web04 | 2.8.150624.2 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100