Apostrophe in Column value of DataTable select query 
October 9th, 2006, 12:35 PM
| | | |
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 | 
October 9th, 2006, 01:45 PM
| | | | 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 | 
October 10th, 2006, 03:35 PM
| | | | 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 | |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|