Getting a value from a field in a DataTable when the column name has spaces

I tried:

ObjDTOleDBNFeIntegra.Rows(I)("[Cnpj Cpf]").ToString() //with brackets  
ObjDTOleDBNFeIntegra.Rows(I)("'Cnpj Cpf'").ToString() //with apostrophe  
ObjDTOleDBNFeIntegra.Rows(I)("Cnpj Cpf").ToString() //without anything  

I use VB.NET, but comments with apostrophes are not displayed here.

And I get exceptions for each case:
Column '[Cnpj Cpf]' does not belong to table Table.(crash) Column 'Cnpj Cpf' does not belong to table Table.(crash) Column ''Cnpj Cpf'' does not belong to table Table.(crash)

What should I do to get a value from a field in a dataTable when the column name has spaces?

+3
source share
1 answer

Have you checked what the column says is called? For example, this may have underscores. Sort through the columns and find out (sorry, examples in C #):

foreach(DataColumn col in table.Columns) {
    Debug.WriteLine(col.ColumnName);
}

, , , - :

DataColumn col = table.Columns["whatever"];
foreach(DataRow row in table.Rows) {
    Console.WriteLine(row[col]);
}
+10

Source: https://habr.com/ru/post/1735986/


All Articles