Question

I have this code:

Categories = new[] 
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 
  "Aug", "Sep", "Oct", "Nov", "Dec" };

this works nicely with DotNet.Highcharts in MVC3. However, I was trying to dynamically populate the Categories with the result from querying the Northwind database like such

IEnumerable <string> fname = from u in db.Order_Details.Take(12)
                             join w in db.Products
                             on u.ProductID equals w.ProductID
                             select w.ProductName;

string[] namearr = (string[])fname.ToArray();

.....

.SetXAxis(new XAxis
{
    Categories = namearr

....

Now some of the namarr have apostrophes in them and highcharts cannot handle those.

How do I solve this issue?

Was it helpful?

Solution

Woah!!! I found the solution after digging through the internet. And I am answering my own question so that someone might find it useful and save loads if time. Apparently there are two approaches

string[] namearr = (string[])fname.ToArray();
int j = 0;
foreach (string name in namearr)
{
    // option 1
    // namearr[j++]= HttpUtility.HtmlEncode(name);

    // option 2
    namearr[j++] = name.Replace("'", "\\\'");
}

The Commented solution leaves the html encoded, but the second one works fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top