Assuming I want productCode
variable below to be assigned the product code of a product named Cookie\'s
NOTE the \"\'\" in the the Prod
Use LINQ, this will make your solution simplier and more general. Otherwise you will end up with replacing single quotes (and other special characters) in the filter with escape chars.
DataTable dt=PullSomeDataFromProductTableDatabase();
string filterValue="Cookie's";
string productCode=dt.AsEnumerable()
.Where(row => row.Field<string>("ProductName") == filterValue)
.FirstOrDefault(row => row.Field<string>("ProductCode"));
Double up any single quotes like this
...dt.Select("[ProductName] = '" + filterValue.Replace("'", "''") + "'")[0]["ProductCode"]
Try string filterValue="Cookie''s";
or
string filterValue="Cookie'+CHAR(39)+'s";