Datatable Select Method Problem

The C# code shown below performs an operation using two datatables.  dt1 is a master list of customers; dt2 is a list of customers to be excluded from the master list based on the key field “CustID”.   The existing code uses the datatable Select method to identify a matched record in dt2, then it deletes the corresponding record in dt1.  The routine works perfectly UNTIL a record contains a CustID that uses a single quote mark ( ‘ ) in the CustID field.  For example CustID=ACME001 works fine, but CustID=ACME’001 would fail.  I understand why the single quote causes the problem, but I do not know the most efficient means of solving the problem so that regardless of the special characters that might be embedded in the CustID field, the routine will still work as intended.  Any help is much appreciated.
dt1 = GetMasterList();  //contains master customer list
dt2 = GetExclusionList(); //contains list to exclude
for (int i = dt1.Rows.Count - 1; i >= 0; i--)
{
DataRow[] rows = dt2.Select("CustID='" + dt1.Rows[i]["CustID"].ToString() + "'");
if (rows.Length > 0)
dt1.Rows.Remove(dt1.Rows[i]);
}

Open in new window

dev775Asked:
Who is Participating?
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.

Only1RBCommented:
When you are loading the query you can replace the single tick in the field by using the "Replace" Method.
string sQuery = "Select * from table where Name = '" +
                           tbText.Text.Replace("'", "''") +
                           "'";
0
HainKurtSr. System AnalystCommented:
DataRow[] rows = dt2.Select("CustID='" + dt1.Rows[i]["CustID"].ToString() + "'");
when it is like "Hain'Kurt" select becomes

CustID='Hain'Kurt'

and it is not valid, so you should replace ' with ''

DataRow[] rows = dt2.Select("CustID='" + dt1.Rows[i]["CustID"].ToString().Replace("'","''") + "'");

CustID='Hain''Kurt'

which is valid now ('' means one ' in string...)
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
dev775Author Commented:
HainKurt - this was the exact answer I was looking for.  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
.NET Programming

From novice to tech pro — start learning today.