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

C

chike_oji

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(GridViewPageEventArgs
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.RaisePostBackEvent(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.
 
P

Peter Bromberg [C# MVP]

I'm not sure if this is really a C# language group question since it is
really about ASP.NET.
However, I'd say first of all that storing your dataasource directly in
ViewState isn't a good idea.
It "gloms up" the page with lots of content that you could store on the
server just as easily, for example is Session State.

Second, your GridView is generating the PageIndexChanging event but
apparently you've got no code to handle it.
You might want to look that event up (or PageIndexChanged, which might be
more useful to rebind your GridView
with the new page index set) over at MSDN.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com


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(GridViewPageEventArgs
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.RaisePostBackEvent(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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top