Search code examples
c#syntaxdatatablesmsdnescaping

Datatable rows select operation showing error when column name has special characters. (C#)


I am trying to perform select operation on a datatable to extract some values from a column. The column that I am targeting has some special characters in it. so the error message is saying,

An exception has occured: Cannot interpret token ']' at position

the syntax that I am using is this,

DataRow[] DataRows = dataSet.Tables["ParentTable"].Select("[ColumnName[somText]]");

I have referred the docs, where they talk about the use of escape sequences in these type of cases, here is the link to that particular section

(https://learn.microsoft.com/en-us/dotnet/api/system.data.datacolumn.expression?view=net-6.0#:~:text=If%20a%20column%20name%20contains%20any%20non%2Dalphanumeric%20characters)

let me know your solutions.


Solution

  • Suggestion from Lei Yang, worked!

    Select(@"[ColumnName[somText\]]")
    

    you can read more about it here.