如何解决在 asp.net 中使用 switch case 时出现 System.StackOverflowException
我正在 ASP.NET WebForms 中开发 Web 应用程序,因为我正在用 1000 条记录填充 gridView 并在 OnRowDataBound 事件中执行一些计算部分。在 OnRowDataBound 事件中有一个包含 100 个 switch case 的 switch case,整个 OnRowDataBound 事件函数大约有 13000 行代码。现在,当我在没有 OnRowDataBound 事件的情况下运行应用程序以从服务器获取数据时,它会获取包含 1000 多条记录的所有记录,但是当我尝试使用包含 100 条开关案例的 OnRowDataBound 事件获取这些记录时,它会进入 System.StackOverFlowException。>
作为一种解决方案,我试图将这些 switch case 减少到 20,然后它运行没有任何错误,但是当 OnRowDataBound 事件有 100 个 switch case 时会进入异常。
ASPX 页面。
<asp:GridView ID="gvReport" runat="server" CellPadding="4" Font-Names="calibri(body)"
Font-Size="9" ForeColor="#333333" GridLines="Vertical" OnRowDataBound="gvReport_RowDataBound"
ShowFooter="True" OnRowCreated="gvReport_RowCreated" OnPreRender="gvReport_PreRender"
EmptyDataText="No Data Retrieved">
<AlternatingRowStyle BackColor="White" />
<RowStyle HorizontalAlign="Left" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" CssClass="Freezing"
Wrap="True" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#505050"
BorderStyle="Solid" BorderWidth="1" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
ASPX.cs 页面
protected void gvReport_RowDataBound(object sender,GridViewRowEventArgs e)
{
switch(Condition){
case"":{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
try
{
strPrevious = DataBinder.Eval(e.Row.DataItem,"Territory").ToString();
}
catch (Exception ex)
{
}
dblSubTotal += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem,"Retailer code").ToString());
dblTotal += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem,"Retailer code").ToString());
dblSubTotal2 += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem,"Retailer mapped").ToString());
dblTotal2 += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem,"Retailer mapped").ToString());
}
}////////////// Such types of calculations are there in 100's of switch cases.
catch (Exception ex)
{
}
}
break; /////////// there are 100's of cases in switch statement,if reduced to 15-20 cases
then runs without any exception.
}
}
这是 OnRowDataBound 事件产生的异常。
解决方法
我会采用的最简单的方法是二分法,(拆分您的switch cases
,直到找到有罪的那个)。您至少有一个(可能更多)switch case
触发了相同的事件 (OnRowDataBound
)。
我知道有很多情况,但这可能是获得结果的最有效方法。一旦找到嫌疑人,就必须中断(断点)并逐步执行,直到找到触发事件的行,从而找到递归。
另外一个好方法是在 gvReport_RowDataBound
的入口处添加一个断点,然后一旦你中断,你就按几个 F5 继续并建立 Call Stack
的一部分,然后看看调用堆栈窗口(Windows>Call Stack),在调用堆栈的每一行双击将带您到代码中触发它的行,在那里您可以看到触发递归的情况。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。