Why double escape for apostrophe?

  • Thread starter Thread starter Earl
  • Start date Start date
E

Earl

Anyone know why the RowFilter has to be double-escaped? Anticipating names
with apostrophes, a single escape does not provide the proper name to filter
on.

For example, this would cause an exception:
Dim strname As String
strname = txtContact.Text
strname.Replace("'", "''")
dv.RowFilter = "ContactFullName='" + strname + "'"

.... but this would provide the proper filter:
dv.RowFilter = "ContactFullName='"" + strname + ""'"

Note the comma before the double quotes on the inside before the apostrophe
and the double quotes after the apostrophe on the outside.
 
Use :
strName=strName.Replace("'","''")

Keep in mind that strings are immutable i.e. are never changed that is the
Replace *function* returns a new string. It doesn't change the existing
string.
 
Thanks Patrice.

Patrice said:
Use :
strName=strName.Replace("'","''")

Keep in mind that strings are immutable i.e. are never changed that is the
Replace *function* returns a new string. It doesn't change the existing
string.
 
Back
Top