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.