GridView bound to a Datatable results in runtime error on attempts topage. Options

Discussion in 'ASP .Net' started by chike_oji, Jan 28, 2008.

  1. chike_oji

    chike_oji Guest

    Hello All,

    Please I have a web application
    in which I read an Excel spreadsheet into
    a DataTable and then bind the DataTable to
    a DataGridView.


    Below is a summary of what I did:


    ///////////////////////////////////////////////////////////////////////////­///////////////////////////////////////////////////////////////////////////­//////////////
    if (IsValid)
    {
    UploadID = Guid.NewGuid().ToString();
    string path = string.Format("{0}\\{1}.xls",
    ConfigurationManager.AppSettings["ExcelFilePath"], UploadID);


    try
    {


    if (UploadExcel.PostedFile == null)
    {
    throw new Exception("Please select a File to
    upload");
    }
    UploadExcel.PostedFile.SaveAs(path);
    //Create query to be passed to the Excel conversion
    method.
    string sql = "Select * from [stockdetails$]";
    //Store data from Excel file in a DataTable
    dtData =
    MicrosoftExcelClient.ConvertExcelToDataTable(path, sql);
    //Bind the GridView to the DataTable to display the
    data
    GridView1.DataSource = dtData;
    GridView1.DataBind();


    //Save in ViewState
    ViewState["dtData"] = dtData;
    ///////////////////////////////////////////////////////////////////////////­///////////////////////////////////////////////////////////////////////////­//////////////


    I set AllowPaging to "true" and anytime I execute the
    web application, the first page of data is correctly displayed but
    whenever I try to navaigate to the next page, it results in the
    runtime error below:


    ///////////////////////////////////////////////////////////////////////////­///////////////////////////////////////////////////////////////////////////­//////////////
    Server Error in '/IConceptDBWebUpdate' Application.
    ---------------------------------------------------------------------------­-----


    The GridView 'GridView1' fired event PageIndexChanging which wasn't
    handled.
    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.


    Exception Details: System.Web.HttpException: The GridView 'GridView1'
    fired event PageIndexChanging which wasn't handled.


    Source Error:


    An unhandled exception was generated during the execution of the
    current web request. Information regarding the origin and location of
    the exception can be identified using the exception stack trace
    below.


    Stack Trace:


    [HttpException (0x80004005): The GridView 'GridView1' fired event
    PageIndexChanging which wasn't handled.]


    System.Web.UI.WebControls.GridView.OnPageIndexChanging(GridViewPageEventArg­
    s
    e) +1473555
    System.Web.UI.WebControls.GridView.HandlePage(Int32 newPage) +83
    System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e,
    Boolean
    causesValidation, String validationGroup) +488
    System.Web.UI.WebControls.GridView.RaisePostBackEvent(String
    eventArgument) +199


    System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.Rais­
    ePostBackEvent(String
    eventArgument) +7
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    sourceControl, String eventArgument) +11
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection
    postData)
    +174
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    +5102


    ---------------------------------------------------------------------------­-----
    Version Information: Microsoft .NET Framework Version:2.0.50727.832;
    ASP.NET Version:2.0.50727.832


    ///////////////////////////////////////////////////////////////////////////­///////////////////////////////////////////////////////////////////////////­//////////////


    Any help with how I can achieve paging in this scenario will be
    appreciated.


    Thank you very much,


    Regards,


    Chike.
     
    chike_oji, Jan 28, 2008
    #1
    1. Advertisements

  2. chike_oji

    Manish Guest

    Hi Chike,

    You can page on the Grid by setting the AllowPaging property to TRUE and
    then handling the PageIndexChanging event as

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal
    e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles
    GridView1.PageIndexChanging
    Me.GridView1.PageIndex = e.NewPageIndex
    Me.GridView1.DataBind()
    End Sub

    Regards,
    Manish
    www.ComponentOne.com

     
    Manish, Jan 28, 2008
    #2
    1. Advertisements

  3. chike_oji

    chike_oji Guest

    Thanks a million times Manish, I am so very grateful.
    Your example solved my problem. At a time I was binding to data before
    the

    this.GridView1.PageIndex = e.NewPageIndex

    The result was that I had to click the page link twice before the
    GridView
    moved to the requested page. But with your suggestion, I am getting
    the
    right results, a click to take me to the right page.

    Thank you very much.

    Regards,

    Chike.

     
    chike_oji, Jan 29, 2008
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.