0
votes

I'm trying to use a GridView to show a table pulled from a SQL Server. It is a log of events. I've placed the GridView control on my page along with an ObjectDataSource control. I've configured the :

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title></title>
</head>
<body>
    <form id="Form1" method="post" runat="server">
    <asp:GridView ID="gvHistory" runat="server" DataSourceID="dsHistory">
    </asp:GridView>
    <asp:ObjectDataSource ID="dsHistory" runat="server" SelectMethod="GetHistoryRows"
        TypeName="AspDotNetStorefrontAdmin.ROIImportHistory"></asp:ObjectDataSource>
    </form>
</body>
</html>

I've created a class in the App_Code folder as follows:

Imports System.Data

Namespace AspDotNetStorefrontAdmin

    Public Class ROIImportHistory

        Public Shared Function GetHistoryRows() As DataTable

            Dim localDatatable As New DataTable

            localDatatable.Columns.Add()
            localDatatable.Columns.Add()
            localDatatable.Columns.Add()
            localDatatable.Rows.Add(New Object() {"Hi", "Hi2", "Hi3"})

            Return localDatatable

        End Function
    End Class

End Namespace

However, I get the following exception when I try to run this:

What is going wrong? I thought I was doing a pretty basic thing here. I've tried lots of different options and all that I know is the page works fine if I remove either the GridView or the ObjectDataSource. If I bind the DataTable directly to the GridView things also work correctly.

My purpose is to get true pagination along with filtering. Given that I could be working with more than't want to load a DataTable every time the page loads.

Server Error in '/' Application.
Exception of type 'System.OutOfMemoryException' was thrown.
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.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.

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:


[OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.]
   System.Reflection.Assembly._GetType(String name, Boolean throwOnError, Boolean ignoreCase) +0
   System.Web.UI.Util.GetTypeFromAssemblies(ICollection assemblies, String typeName, Boolean ignoreCase) +201
   System.Web.Compilation.BuildManager.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase) +302
   System.Web.UI.WebControls.ObjectDataSourceView.GetType(String typeName) +70
   System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1692
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +27
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +261
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +95
   System.Web.UI.Control.EnsureChildControls() +146
   System.Web.UI.Control.PreRenderRecursiveInternal() +61
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394


Version Information: Microsoft .NET Framework Version:2.0.50727.6400; ASP.NET Version:2.0.50727.6387 

I've referenced this and this to no avail.

NOTE: I found that this code runs fine loaded into another site on the same server in the same Application Pool. I therefore believe this has to do with a configuration issue.

2

2 Answers

0
votes

I think you may be using ObjectDataSource incorrectly here.

My understanding of ObjectDataSource is that you give it a type for records, and a "select method" that will return a collection of your record data type.

Alternately, use Entity Framework with the ObjectDataSource.

0
votes

I'm not sure that this is the cause of your problem but back in the day when I used ObjectDataSource control (before MVC came to stage) I used to have to decorate the data source class with DataObject attribute and the data fetching method with DataObjectMethod attribute, otherwise ObjectDataSource control would not bind to my business logic methods. So in your case that would mean:

 <DataObject>
 Public Class ROIImportHistory

     <DataObjectMethod(DataObjectMethodType.Select, True)>
     Public Shared Function GetHistoryRows() As DataTable

         Dim localDatatable As New DataTable
         localDatatable.Columns.Add()
         localDatatable.Columns.Add()
         localDatatable.Columns.Add()
         localDatatable.Rows.Add(New Object() {"Hi", "Hi2", "Hi3"})
         Return localDatatable

     End Function

End Class

The attributes tell the ObjectDataSource control that the class and its method can be used as the data source. Again I'm not sure that this is the cause of your problem but it's worth a try. You will need to add reference to System.ComponentModel in order for these attributes to register.