我在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'> </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> </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;
}
}
}
答案 0 :(得分:0)
我通过指定StringBuilder的容量和最大容量解决了该问题,下面是我更改的行;
StringBuilder htmlStr = new StringBuilder(276438, Int32.MaxValue);
同样,这是一个临时解决方案。我愿意接受更好的建议。