string with StingBuilder error c# - c#

i am trying to create a string with html code to use it to render a cart
for(var i=1; i<=#totalCountItems; i++){
sb.Append("<div class=\"ibox-content\">"+
"<div class=\"table-responsive\">"+
"<table class=\"table shoping-cart-table\">"+
"<tbody>"+
"<tr>"+
"<td width=\"90\">"+
"<div class=\"cart-product-imitation\">"+
"</div>"+
"</td>"+
"<td class=\"desc\">"+
"<h3>"+
"<a href=\"#\" class=\"text-navy\">"+
Model[i].Name+
"</a>"+
"</h3>"+
"<p class=\"small\">"+
Model[i].DescriptionLong+
"</p>"+
"<dl class=\"small m-b-none\">"+
"<dt>Description lists</dt>"+
"<dd>"+Model[i].DescriptionSort+"</dd>"+
"</dl>"+
"<div class=\"m-t-sm\">"+
"<i class=\"fa fa-gift\"></i> Add gift package"+
"|"+
"<i class=\"fa fa-trash\"></i> Remove item"+
"</div>"+
"</td>"+
"<td>"+
"$"+Model[i].Price+
"<s class=\"small text-muted\">$"+Model[i].DPrice+"</s>"+
"</td>"+
"<td width=\"65\">"+
"<input type=\"text\" class=\"form-control\" placeholder=\"1\">"+
"</td>"+
"<td>"+
"<h4>"+
"$"+(Model[i].Price)*(#Model[i].Count)+
"</h4>"+
"</td>"+
"</tr>"+
"</tbody>"+
"</table>"+
"</div>"+
"</div>");
}}
$("iboxTest").html(#sb);
}
the second time inside the loop i am getting an error 'System.OutOfMemoryException' in mscorlib.dll
i reduced the size of the string and it worked. any ideas?

You use StringBuilder, but you also create too many strings.
Doing this:
sb.Append("<div class=\"ibox-content\">"+
"<div class=\"table-responsive\">"+
"<table class=\"table shoping-cart-table\">"+
......
you append only one time, but using + (concatenation) you creates many new strings and get OutOfMemoryException.
Code above should be changed to something like this(append every line using StringBuilder.Append()):
sb.Append("<div class=\"ibox-content\">");
sb.Append("<div class=\"table-responsive\">");
sb.Append("<table class=\"table shoping-cart-table\">");
...

Related

Add double quotes to the datatable column

In one of my datatable column, I want the value to be shown in double quotes
AS:- "My value"
Below is my code:-
string StrPriBody = "Dear User, <br><br> The Number of days revised by you from " +
" " + table.Rows[0]["LAST_ACTION_DAYS"] + " days to " +
" " + table.Rows[0]["CURRENT_ACTION_DAYS"] + " days. <br /> " +
" with Remark <b> " + table.Rows[0]["REMARKS"] + "</b><br /><br />";
I want to show REMARK value in double quotes.
How to achieve that ?
Add extra quotes with backslash:
string StrPriBody = "Dear User, <br><br> The Number of days revised by you from " +
" " + table.Rows[0]["LAST_ACTION_DAYS"] + " days to " +
" " + table.Rows[0]["CURRENT_ACTION_DAYS"] + " days. <br /> " +
" with Remark <b> \"" + table.Rows[0]["REMARKS"] + "\"</b><br /><br />";
Use \ to print the escape sequence characters in a string
" with Remark <b> \"" + table.Rows[0]["REMARKS"] + "\" </b><br /><br />";
For better readability I would use verbatim string literal, as it allows to avoid concatenation and easily expand on multiple lines. Also, String.Format would make your string more readable:
string StrPriBody = String.Format(#"
Dear User,
<br><br>
The Number of days revised by you from {0} days to {1} days. <br />
with Remark <b> ""{2}""</b>
<br /><br />",
table.Rows[0]["LAST_ACTION_DAYS"],
table.Rows[0]["CURRENT_ACTION_DAYS"],
table.Rows[0]["REMARKS"]);
Also, C# 6.0 (Visual Studio 2015) has introduced interpolated strings, that makes string construction even more reader friendly:
string StrPriBody = $#"
Dear User,
<br><br>
The Number of days revised by you from {table.Rows[0]["LAST_ACTION_DAYS"]} days to {table.Rows[0]["CURRENT_ACTION_DAYS"]} days. <br />
with Remark <b> ""{table.Rows[0]["REMARKS"]}""</b>
<br /><br />";

Better way to give space other than

I am using the following code to give blank space so that the three elements "label, then dropdown and then a button for action on dropdown" are right aligned in a panel in a web page.
Now, I know I can do with padding/margin, however, it all works only with respect to the element at right side and not from the right hand side of the browser.
However, I was talented enough to achieve what I want using but I find it weird to write the code this way:
LiteralSpecial.Text = " " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"Select page ";
Is there are way to refine this please, folks?
Have you tried using CSS?
<div style="text-align:right">Select page</div>
See it in action: http://jsfiddle.net/vhxchyrj/2/
<div style="width:600px;padding-left:550px;">Select page</div>

Textbox not showing output in MVC [duplicate]

This question already has answers here:
How to display the text in MVC?
(4 answers)
Closed 8 years ago.
I want to show a output in textbox in MVC. But its not displaying anything. I used the following code and i attached screenshot below:
#Html.TextAreaFor(up => up.CompileOutput)
foreach (CompilerError CompErr in results.Errors)
{
userProgram.CompileOutput = "Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
return View(userProgram);
The first image shows that the output is binded with that particular textbox. But in browser (image 2) shows nothing in the textbox (red colour)
I am even wondering why you did not got an exception. return view(string) will look for a view with the string parameter as name, it will not show the text.
I would suggest you use ViewBag instead. So you set your error text in a property you name as follow:
foreach (CompilerError CompErr in results.Errors)
{
userProgram.CompileOutput = "Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
ViewBag.ErrorText = userProgram.CompileOutput;
You can later on retrieve the value by simply calling ViewBag.ErrorText from you Razor view
Why not try doing it another way?
#Html.TextArea("CompileOutput", userProgram.CompileOutput)

C# sending e-mail with html link to file - unrecognized escape sequence

I have a C# app that is sending an e-mail. I wish to send a link in the mail for users to click and open a file.
However where I have the link to my workbook I have issues. I have 5 errors all the same 'unrecognized escape sequence' where every "/" is. How do I get round this?
string htmlHeader = "<table style='font-size: 12pt;'>" +
"<tr><a href='file:///G:\Shared\Team\New\Corporate%20Actions\Corp%20Events.xlsx'>Corp Events Workbook></tr><tr/><tr/>" +
"<tr><th align='left'>Status</th><th> </th>" +
"<th align='left'>Sedol</th><th> </th>" +
"<th align='left'>Name</th><th> </th>" +
"<th align='left'>Date Effective</th><th> </th>" +
"<th align='left'>Event Code</th><th> </th>" +
"<th align='left'>Terms</th><th> </th></tr>";
C# Escape characters using the letter \ followed by another letter, example: a newline escape: \n. Since there is no \S escape character in C# (see the list here: http://msdn.microsoft.com/en-us/library/h21280bw.aspx) the compiler can't parse it. To solve it us \\, the escape sequnce followed by the backslash so the compiler will know you mean to print \.
Exmaple:
string htmlHeader = "<table style='font-size: 12pt;'>" +
"<tr><a href='file:///G:\\Shared\\Team\\New\\Corporate%20Actions\\Corp%20Events.xlsx'>Corp Events Workbook></tr><tr/><tr/>" +
"<tr><th align='left'>Status</th><th> </th>" +
"<th align='left'>Sedol</th><th> </th>" +
"<th align='left'>Name</th><th> </th>" +
"<th align='left'>Date Effective</th><th> </th>" +
"<th align='left'>Event Code</th><th> </th>" +
"<th align='left'>Terms</th><th> </th></tr>";
Notice the second line, on the path part, there is a double left-backslash instead of one.
Try adding a # in front of your string(part) containing the escape sequence
like:
string htmlHeader = "<table style='font-size: 12pt;'>" +
#"<tr><a href='file:///G:\Shared\Team\New\Corporate%20Actions\Corp%20Events.xlsx'>Corp Events Workbook></tr><tr/><tr/>" +
"<tr><th align='left'>Status</th><th> </th>" +
"<th align='left'>Sedol</th><th> </th>" +
"<th align='left'>Name</th><th> </th>" +
"<th align='left'>Date Effective</th><th> </th>" +
"<th align='left'>Event Code</th><th> </th>" +
"<th align='left'>Terms</th><th> </th></tr>";
Not the most clean solution but its a working solution.I would advise your to look at UnTraDe response aswell.

System.OutOfMemoryException at System.Text.StringBuilder.ToString() with Excel XML String

So recently I've been working on code in C#/ASP.NET that throws an error during selections of large parameters:
Exception of type 'System.OutOfMemoryException' was thrown. at
System.Text.StringBuilder.ToString()
General Overview:
Code queries a bunch of data from a database based on a selection by the user and puts in into an Excel document to be then be exported/downloaded by the user. It first uses StringBuilder to append the Prefix of the XML with:
const string startExcelXML = "<xml version>\r\n<Workbook " +
"xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n" +
" xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n " +
"xmlns:x=\"urn:schemas- microsoft-com:office:" +
"excel\"\r\n xmlns:ss=\"urn:schemas-microsoft-com:" +
"office:spreadsheet\">\r\n <Styles>\r\n " +
"<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n " +
"<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" +
"\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" +
"\r\n <Protection/>\r\n </Style>\r\n " +
"<Style ss:ID=\"BoldColumn\">\r\n <Font " +
"x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n </Style>\r\n " +
"<Style ss:ID=\"StringLiteral\">\r\n <NumberFormat" +
" ss:Format=\"#\"/>\r\n </Style>\r\n <Style " +
"ss:ID=\"Decimal\">\r\n <NumberFormat " +
"ss:Format=\"0.0000\"/>\r\n </Style>\r\n " +
"<Style ss:ID=\"Integer\">\r\n <NumberFormat " +
"ss:Format=\"0\"/>\r\n </Style>\r\n <Style " +
"ss:ID=\"DateLiteral\">\r\n <NumberFormat " +
"ss:Format=\"mm/dd/yyyy;#\"/>\r\n </Style>\r\n " +
"</Styles>\r\n ";
It then goes through a loop that goes through and appends </Data></Cell> and the like as required before finally appending with:
const string endExcelXML = "</Workbook>";
After that it then return contentSB.ToString(); Since this is the only ToString() in the method the exception references, it has to be this piece of code.
Similar StackOverflow Issue:
interesting OutOfMemoryException with StringBuilder
Thoughts:
I've tried using the following code to get a general idea of how big the string is, which works for smaller selections, but doesn't output anything for larger selections and where contentSB is the StringBuilder object:
System.Diagnostics.Debug.WriteLine("String:", contentSB);
System.Diagnostics.Debug.WriteLine("String length:", contentSB.Length);
The referenced other StackOverflow issue occurs when appending, whereas mine is when returning a ToString(), so the cause of the issue might be different as it occurs not in the middle of Appending in the loop, but in the conversion process/return. What is the root cause and how do I fix it?
Look like the string is bigger than what the memory can take. Guess that you could you be keeping long-lived references to large objects in memory?
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/5050e855-20d0-4fc5-97b6-79fdc7f176c6/
C# Stringbuilder OutOfMemoryException

Categories

Resources