Search code examples
c#datatable.select

How can I strip a single quote from a dataTable.Select( ) query in C#?


So I have a list of dealers names and I am searching them in my datatable -- problem is, some chucklehead HAS to be named 'Young's' --- this causes an error.

drs = dtDealers.Select("DealerName = '" + dealerName + "'");

So I tried to replace the string (although it didnt work for me - maybe I dont know how to use replace...)

 DataRow[] drs;
                if (dealerName.Contains("'"))
                {
                    string dealerSearch = dealerName;
                    dealerSearch = dealerSearch.Replace("'", "\'");
                    drs = dtDealers.Select("DealerName = '" + dealerSearch + "'");
                }
                else
                {
                    drs = dtDealers.Select("DealerName = '" + dealerName + "'");
                }

Anyone have any good ideas?

Thanks! Todd


Solution

  • You can use the @ operator called the verbatim operator which means literal in latin. It will not do any interpretation of the characters within the string that would otherwise mean something.