locked
Search for Single Quote in DataTable RRS feed

  • Question

  • Hi folks,

    In my WinForms app, I have a textbox.

    When someone begins to type into the textbox, my program begins to search a dataTable for the text entered. Here is my code:

    txtSearchString.Text = "'Text" // inc single Quote 
    
    DataRow[] foundRow;
    string searchString = txtSearchString.text;
    searchString = searchString.Replace("'", @"\\'") // does NOT work
    foundRow = _dsStock.Tables[0].Select("ID = '" + searchString + "'");  // Error The expression contains an invalid string '\\''

    Any ideas ?

    John


    jppnn

    Wednesday, September 23, 2020 11:42 AM

Answers

  • Figured it out - it should be:

    searchString = searchString.Replace("'", @"\''")

    NOT

    searchString.Replace("'", @"\\'")

    J


    jppnn

    • Marked as answer by john pp nn Wednesday, September 23, 2020 11:51 AM
    Wednesday, September 23, 2020 11:51 AM