I think you have two problems:
- You probably shouldn't be using
<asp:Label> for the thing you're doing. Try <asp:Literal> instead or provide us with more information on exactly why you're using a label and what it is you want to accomplish (visually, or in HTML terms).
- You should build your string with
System.Text.StringBuilder and not concatenate it with +=. This will improve performance and reduce memory usage, but since your exception seems to be occurring on label.Text = str;, just replacing the concatenation with StringBuilder won't solve the problem.
Say you have this ASP.NET markup:
<asp:Label id="MyLabel" runat="server" />
and this C# codebehind:
string str = String.Empty;
str += "Some text "
str += "some more text";
MyLabel.Text = str;
Try to replace both of these with:
<asp:Literal id="MyLiteral" runat="server" />
and
StringBuilder sb = new StringBuilder();
sb.Append("Some text ");
sb.Append("some more text");
MyLiteral.Text = sb.ToString();
Another option is to write directly to the output stream with Response.Write(). Without knowing exactly what it is you're trying to accomplish and why you have such a large string that you end up with an OutOfMemoryException, it's hard to help you any further than this, I'm afraid.
stringbuilderfor appending text....