【问题标题】:Exception: 'System.OutOfMemoryException' was thrown when there is 20,000+ data need to be bind in RadComboBox inside RadGrid异常:当需要在 RadGrid 内的 RadComboBox 中绑定 20,000 多个数据时,抛出“System.OutOfMemoryException”
【发布时间】:2015-08-07 03:35:15
【问题描述】:

我有一个 RadGrid 和一个 RadComboBox 在 RadGrid 之外(比如 comboOutside),在 Web 窗体内。
在 RadGrid 内部,还有 1 个 RadComboBox(比如 comboRadGrid)。在从 comboOutside 中选择项目时,comboRadGrid 是绑定的然后用户从 comboRadGrid 中选择特定公司,并将记录添加到 RadGrid。

对于所有项目,功能都运行良好,但我在绑定特定 comboOutside 项目时遇到问题。 即,当我从 comboOutside 中选择特定项目时,假设我在 comboOutside 中有 100 个项目,当我从中选择第 35 个项目时,comboRadGrid 总是在绑定第 35 个项目的记录时抛出此错误(因为第 35 个项目有 2000 多个要绑定的记录在comboRadGrid中)
错误如下:

除了 RadComboBox 的 1 个特定项目外,所有项目的功能都可以正常工作。我不明白为什么。因此,我无法在 RadGrid
中添加记录 下面是我的代码- C# 代码

public DataTable GetAccCode(string CompanyCode)
    {
        SqlConnection con = new SqlConnection(strcon);
        SqlCommand cmd = new SqlCommand("[Invoice].[usp_tbl_AccountCode_DL_Test]", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@CompanyCode", CompanyCode);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        try
        {
            con.Open();
            da.Fill(dt);
            con.Close();
        }
        catch (Exception ex)
        {
        }
        return dt;
    }
protected void RGGSTAcCode_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            //bind dropdwon while "Add" 
            string CompanyCode = ddlCompany.SelectedValue.ToString();
            GridEditableItem item = (GridEditableItem)e.Item;

            //code to bind inside RadComboBox list   
            RadComboBox rcb = (RadComboBox)item.FindControl("ddlAccountCode");
            rcb.DataSource = GetAccCode(CompanyCode);
            rcb.DataTextField = "AccountDescription";
            rcb.DataValueField = "AccountCodeID";
            rcb.DataBind();
            rcb.Items.Insert(0, new RadComboBoxItem("- Select -", string.Empty));

            Session["AccCode"] = rcb.SelectedValue.ToString();
            string a = rcb.SelectedValue.ToString();

            //Select particular dropdown value while "Edit"
            Label lblAcCode2 = item.FindControl("lblAcCode2") as Label;
            if (!string.IsNullOrEmpty(lblAcCode2.Text))
            {
                rcb.SelectedValue = lblAcCode2.Text;
                rcb.SelectedItem.Text = lblAcCode2.Text;
            }
        }
    }
     //code to bind outside RadComboBox list
        protected void BindComapnyDL()
        {
            SqlConnection con = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand("General.usp_tbl_BuyerCode_Query", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            try
            {
                con.Open();
                da.Fill(dt);
                con.Close();
            }
            catch (Exception ex)
            {
            }

            ddlCompany.DataTextField = "Title";
            ddlCompany.DataValueField = "Code";
            ddlCompany.DataSource = dt;
            ddlCompany.DataBind();

            Session["Comp"] = ddlCompany.SelectedValue.ToString();
            string a = ddlCompany.SelectedValue.ToString();      
        }

        //RadComboBox select index changed event
        protected void ddlCompany_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (ddlCompany.SelectedValue == null || ddlCompany.SelectedValue == "")
            {
                GridCommandItem cmditem = (GridCommandItem)RGGSTAcCode.MasterTableView.GetItems(GridItemType.CommandItem)[0];
                System.Web.UI.WebControls.Button ctrl = (System.Web.UI.WebControls.Button)cmditem.FindControl("AddNewRecordButton");
                ctrl.Enabled = false;

                System.Web.UI.WebControls.LinkButton btn = (System.Web.UI.WebControls.LinkButton)cmditem.FindControl("InitInsertButton");
                btn.Enabled = false;

                string content = "Please select company first";
                ScriptManager.RegisterStartupScript(this, typeof(string), "Successful", "alert('" + content + "');", true);
            }
            else
            {
                RGGSTAcCode.Rebind();
            }
        }

HTML 代码

<telerik:RadComboBox ID="ddlCompany" runat="server" Height="200" Width="240"
          DropDownWidth="310" EmptyMessage="- Select Product -" HighlightTemplatedItems="true" CausesValidation="false"
          Filter="Contains" AppendDataBoundItems="true" AllowCustomText="true" AutoPostBack="true"
          DataTextField="Title" DataValueField="Code" OnSelectedIndexChanged="ddlCompany_SelectedIndexChanged">
        </telerik:RadComboBox>

<telerik:RadGrid ID="RGGSTAcCode" runat="server"
                   ShowFooter="True" GroupingEnabled="False" ShowStatusBar="true" EmptyDataText="No record available." 
                   AllowAutomaticInserts="False" AllowAutomaticUpdates="False" AllowAutomaticDeletes="true" 
                   OnNeedDataSource="RGGSTAcCode_NeedDataSource" OnItemDataBound="RGGSTAcCode_ItemDataBound" 
                   OnInsertCommand="RGGSTAcCode_InsertCommand" OnDeleteCommand="RGGSTAcCode_DeleteCommand" 
                   OnUpdateCommand="RGGSTAcCode_UpdateCommand" OnItemCommand="RGGSTAcCode_ItemCommand">
                  <mastertableview ShowHeadersWhenNoRecords="true" autogeneratecolumns="false" datakeynames="AccountCodeID" InsertItemDisplay="Top"
                    insertitempageindexaction="ShowItemOnCurrentPage" ShowFooter="True" CommandItemDisplay="Top" ClientIDMode="Static">                                   
                         <Columns> 
                             <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"></telerik:GridEditCommandColumn> 

                             <telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code">
                                <ItemTemplate>
                                  <asp:Label ID="lblAcCode" runat="server" Text='<%# Eval("AccountCode")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                   <asp:Label ID="lblAcCode2" runat="server" Text='<%# Eval("AccountCode") + " - " + Eval("AccountDescription")%>' Visible="false"></asp:Label>

                                   <telerik:RadComboBox ID="ddlAccountCode" runat="server" Height="200" Width="240"
                                       DropDownWidth="310" HighlightTemplatedItems="true" CausesValidation="true"
                                       Filter="Contains" AppendDataBoundItems="true" DataTextField="AccountDescription" DataValueField="AccountCodeID">
                                   </telerik:RadComboBox>
                                </EditItemTemplate>
                             </telerik:GridTemplateColumn>

                             <telerik:GridBoundColumn DataField="AccountDescription" HeaderText="Description" UniqueName="AccountDescription" SortExpression="AccountDescription" InsertVisiblityMode="AlwaysHidden" ReadOnly="true" ></telerik:GridBoundColumn>
                             <telerik:GridBoundColumn aggregate="SUM" DataField="Amount" HeaderText="Amount" FooterAggregateFormatString="Total : {0:###,##0.00}" DataFormatString="{0:n}" FooterStyle-BackColor="#ffc04c" UniqueName="Amount" SortExpression="Amount"></telerik:GridBoundColumn>
                             <telerik:GridBoundColumn DataField="Remark" HeaderText="IFCA Remark" UniqueName="Remark" SortExpression="Remark">

                             </telerik:GridBoundColumn>    

                             <telerik:GridButtonColumn ConfirmTextFormatString="Are you sure you want to Delete {0} Account Code?" ConfirmTextFields="AccountCodeID"
                             ConfirmDialogType="RadWindow" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"></telerik:GridButtonColumn>                                                                            
                      </Columns>
                      <EditFormSettings>
                         <EditColumn ButtonType="ImageButton" />
                      </EditFormSettings>
                      <CommandItemSettings AddNewRecordText="Add new record" RefreshText="Refresh"></CommandItemSettings>
                  </mastertableview>
                </telerik:RadGrid> 

请告诉我为什么 RadComboBox 选择的特定项目会出现此错误。

【问题讨论】:

  • 请有人帮我解决这个问题。我是 asp.net 的新手,我没有得到如何在我的 Web 应用程序中解决这个问题,因为我正面临特定数据的这个问题。任何帮助将不胜感激。请回复
  • 对于我的 2 美分,我会说使用 Ajax 和延迟加载技术来填充您的网格和组合框。 2000 多个项目一次加载很多,而且可能会对您的数据库服务器造成负担。请参阅此处的示例:demos.telerik.com/aspnet-ajax/grid/examples/performance/…
  • 非常感谢您的回复。我必须根据 RadComboBox (comboOutside) 的 RadGrid 之外的项目填充/绑定 RadGrid 内部的 RadComboBox (comboRadGrid)。我浏览了您建议的链接,但无法正确理解。我是 Telerik 和 asp.net 的新手请让我知道如何修改我的 comboRadGrid 绑定代码(基于外部 Combo List 项),以便不再出现此问题?请回复
  • 对于组合框,请查看此链接:demos.telerik.com/aspnet-ajax/combobox/examples/…
  • 感谢您的回复。请再次阅读我发布的问题。我必须根据 RadGrid 之外的 RadComboBox 的 Item 值绑定 RadComboBox(在 RadGrid 内)。只有那些在 comboRadGrid 中绑定大数据的记录才会出现此错误。如果可能,请根据我发布的代码创建一个示例代码,以便我更好地理解。请回复。

标签: c# asp.net radgrid radcombobox


【解决方案1】:

通过以下链接使用 LoadOnDemand 机制:
demos.telerik.com/aspnet-ajax/combobox/examples/...

这种方法用于将大量记录绑定到 RadComboBox 中,这将避免发生此类异常。它工作正常。
感谢@Icemanind 的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多