簡體   English   中英

C#中的Stackoverflow異常

[英]Stackoverflow Exception in C#

我在ASP.net C#代碼中收到stackoverflow異常。 該應用程序進入中斷模式。 我正在獲取的記錄超過1000,如果我超過了1,35,它就會中斷。 如果范圍最大為30,則可以正常工作。代碼只是生成html,我們將生成的html用作報告。 有人可以幫忙嗎?

下面是我的代碼;

    using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ARF.UI.Pages
{
    public partial class fromtoinvoiceofficecopy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string from = Request.QueryString["FromInvoiceNo"];
            string to = Request.QueryString["ToInvoiceNo"];

            Double FromInvoiceNum = Convert.ToDouble(from);
            Double ToInvoiceNum = Convert.ToDouble(to);

            DataSet ds = DAL.ReportData.Rpt_NonSaleTaxInvoiceByInvoiceNum(FromInvoiceNum, ToInvoiceNum);
            DataTable NonSaleTaxInvoiceHeader = ds.Tables[0];
            DataRow row = NonSaleTaxInvoiceHeader.Rows[0];
            string note = row["Notes"].ToString();
            DataTable NonSaleTaxInvoiceDetail = ds.Tables[1];

            //myDiv.InnerHtml = "From: "+from+"<br>"+"To: "+to;
            myDiv.InnerHtml = CreateHTMLTableFromDatatable(NonSaleTaxInvoiceHeader, NonSaleTaxInvoiceDetail);
        }
        public static string CreateHTMLTableFromDatatable(DataTable dtItems, DataTable dtDetails)
        {
            string html = "";
            StringBuilder htmlStr = new StringBuilder("");
            double TotalGrossAmt = 0;
            double TotalDiscount = 0;
            double TotalNetAmt = 0;
            double Freight = 0;
            //add rows
            for (int i = 0; i < dtItems.Rows.Count; i++)
            {
                htmlStr.Append("<div align='center' class='hdrcontent'><div class='logotxt'><img src='../Images/d_logo.png' style='width:60px'><b>XYZ Laboratories</b></div><div class='addr'>XYZ Road, XYZ CITY, XYZ<br>Tel:   Fax # </div></div><br><br>");
                htmlStr.Append("<b>");
                htmlStr.Append("<table class='hdrtbl'>");
                htmlStr.Append("<tr>");
                htmlStr.Append("<td>" + "Customer Code:" + "</td>");
                htmlStr.Append("<td>" + dtItems.Rows[i]["Code"].ToString() + "</td>");
                htmlStr.Append("<td></td>");
                htmlStr.Append("<td>" + "Invoice#:" + "</td>");
                htmlStr.Append("<td>"+dtItems.Rows[i]["InvoiceNumber"].ToString()+"</td>");
                htmlStr.Append("</tr>");
                htmlStr.Append("<tr>");
                htmlStr.Append("<td>Customer Name:</td>");
                htmlStr.Append("<td>"+dtItems.Rows[i]["InvoiceTo"]+"</td>");
                htmlStr.Append("<td></td>");
                htmlStr.Append("<td>Date:</td>");
                htmlStr.Append("<td>" + string.Format("{0:yyyyMMdd}", dtItems.Rows[i]["Date"])+ "</td>");
                htmlStr.Append("</tr>");
                htmlStr.Append("<tr>");
                htmlStr.Append("<td>Address:</td>");
                htmlStr.Append("<td>" + dtItems.Rows[i]["Address"] + "</td>");
                htmlStr.Append("<td></td>");
                htmlStr.Append("<td>DC Number:</td>");
                htmlStr.Append("<td>" + dtItems.Rows[i]["DeliveryChallanNo"] + "</td>");
                htmlStr.Append("</tr>");
                htmlStr.Append("<tr>");
                htmlStr.Append("<td>Mobile No:</td>");
                htmlStr.Append("<td>" + dtItems.Rows[i]["Mobile"] + "</td>");
                htmlStr.Append("<td></td>");
                htmlStr.Append("<td>BILTY Number:</td>");
                htmlStr.Append("<td>" + dtItems.Rows[i]["TRRRNo"] + "</td>");
                htmlStr.Append("</tr>");
                htmlStr.Append("<tr>");
                htmlStr.Append("<td>Forwarded Through:</td>");
                htmlStr.Append("<td>" + dtItems.Rows[i]["CarrierMS"] + "</td>");
                htmlStr.Append("<td></td>");
                htmlStr.Append("<td>Total Cartons:</td>");
                htmlStr.Append("<td>" + dtItems.Rows[i]["Cartons"] + "</td>");
                htmlStr.Append("</tr>");
                htmlStr.Append("</table>");
                htmlStr.Append("</b><br><br>");

                DataRow[] childs = lookupInvoiceDetails(dtDetails, dtItems.Rows[i][0].ToString());
                int length = childs.Length;
                htmlStr.Append("<tr>");
                htmlStr.Append("<table class='tbl'>");
                htmlStr.Append("<tr class='hdrsub'>");
                htmlStr.Append("<td>Product Name</td><td>Pack Size</td><td>Qty</td><td>Bonus</td>");
                htmlStr.Append("<td>Batch<br>Number</td><td>Rate (Rs)</td><td>GROSS AMT <br>(Rs) </td><td>Discount <br>%age </td><td>Discount<br>Amount<br>(Rs) </ td><td>Net Amount<br>(Rs) </td>");
                htmlStr.Append("</tr>");
                foreach(DataRow dr in childs)
                {
                    htmlStr.Append("<tr>");
                    htmlStr.Append("<td>"+dr["ItemAndUnitPacking"]+"</td>");
                    htmlStr.Append("<td>"+dr["PackSizeDisplay"] +"</td>");
                    htmlStr.Append("<td>"+dr["QTY"]+"</td>");
                    htmlStr.Append("<td>"+dr["Bonus"]+"</td>");
                    htmlStr.Append("<td>"+dr["BatchNumber"]+"</td>");
                    htmlStr.Append("<td>"+dr["UnitPrice"]+"</td>");
                    htmlStr.Append("<td>"+dr["GrossAmtRs"]+"</td>");
                    htmlStr.Append("<td>"+dr["DiscountPercentage"] +"</td>");
                    htmlStr.Append("<td>"+dr["TradeDiscount"] +"</td>");
                    htmlStr.Append("<td>"+ dr["NetAmountRs"] + "</td>");

                    TotalGrossAmt += Convert.ToDouble(dr["GrossAmtRs"].ToString());
                    TotalDiscount += Convert.ToDouble(dr["TradeDiscount"].ToString());
                    TotalNetAmt += Convert.ToDouble(dr["NetAmountRs"].ToString());
                    Freight = Convert.ToDouble(dr["Freight"].ToString());

                    htmlStr.Append("</tr>");
                }
                htmlStr.Append("<tr>");
                htmlStr.Append("<td colspan='5'>Invoice Remarks (If Any) :</td>");
                htmlStr.Append("<td><b>Total</b></td>");
                htmlStr.Append("<td><b>"+TotalGrossAmt.ToString("#,##0.00") + "</b></td>");
                htmlStr.Append("<td></td>");
                htmlStr.Append("<td><b>"+TotalDiscount.ToString("#,##0.00") + "</b></td>");
                htmlStr.Append("<td><b>"+TotalNetAmt.ToString("#,##0.00") + "</b></td>");
                htmlStr.Append("</tr>");
                htmlStr.Append("<tr>");
                htmlStr.Append("<td colspan='5'></td>");
                htmlStr.Append("<td colspan='4'><b>LESS BILTY Charges</b></td>");
                htmlStr.Append("<td><b>"+Freight.ToString("#,##0.00") + "</b></td>");
                htmlStr.Append("</tr>");
                htmlStr.Append("<tr>");
                htmlStr.Append("<td colspan='5'></td>");
                htmlStr.Append("<td colspan='4'><b>NET PAYABLE</b></td>");
                htmlStr.Append("<td><b>"+(TotalNetAmt-Freight).ToString("#,##0.00") + "</b></td>");
                htmlStr.Append("</tr>");
                htmlStr.Append("<tr>");
                htmlStr.Append("<td colspan='10'><b>WARRANTY:-</b> I,under Section:23(1)(i) of the Drug Act, 1976,hereby give this warranty that the Drugs described in");
                htmlStr.Append("this invoice and sold by us do not cotravene in any way with the provisions of section 23 of the");
                htmlStr.Append("Drugs Act,1976.</td>");
                htmlStr.Append("</tr>");
                htmlStr.Append("<tr><td colspan='10'>&nbsp;</td></tr>");
                htmlStr.Append("<tr>");
                htmlStr.Append("<td colspan='10'><b>TERMS & CONDITIONS:-</b><br>");
                htmlStr.Append("1.Damage / Breakage / Leakage of stock will only be entertained, if informed in writing within 15 days from date of invoice.<br>");
                htmlStr.Append("2.Claims of Near Expiry Stock will be entertained, if informed in wrting before three(3) months prior to expired.<br>");
                htmlStr.Append("3.Your complaints will only be entertained, if it will be submitted in writing to Head office at ");
                htmlStr.Append("</td>");
                htmlStr.Append("</tr>");
                htmlStr.Append("</table>");
                htmlStr.Append("<br>");
                htmlStr.Append("</tr>");
                htmlStr.Append("<table class='footertbl'>");
                htmlStr.Append("<tr>&nbsp;</tr>");
                htmlStr.Append("<tr>");
                htmlStr.Append("<td style='width:150px;'><b>CHECKED BY:</b></td><td> --------------------------- </td><td></td><td></td><td></td>");
                htmlStr.Append("<td><b>ISSUED BY:</b></td><td colspan='2'>-------------------------</td>");
                htmlStr.Append("</tr>");
                htmlStr.Append("<tr>");
                htmlStr.Append("<td><br><br><b>Printed date "+ DateTime.Now + "</b></td>");
                htmlStr.Append("<tr>");
                htmlStr.Append("</table>");
            }
            htmlStr.Append("</table>");
            return htmlStr.ToString();
        }
        static public DataRow[] lookupInvoiceDetails(DataTable dtDetails, string qry)
        {
            DataRow[] foundInvoiceDetails = dtDetails.Select("NonSaleTaxInvoiceId = '" + qry + "'");

            if (foundInvoiceDetails.Length != 0)
            {
                //return foundInvoiceDetails;

            }
            return foundInvoiceDetails;
        }
    }
}

我通過指定StringBuilder的容量和最大容量解決了問題,下面是我更改的行;

StringBuilder htmlStr = new StringBuilder(276438, Int32.MaxValue);

同樣,這是一個臨時解決方案。 我願意接受更好的建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM