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?
 
cyberkiwiConnect With a Mentor Commented:
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
 
Chad SmithSoftware EngineerCommented:
SearchField.Text.Replace("\'", "\"");
0
 
HudsonMarineAuthor Commented:
Worked well thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.