Greetings, I have a problem with my name search function, whenever the user key in their name in the search area, if their name contains special character as apostrophe for instance " Jacob ' " it will lead to Error message :
Exception Details: System.Data.SyntaxErrorException: The expression contains an invalid string constant: '.
Below I have attached my code in HTML and CS, may I know the solution to allow the special character to be searched along with names.
HTML
<asp:TextBox ID="txtKeyWord" runat="server" Width="246px" Height="22px" ValidationGroup="" /> <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
CS (C#)
protected void btnSearch_Click (object sender, EventArgs e)
{
SearchString = txtKeyWord.Text;
GridView1.DataBind();
}
public string HighlightText (string InputTxt)
{
string Search_Str = txtKeyWord.Text.ToString();
// Setup the regular expression and add the Or operator.
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
// Highlight keywords by calling the
//delegate each time a keyword is found.
return RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));
// Set the RegExp to null.
RegExp = null;
}