异常:当在 RadGrid 中的 RadComboBox 中需要绑定 20,000+ 数据时抛出 'System.OutOfMemoryException'
Exception: 'System.OutOfMemoryException' was thrown when there is 20,000+ data need to be bind in RadComboBox inside RadGrid
我有一个 RadGrid 和一个 RadComboBox outside RadGrid(比如 comboOutside),在 Web 表单中。
在 RadGrid 内部,还有 1 个 RadComboBox(比如 comboRadGrid)。在来自 comboOutside 的项目 selection 上,comboRadGrid 被绑定 即, 如果项目 'Company' 是来自 comboOutside 的 selected,那么所有公司名称都将在 comboRadGrid 中绑定;然后 comboRadGrid 中的用户 select 特定公司和记录被添加到 RadGrid 中。
对于所有项目,功能都运行良好,但我在绑定特定 comboOutside 项目时遇到问题。
即,当我从 comboOutside 中选择一个特定项目时,假设我在 comboOutside 中有 100 个项目,并且当我从其中 select 第 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
selection 的特定项目会出现此错误。
使用 LoadOnDemand 机制,遵循此 link:
demos.telerik.com/aspnet-ajax/combobox/examples/...
这种方法用于在RadComboBox 中绑定大量记录,可以避免此类异常的发生。它工作正常。
感谢@Icemanind 的帮助。
我有一个 RadGrid 和一个 RadComboBox outside RadGrid(比如 comboOutside),在 Web 表单中。
在 RadGrid 内部,还有 1 个 RadComboBox(比如 comboRadGrid)。在来自 comboOutside 的项目 selection 上,comboRadGrid 被绑定 即, 如果项目 'Company' 是来自 comboOutside 的 selected,那么所有公司名称都将在 comboRadGrid 中绑定;然后 comboRadGrid 中的用户 select 特定公司和记录被添加到 RadGrid 中。
对于所有项目,功能都运行良好,但我在绑定特定 comboOutside 项目时遇到问题。
即,当我从 comboOutside 中选择一个特定项目时,假设我在 comboOutside 中有 100 个项目,并且当我从其中 select 第 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
selection 的特定项目会出现此错误。
使用 LoadOnDemand 机制,遵循此 link:
demos.telerik.com/aspnet-ajax/combobox/examples/...
这种方法用于在RadComboBox 中绑定大量记录,可以避免此类异常的发生。它工作正常。
感谢@Icemanind 的帮助。