locked
Exception: The expression contains an invalid string constant RRS feed

  • Question

  • When i set DateView.RowFilter with special characters, (like ') I'm getting the "The expression contains an invalid string constant". Is this the issue with DataView.

     

    Exception:

    The expression contains an invalid string constant: 'Bon app''.

     

    Details:

    System.Data.SyntaxErrorException was unhandled by user code

      Message=The expression contains an invalid string constant: 'Bon app''.

      Source=System.Data

      StackTrace:

           at System.Data.ExpressionParser.ScanString(Char escape)

           at System.Data.ExpressionParser.Scan()

           at System.Data.ExpressionParser.Parse()

           at System.Data.DataExpression..ctor(DataTable table, String expression, Type type)

           at System.Data.DataView.set_RowFilter(String value)

     

    Wednesday, April 27, 2011 7:14 AM

Answers

  • Hi,

    String values are enclosed within single quotes ' '. If the string contains single quote ', the quote must be doubled.

    [C#]

    dataView.RowFilter = "Name = 'John'"    // string value
    dataView.RowFilter = "Name = 'John ''A'''" // string with single quotes "John 'A'"
    
    dataView.RowFilter = String.Format("Name = '{0}'", "John 'A'".Replace("'", "''"));

    Please check the below link. It may help you.

    http://www.csharp-examples.net/dataview-rowfilter/


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This will help other members to find the solution easily.
    • Marked as answer by Jie Bao Wednesday, May 4, 2011 10:58 AM
    Wednesday, April 27, 2011 7:33 AM

All replies

  • Hi,

    String values are enclosed within single quotes ' '. If the string contains single quote ', the quote must be doubled.

    [C#]

    dataView.RowFilter = "Name = 'John'"    // string value
    dataView.RowFilter = "Name = 'John ''A'''" // string with single quotes "John 'A'"
    
    dataView.RowFilter = String.Format("Name = '{0}'", "John 'A'".Replace("'", "''"));

    Please check the below link. It may help you.

    http://www.csharp-examples.net/dataview-rowfilter/


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This will help other members to find the solution easily.
    • Marked as answer by Jie Bao Wednesday, May 4, 2011 10:58 AM
    Wednesday, April 27, 2011 7:33 AM
  • Hi Vimal,

     

    Thanks for the update. I'm having more special character in my ItemsSource. Is there any generic solution that will work with all the special characters.

     

    Thanks,

    Sivakumar

    Wednesday, April 27, 2011 12:08 PM
  • Hi,

    Is it possible for you to post your DataView.RowFilter statement. It will be helpful if we know your filter condition with more special characters.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This will help other members to find the solution easily.
    Thursday, April 28, 2011 2:23 AM
  • Yes, please show it. And read the first paragraph from the

    http://www.csharp-examples.net/dataview-rowfilter/

     If a column name contains any of these special characters ~ ( ) # \ / = > < + - * % & | ^ ' " [ ], you must enclose the column name within square brackets [ ]. If a column name contains right bracket ] or backslash \, escape it with backslash (\] or \\).

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 29, 2011 7:59 AM