apostrophe error in .net app

i got this:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Syntax error: Missing operand after 'amico' operator.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SyntaxErrorException: Syntax error: Missing operand after 'amico' operator.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[SyntaxErrorException: Syntax error: Missing operand after 'amico' operator.]
   System.Data.ExpressionParser.Parse() +241
   System.Data.DataExpression..ctor(DataTable table, String expression, Type type) +121
   System.Data.DataView.set_RowFilter(String value) +153
   System.Web.UI.WebControls.FilteredDataSetHelper.CreateFilteredDataView(DataTable table, String sortExpression, String filterExpression, IDictionary filterParameters) +386
   System.Web.UI.WebControls.ObjectDataSourceView.CreateFilteredDataView(DataTable dataTable, String sortExpression, String filterExpression) +114
   System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1748
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +19
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
   System.Web.UI.WebControls.GridView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22
   System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +17
   System.Web.UI.Control.PreRenderRecursiveInternal() +80
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

 


and after a google i see ( http://aspnetresources.com/blog/apostrophe_in_rowfilter ) but im not sure where to edit this and it seams like its a bunch of VB suggestions and I got C#

I have attached my code behind below:




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Contacts_Companies : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //PAB Set CurrentUser session from login
        {
            if (!Page.IsPostBack)
                Session["CurrentUser"] = Page.User.Identity.Name;
        }
        //PAB Lookup Query String Type to determine Object Data Source

        if (Request.Params["Type"] == "MyCompanies")
        {
            Companies.DataSourceID = "MyCompanies";
        }
        else
        {
            Companies.DataSourceID = "AllCompanies";

        }
        if (SearchField.Text != "")
        {
            AllCompanies.FilterExpression = "Company_Name LIKE '%" + SearchField.Text + "%'";
            MyCompanies.FilterExpression = "Company_Name LIKE '%" + SearchField.Text + "%'";
        }
 
    }
  
    protected void Search_Click(object sender, EventArgs e)
    {

        if (SearchField.Text != "")
        {

            AllCompanies.FilterExpression = "Company_Name LIKE '%" + SearchField.Text + "%'";
            MyCompanies.FilterExpression = "Company_Name LIKE '%" + SearchField.Text + "%'";
        }

    }
    protected void AllCompanies_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        AllCompanies.FilterExpression = "Company_Name LIKE '%" + SearchField.Text + "%'";
    }
    protected void MyCompanies_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        MyCompanies.FilterExpression = "Company_Name LIKE '%" + SearchField.Text + "%'";
    }
    protected void SearchField_TextChanged(object sender, EventArgs e)
    {

    }
}

Open in new window

LVL 1
HudsonMarineAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberkiwiCommented:
When adding to SearchField.Text, change the single quotes to two single-quotes

e.g.
            AllCompanies.FilterExpression = "Company_Name LIKE '%" + SearchField.Text.Replace("'","''") + "%'";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chad SmithSoftware EngineerCommented:
SearchField.Text.Replace("\'", "\"");
0
HudsonMarineAuthor Commented:
Worked well thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.