简体   繁体   中英

Stackoverflow Exception in C#

I am getting stackoverflow Exception in ASP.net C# code. the app goes into break mode. The records that i am fetching are above 1000, it breaks if i pass the range 1,35. it works fine if the range is up to 30. the code is simply generating html, we are using the generated html as report. can some one please help?

below is my code;

    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;
        }
    }
}

I resolved the problem by specifying the capacity and Max capacity of StringBuilder, below is the line that i changed;

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

Again this is a temporary solution. i am open to better suggestion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM