troubleshooting Question

Getting "Object Reference Error" while adding String.Format in StringBuilder

Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates asked on
C#
4 Comments1 Solution402 ViewsLast Modified:
Hi Experts,

I am getting "object reference error" while putting string.format in stringbuilder. I have below code in C#2.0

public const string PageLinkGoogleMarkup = "<link rel=\"alternate\" hreflang=\"{0}\" href=\"{1}www.mysite.com{2}{3}\" />\r\n";

Now when I am adding this to HtmlTextWriter it is working fine, as below:

HtmlTextWriter writer (Object);
writer.write(string.format(PageLinkGoogleMarkup,str[1],header,links,querystr)); //This works perfect

And when I tries to add this above code to StringBuilder instead of HtmlTextWriter it gives error:

As there is big loop so I decided to go with appending everything to StringBuilder first and at last rendering using writer.Write (Whole html in a bunch), please below code

StringBuilder sb = new StringBuilder();
sb.Append(string.format(PageLinkGoogleMarkup,str[1],header,links,querystr)); //here i get object reference error

writer.Write(sb.ToString());

Please suggest!!

Thanks.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros