Connecting Tech Pros Worldwide Forums | Help | Site Map

Apostrophe in Column value of DataTable select query

Tom
Guest
 
Posts: n/a
#1: Oct 9 '06
Hi, I have some kind of problems with an apostrophe character ('). I would
like to select from DataTable DataRow containing value horses' (with an
apostrophe on the end). But when I do it in an obvious way, like this:



DataTable dt = new DataTable();

DataColumn id = new DataColumn("ID", Type.GetType("System.Int32"));

DataColumn str = new DataColumn("STR", Type.GetType("System.String"));

dt.Columns.Add(id);

dt.Columns.Add(str);



DataRow dr;


dr = dt.NewRow();

dr["STR"] = "horses'";

dt.Rows.Add(dr);



string ster = "STR = 'horses'";

DataRow[] wyn = dt.Select(ster);



I get an error shown below.


An unhandled exception of type 'System.Data.SyntaxErrorException' occurred
in system.data.dll

Additional information: The expression contains an invalid string constant:
'horses''.

I know its related with the apostrophe character, but cant "google" anything
to make this work properly. Could you help me please?


Best regars, Tom



Patrick Steele
Guest
 
Posts: n/a
#2: Oct 9 '06

re: Apostrophe in Column value of DataTable select query


In article <egdd46$pir$1@atlantis.news.tpi.pl>,
bear007.anty_spamerowanie@poczta.onet.pl says...
Quote:
Hi, I have some kind of problems with an apostrophe character ('). I would
like to select from DataTable DataRow containing value horses' (with an
apostrophe on the end). But when I do it in an obvious way, like this:
>
>
>
DataTable dt = new DataTable();
>
DataColumn id = new DataColumn("ID", Type.GetType("System.Int32"));
>
DataColumn str = new DataColumn("STR", Type.GetType("System.String"));
>
dt.Columns.Add(id);
>
dt.Columns.Add(str);
>
>
>
DataRow dr;
>
>
dr = dt.NewRow();
>
dr["STR"] = "horses'";
>
dt.Rows.Add(dr);
>
>
>
string ster = "STR = 'horses'";
>
DataRow[] wyn = dt.Select(ster);
>
>
>
I get an error shown below.
>
>
An unhandled exception of type 'System.Data.SyntaxErrorException' occurred
in system.data.dll
>
Additional information: The expression contains an invalid string constant:
'horses''.
>
I know its related with the apostrophe character, but cant "google" anything
to make this work properly. Could you help me please?
Try a double-apostrophe:

string ster = "STR = 'horses'''";

--
Patrick Steele
http://weblogs.asp.net/psteele
Tom
Guest
 
Posts: n/a
#3: Oct 10 '06

re: Apostrophe in Column value of DataTable select query


Thanks, it's working :)


Uzytkownik "Patrick Steele" <patrick@mvps.orgnapisal w wiadomosci
news:MPG.1f94115ee23953b29896a4@msnews.microsoft.c om...
Quote:
In article <egdd46$pir$1@atlantis.news.tpi.pl>,
bear007.anty_spamerowanie@poczta.onet.pl says...
Quote:
>Hi, I have some kind of problems with an apostrophe character ('). I
>would
>like to select from DataTable DataRow containing value horses' (with an
>apostrophe on the end). But when I do it in an obvious way, like this:
>>
>>
>>
>DataTable dt = new DataTable();
>>
>DataColumn id = new DataColumn("ID", Type.GetType("System.Int32"));
>>
>DataColumn str = new DataColumn("STR", Type.GetType("System.String"));
>>
>dt.Columns.Add(id);
>>
>dt.Columns.Add(str);
>>
>>
>>
>DataRow dr;
>>
>>
>dr = dt.NewRow();
>>
>dr["STR"] = "horses'";
>>
>dt.Rows.Add(dr);
>>
>>
>>
>string ster = "STR = 'horses'";
>>
>DataRow[] wyn = dt.Select(ster);
>>
>>
>>
>I get an error shown below.
>>
>>
>An unhandled exception of type 'System.Data.SyntaxErrorException'
>occurred
>in system.data.dll
>>
>Additional information: The expression contains an invalid string
>constant:
>'horses''.
>>
>I know its related with the apostrophe character, but cant "google"
>anything
>to make this work properly. Could you help me please?
>
Try a double-apostrophe:
>
string ster = "STR = 'horses'''";
>
--
Patrick Steele
http://weblogs.asp.net/psteele

Closed Thread