Apostrophe in Column value of DataTable select query

T

Tom

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
 
P

Patrick Steele

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'''";
 
T

Tom

Thanks, it's working :)


Uzytkownik "Patrick Steele said:
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'''";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top