0

I get an error when the query contains a prime. Example: Cote d'Ivoire. Please how can I resolve this?

enter image description here

internal static string GetGridDatumID(string v)
{
    return GridTable.Select(string.Format("Name = '{0}'", v))[0][3].ToString();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

For filtering a single quote character, You have to escape it in your search criteria:

return GridTable.Select(string.Format("Name like '%{0}%'", "''"))[0][3].ToString();

Full example:

var dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));

var aRow = dt.NewRow();
aRow["Id"] = 1;
aRow["Name"] = "Name1'111";
dt.Rows.Add(aRow);

aRow = dt.NewRow();
aRow["Id"] = 2;
aRow["Name"] = "Nam'e2'";
dt.Rows.Add(aRow);

aRow = dt.NewRow();
aRow["Id"] = 3;
aRow["Name"] = "Name3";
dt.Rows.Add(aRow);

var foundRows = dt.Select("Name like '%''%'");
TheMah
  • 378
  • 5
  • 19