【问题标题】:OutOfMemoryException Binding ObjectDataSource to GridViewOutOfMemoryException 将 ObjectDataSource 绑定到 GridView
【发布时间】:2013-05-09 05:46:09
【问题描述】:

我正在尝试使用GridView 来显示从 SQL Server 中提取的表。它是事件的日志。我已经在我的页面上放置了GridView 控件以及ObjectDataSource 控件。我已经配置了:

<%@ 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>

我在App_Code 文件夹中创建了一个类,如下所示:

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

但是,当我尝试运行此程序时,出现以下异常:

出了什么问题?我以为我在这里做了一件非常基本的事情。我尝试了很多不同的选项,我只知道如果我删除GridViewObjectDataSource,页面可以正常工作。如果我将DataTable 直接绑定到GridView,事情也可以正常工作。

我的目的是在过滤的同时获得真正的分页。鉴于我可以处理的不仅仅是每次加载页面时都不想加载DataTable

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 

我引用了thisthis 无济于事。

注意:我发现此代码运行良好,加载到同一应用程序池中同一服务器上的另一个站点。因此,我认为这与配置问题有关。

【问题讨论】:

    标签: asp.net gridview datasource objectdatasource


    【解决方案1】:

    我认为您可能在这里错误地使用了 ObjectDataSource。

    我对 ObjectDataSource 的理解是你给它一个记录类型,以及一个“选择方法”,它将返回你的记录数据类型的集合。

    或者,use Entity Framework with the ObjectDataSource

    【讨论】:

    • 请注意我对上述问题所做的修改。你会看到我正确地使用了它。此外,从msdn.microsoft.com/en-us/library/… 可以看出,DataTable 是受支持的数据源。
    【解决方案2】:

    我不确定这是不是您的问题的原因,但是在我使用 ObjectDataSource 控件的那一天(在 MVC 出现之前)我曾经不得不用 DataObject 属性和数据获取来装饰数据源类具有 DataObjectMethod 属性的方法,否则 ObjectDataSource 控件不会绑定到我的业务逻辑方法。所以在你的情况下,这意味着:

     <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
    

    属性告诉 ObjectDataSource 控件该类及其方法可以用作数据源。同样,我不确定这是您问题的原因,但值得一试。您需要添加对 System.ComponentModel 的引用才能注册这些属性。

    【讨论】:

      猜你喜欢
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多