Search code examples
c#asp.netgridviewpaginationobjectdatasource

ASP.NET ObjectDataSource exception on pagination event


ASP.Net is throwing this error when paginating a gridView. It loads the first chunk of elements fine, but fails when clicking on any page number. I have a similar Page implementing ViewAllEvents which has no parameters and runs fine. So I guess it has to do with the parameter adding part of the codebehind.

ObjectDataSource 'pbpDataSource' could not find a non-generic method 'FindEvents' that has parameters: keywords, keywords1, count, start."} System.Exception {System.InvalidOperationException}

Page code:

    <%@ Page Language="C#" MasterPageFile="~/Sportacus.Master" AutoEventWireup="true"
    Codebehind="ViewEvents.aspx.cs" Inherits="Es.Udc.DotNet.Sportacus.Web.Pages.Event.ViewEvents"
    meta:resourcekey="Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_MenuExplanation"
    runat="server">
    -
    <asp:Localize ID="lclMenuExplanation" runat="server" meta:resourcekey="lclMenuExplanation" />
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder_BodyContent"
    runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="gvEvents" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
            AutoGenerateColumns="False" onpageindexchanging="gvEvents_PageIndexChanging" ShowHeaderWhenEmpty="true">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
            <Columns>
                <asp:HyperLinkField DataNavigateUrlFields="Id" HeaderText="<%$ Resources:, hdrEventName %>" DataNavigateUrlFormatString="SingleEvent.aspx?evnId={0}" DataTextField="Name" />
                <asp:BoundField DataField="Category.catName" HeaderText ="<%$ Resources:, hdrCategoryName %>"/>
                <asp:BoundField DataField="Date" HeaderText ="<%$ Resources:, hdrEventDate %>"/>
            </Columns>
        </asp:GridView>
        <asp:ObjectDataSource ID="pbpDataSource" runat="server"></asp:ObjectDataSource>
    </form>
</asp:Content>

Code Behind:

using System;
using System.Web.Security;

using Es.Udc.DotNet.Sportacus.Web.HTTP.Session;
using Es.Udc.DotNet.ModelUtil.Exceptions;
using Es.Udc.DotNet.Sportacus.Model.UserService.Exceptions;
using System.Collections.Generic;
using Es.Udc.DotNet.Sportacus.Model.EventService;
using Microsoft.Practices.Unity;
using System.Web;
using System.Web.UI.WebControls;
using Es.Udc.DotNet.Sportacus.Web.Properties;
using System.Reflection;
using System.Data;


namespace Es.Udc.DotNet.Sportacus.Web.Pages.Event
{

    public partial class ViewEvents : SpecificCulturePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                pbpDataSource.ObjectCreating += this.PbpDataSource_ObjectCreating;
                pbpDataSource.TypeName =
                    Settings.Default.ObjectDS_Event_Service;

                pbpDataSource.EnablePaging = true;

                pbpDataSource.SelectMethod =
                    Settings.Default.ObjectDS_Event_SelectMethod;

                pbpDataSource.SelectCountMethod =
                    Settings.Default.ObjectDS_Event_SelectCountMethod;
                pbpDataSource.StartRowIndexParameterName = Settings.Default.Sportacus_startIndex;
                pbpDataSource.MaximumRowsParameterName = Settings.Default.Sportacus_count;

                long catId = Convert.ToInt32(Request.Params.Get("catId"));
                String keywords = Request.Params.Get("keywords");

                pbpDataSource.SelectParameters.Add("keywords", DbType.String, keywords);

                if (catId != -1)
                {
                    pbpDataSource.SelectParameters.Add("categoryId", DbType.Int64, catId.ToString());
                }


                gvEvents.AllowPaging = true;
                gvEvents.PageSize = Settings.Default.Sportacus_defaultCount;

                gvEvents.DataSource = pbpDataSource;
                gvEvents.DataBind();
            }
            catch (TargetInvocationException)
            {
                //TODO Etiqueta de error
            }

}
        protected void gvEvents_PageIndexChanging(object sender,GridViewPageEventArgs e)
        {
            gvEvents.PageIndex= e.NewPageIndex;
            gvEvents.DataBind();
        }    




        protected void PbpDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            /* Get the Service */
            IUnityContainer container =
                (IUnityContainer)HttpContext.Current.
                    Application["unityContainer"];
            IEventService eventService = new EventService();

            eventService = (IEventService)container.BuildUp(eventService.GetType(), eventService, "IEventService");

            e.ObjectInstance = (IEventService)eventService;
            }

        }
    }

I'm not posting config files, but they are triple-checked so, that shouldn't be the issue.

Why is the page asking for keywords1? How do I fix this?

PS. Don't worry about the Method not asking for the catId parameter, the Method is overloaded so it can work with or without that parameter.

EDIT: Stack Trace

User code did not control System.InvalidOperationException
  HResult=-2146233079
  Message=ObjectDataSource 'pbpDataSource' could not find a non-generic method 'FindEvents' that has parameters: keywords, keywords1, count, start.
  Source=System.Web
  StackTrace:
       in System.Web.UI.WebControls.ObjectDataSourceView.GetResolvedMethodData(Type type, String methodName, IDictionary allParameters, DataSourceOperation operation)
       en System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)
       en System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
       en System.Web.UI.WebControls.DataBoundControl.PerformSelect()
       en System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
       en System.Web.UI.WebControls.GridView.DataBind()
       en Es.Udc.DotNet.Sportacus.Web.Pages.Event.ViewEvents.Page_Load(Object sender, EventArgs e) en c:\Users\Trigork\Documents\Visual Studio 2013\Projects\Sportacus\Web\Pages\Event\ViewEvents.aspx.cs:line 61
       en System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       en System.Web.UI.Control.OnLoad(EventArgs e)
       en System.Web.UI.Control.LoadRecursive()
       en System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

If you're wondering, Line 61 is gvEvents.DataBind();

Request.Params string just before the exception is thrown

"keywords=a&catId=-1&__EVENTTARGET=ctl00%24ContentPlaceHolder_BodyContent%24gvEvents&__EVENTARGUMENT=Page%242&__VIEWSTATE=%2fwEPDwUKMTQ2NjcwNTU2NQ9kFgJmD2QWBAIJD2QWCAIFDw8WAh4HVmlzaWJsZWhkZAIHDw8WAh8AaGRkAgkPDxYCHwBoZGQCCw8PFgIfAGhkZAILD2QWAgIBD2QWBAIBDzwrABEDAA8WCB4LQWxsb3dQYWdpbmdnHghQYWdlU2l6ZQICHgtfIURhdGFCb3VuZGceC18hSXRlbUNvdW50AgNkARAWAQIBFgE8KwAFAQAWAh4KSGVhZGVyVGV4dAUKQ2F0ZWdvcsOtYRYBZgwUKwAAFgJmD2QWBgIBD2QWBmYPZBYCZg8PFgQeBFRleHQFDUJhcsOnYS1NYWRyaWQeC05hdmlnYXRlVXJsBRhTaW5nbGVFdmVudC5hc3B4P2V2bklkPTNkZAIBDw8WAh8GBQZGdXRib2xkZAICDw8WAh8GBRIwNi8wOC8yMDE1IDA6MDA6MDBkZAICD2QWBmYPZBYCZg8PFgQfBgUTQmFza2V0IHF1ZSB0ZSBjYWdhcx8HBRhTaW5nbGVFdmVudC5hc3B4P2V2bklkPTJkZAIBDw8WAh8GBQZCYXNrZXRkZAICDw8WAh8GBRIwOC8wNi8yMDE1IDA6MDA6MDBkZAIDDw8WAh8AaGRkAgMPD2QPEBYBZhYBFggeBE5hbWUFCGtleXdvcmRzHgZEYlR5cGULKWJTeXN0ZW0uRGF0YS5EYlR5cGUsIFN5c3RlbS5EYXRhLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4ORAeDERlZmF1bHRWYWx1ZQUBYR4OUGFyYW1ldGVyVmFsdWVkFgECA2RkGAEFLWN0bDAwJENvbnRlbnRQbGFjZUhvbGRlcl9Cb2R5Q29udGVudCRndkV2ZW50cw88KwAMAQgCAmS8b0K%2fvi64Lbx%2fNz1XtOASXI%2f%2bXvPkYPQRF9btH03y7A%3d%3d&__EVENTVALIDATION=%2fwEdAALeVFTvnmTv0c3DzfBr2KnSl7v1kH35r4CKJ9pINf58%2bwkrHsyc7srY52vIBt2HtEPSwzWAi%2fkIjMHD2HPNVjjw&AspxAutoDetectCookieSupport=1&ASP.NET_SessionId=b5opxek5wofqswxdekptvvcw&.ASPXAUTH=5695CBAE7ED635548F37EC4DABD548CA6ECE2446D099EBCF528026246DF38E1F2F98E54CCA20C5EC6D354D71A78B084A9107C2BFF664D22D1CF0681B67FF825F659B64A325652376284FC680AC8518615F958A39528C61E944B9756B890B8B8E6D266DCDD72781CD31EF555C1EBEB42A530BC74A75DAE14DCDCAF702CDAFD224A16D1BB3CB0B224618B6CC920E6E7502&ALL_HTTP=HTTP_CACHE_CONTROL%3amax-age%3d0%0d%0aHTTP_CONNECTION%3akeep-alive%0d%0aHTTP_CONTENT_LENGTH%3a1224%0d%0aHTTP_CONTENT_TYPE%3aapplication%2fx-www-form-urlencoded%0d%0aHTTP_ACCEPT%3atext%2fhtml%2capplication%2fxhtml%2bxml%2capplication%2fxml%3bq%3d0.9%2cimage%2fwebp%2c*%2f*%3bq%3d0.8%0d%0aHTTP_ACCEPT_ENCODING%3agzip%2c+deflate%0d%0aHTTP_ACCEPT_LANGUAGE%3aes-ES%2ces%3bq%3d0.8%2cen%3bq%3d0.6%2cja%3bq%3d0.4%0d%0aHTTP_COOKIE%3aAspxAutoDetectCookieSupport%3d1%3b+ASP.NET_SessionId%3db5opxek5wofqswxdekptvvcw%3b+.ASPXAUTH%3d5695CBAE7ED635548F37EC4DABD548CA6ECE2446D099EBCF528026246DF38E1F2F98E54CCA20C5EC6D354D71A78B084A9107C2BFF664D22D1CF0681B67FF825F659B64A325652376284FC680AC8518615F958A39528C61E944B9756B890B8B8E6D266DCDD72781CD31EF555C1EBEB42A530BC74A75DAE14DCDCAF702CDAFD224A16D1BB3CB0B224618B6CC920E6E7502%0d%0aHTTP_HOST%3alocalhost%3a3404%0d%0aHTTP_REFERER%3ahttp%3a%2f%2flocalhost%3a3404%2fPages%2fEvent%2fViewEvents.aspx%3fkeywords%3da%26catId%3d-1%0d%0aHTTP_USER_AGENT%3aMozilla%2f5.0+(Windows+NT+6.2%3b+WOW64)+AppleWebKit%2f537.36+(KHTML%2c+like+Gecko)+Chrome%2f42.0.2311.152+Safari%2f537.36%0d%0aHTTP_ORIGIN%3ahttp%3a%2f%2flocalhost%3a3404%0d%0a&ALL_RAW=Cache-Control%3a+max-age%3d0%0d%0aConnection%3a+keep-alive%0d%0aContent-Length%3a+1224%0d%0aContent-Type%3a+application%2fx-www-form-urlencoded%0d%0aAccept%3a+text%2fhtml%2capplication%2fxhtml%2bxml%2capplication%2fxml%3bq%3d0.9%2cimage%2fwebp%2c*%2f*%3bq%3d0.8%0d%0aAccept-Encoding%3a+gzip%2c+deflate%0d%0aAccept-Language%3a+es-ES%2ces%3bq%3d0.8%2cen%3bq%3d0.6%2cja%3bq%3d0.4%0d%0aCookie%3a+AspxAutoDetectCookieSupport%3d1%3b+ASP.NET_SessionId%3db5opxek5wofqswxdekptvvcw%3b+.ASPXAUTH%3d5695CBAE7ED635548F37EC4DABD548CA6ECE2446D099EBCF528026246DF38E1F2F98E54CCA20C5EC6D354D71A78B084A9107C2BFF664D22D1CF0681B67FF825F659B64A325652376284FC680AC8518615F958A39528C61E944B9756B890B8B8E6D266DCDD72781CD31EF555C1EBEB42A530BC74A75DAE14DCDCAF702CDAFD224A16D1BB3CB0B224618B6CC920E6E7502%0d%0aHost%3a+localhost%3a3404%0d%0aReferer%3a+http%3a%2f%2flocalhost%3a3404%2fPages%2fEvent%2fViewEvents.aspx%3fkeywords%3da%26catId%3d-1%0d%0aUser-Agent%3a+Mozilla%2f5.0+(Windows+NT+6.2%3b+WOW64)+AppleWebKit%2f537.36+(KHTML%2c+like+Gecko)+Chrome%2f42.0.2311.152+Safari%2f537.36%0d%0aOrigin%3a+http%3a%2f%2flocalhost%3a3404%0d%0a&APPL_MD_PATH=%2fLM%2fW3SVC%2f8%2fROOT&APPL_PHYSICAL_PATH=C%3a%5cUsers%5cTrigork%5cDocuments%5cVisual+Studio+2013%5cProjects%5cSportacus%5cWeb%5c&AUTH_TYPE=Forms&AUTH_USER=Trigork&AUTH_PASSWORD=&LOGON_USER=Trigork&REMOTE_USER=Trigork&CERT_COOKIE=&CERT_FLAGS=&CERT_ISSUER=&CERT_KEYSIZE=&CERT_SECRETKEYSIZE=&CERT_SERIALNUMBER=&CERT_SERVER_ISSUER=&CERT_SERVER_SUBJECT=&CERT_SUBJECT=&CONTENT_LENGTH=1224&CONTENT_TYPE=application%2fx-www-form-urlencoded&GATEWAY_INTERFACE=CGI%2f1.1&HTTPS=off&HTTPS_KEYSIZE=&HTTPS_SECRETKEYSIZE=&HTTPS_SERVER_ISSUER=&HTTPS_SERVER_SUBJECT=&INSTANCE_ID=8&INSTANCE_META_PATH=%2fLM%2fW3SVC%2f8&LOCAL_ADDR=%3a%3a1&PATH_INFO=%2fPages%2fEvent%2fViewEvents.aspx&PATH_TRANSLATED=C%3a%5cUsers%5cTrigork%5cDocuments%5cVisual+Studio+2013%5cProjects%5cSportacus%5cWeb%5cPages%5cEvent%5cViewEvents.aspx&QUERY_STRING=keywords%3da%26catId%3d-1&REMOTE_ADDR=%3a%3a1&REMOTE_HOST=%3a%3a1&REMOTE_PORT=17999&REQUEST_METHOD=POST&SCRIPT_NAME=%2fPages%2fEvent%2fViewEvents.aspx&SERVER_NAME=localhost&SERVER_PORT=3404&SERVER_PORT_SECURE=0&SERVER_PROTOCOL=HTTP%2f1.1&SERVER_SOFTWARE=Microsoft-IIS%2f8.0&URL=%2fPages%2fEvent%2fViewEvents.aspx&HTTP_CACHE_CONTROL=max-age%3d0&HTTP_CONNECTION=keep-alive&HTTP_CONTENT_LENGTH=1224&HTTP_CONTENT_TYPE=application%2fx-www-form-urlencoded&HTTP_ACCEPT=text%2fhtml%2capplication%2fxhtml%2bxml%2capplication%2fxml%3bq%3d0.9%2cimage%2fwebp%2c*%2f*%3bq%3d0.8&HTTP_ACCEPT_ENCODING=gzip%2c+deflate&HTTP_ACCEPT_LANGUAGE=es-ES%2ces%3bq%3d0.8%2cen%3bq%3d0.6%2cja%3bq%3d0.4&HTTP_COOKIE=AspxAutoDetectCookieSupport%3d1%3b+ASP.NET_SessionId%3db5opxek5wofqswxdekptvvcw%3b+.ASPXAUTH%3d5695CBAE7ED635548F37EC4DABD548CA6ECE2446D099EBCF528026246DF38E1F2F98E54CCA20C5EC6D354D71A78B084A9107C2BFF664D22D1CF0681B67FF825F659B64A325652376284FC680AC8518615F958A39528C61E944B9756B890B8B8E6D266DCDD72781CD31EF555C1EBEB42A530BC74A75DAE14DCDCAF702CDAFD224A16D1BB3CB0B224618B6CC920E6E7502&HTTP_HOST=localhost%3a3404&HTTP_REFERER=http%3a%2f%2flocalhost%3a3404%2fPages%2fEvent%2fViewEvents.aspx%3fkeywords%3da%26catId%3d-1&HTTP_USER_AGENT=Mozilla%2f5.0+(Windows+NT+6.2%3b+WOW64)+AppleWebKit%2f537.36+(KHTML%2c+like+Gecko)+Chrome%2f42.0.2311.152+Safari%2f537.36&HTTP_ORIGIN=http%3a%2f%2flocalhost%3a3404"

As suggested by the user @rism, I tried to swallow the InvalidOperationException, just to find out that the gridView gvEvents doesn't show up even with the first chunk of results


Solution

  • You should edit your Page_Load in order to not re-add the param keywords when the page is loaded on postback (like when you change the Page).

    EDIT: All what you do on Page_Load stays within the Page (in the ViewState), so it's a good practice to use a if (!IsPostBack) for some parts of code.