15

Working on a C# console app, I have a line:

rowsFound = tempUsers.Select("EmailAddress = '" + userData[2].ToString() + "'");

rowsFound is a DataRow[], tempUsers is a DataTable, and userData is a SqlDataReader. I had the wrong index for userData (it was 1) and I got this error:

System.Data.SyntaxErrorException was unhandled
   Message=Syntax error: Missing operand after 'Bannon' operator.
   Source=System.Data
StackTrace:
   at System.Data.ExpressionParser.Parse()
   at System.Data.DataExpression..ctor(DataTable table, String expression, Type type)
   at System.Data.Select..ctor(DataTable table, String filterExpression, String sort, DataViewRowState recordStates)
   at System.Data.DataTable.Select(String filterExpression)

When I inserted the correct index (2), the error went away.

Any ideas on what a "Bannon operator" is?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Greg Bair
  • 670
  • 6
  • 20

3 Answers3

25

That guy was probably called O'Bannon (= userData[1]), resulting in the following string:

EmailAddress = 'O'Bannon'

The following Stackoverflow question contains a guide on how to properly escape data used in DataTable.Select:

With respect to single quotes, you just need to duplicate them: ' -> ''.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
8

userData[1].ToString() was probably something like Bobby' Bannon

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

Please see this link. Bannon comes from the either UserData[2].ToString()

Jethro
  • 5,896
  • 3
  • 23
  • 24