views:

541

answers:

1

I have 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'm using VB.NET, but comments with apostrophes in here don't seem to be identified.

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

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

+1  A: 

Have you checked what the column thinks it is called? It might have underscores, for example. Loop over the columns and find out (sorry, examples in C#):

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

Actually, it is faster to use the column if you are doing it in a loop, so I might use something like:

DataColumn col = table.Columns["whatever"];
foreach(DataRow row in table.Rows) {
    Console.WriteLine(row[col]);
}
Marc Gravell
OUCH! I could have solved this without this question, just by paying attention that it has TWO spaces instead of one. But your answer made me take a closer look at the name itself (I guess the [space] char in access's typeface is a bit smaller, which misled me). Thank you very much, right answer and +1 =)
MarceloRamires