how to deal with missing column in a dataset.

H

Heath P. Dillon

Hi,

I have an application that selects data from a SQL table and stores it in a
dataset. The problem I have is that my dataset can often contain some cols,
and other times not. (Its weird, but its the way the data manipulation
works)

For example in the code below, I get an error as the column "description"
does not exist in the table "in".

What I want to be able to do, is some how determine if the table contains
that col, and if it does then get the value, if not skip it.

It tried 'if qdrow("description") is nothing, but that still looks for the
col "description".


ODBCDataAdapterInboundQuery.Fill(dsInQueue, "In")

For Each Qdrow In dsInQueue.Tables("In").Rows

ABC = Qdrow("description")

Next
 
T

Tom Shelton

Hi,

I have an application that selects data from a SQL table and stores it in a
dataset. The problem I have is that my dataset can often contain some cols,
and other times not. (Its weird, but its the way the data manipulation
works)

For example in the code below, I get an error as the column "description"
does not exist in the table "in".

What I want to be able to do, is some how determine if the table contains
that col, and if it does then get the value, if not skip it.

It tried 'if qdrow("description") is nothing, but that still looks for the
col "description".


ODBCDataAdapterInboundQuery.Fill(dsInQueue, "In")

For Each Qdrow In dsInQueue.Tables("In").Rows

ABC = Qdrow("description")

Next

Two choices for you:
DataTable.Columns.IndexOf (ByVal columnName As String)
DataTable.Columns.Contains (ByVal columnName As String)

Contains returns a boolean, IndexOf returns a zero based index or -1 if not
found.

HTH
 
A

Armin Zingler

Heath said:
Hi,

I have an application that selects data from a SQL table and stores
it in a dataset. The problem I have is that my dataset can often
contain some cols, and other times not. (Its weird, but its the way
the data manipulation works)

For example in the code below, I get an error as the column
"description" does not exist in the table "in".

What I want to be able to do, is some how determine if the table
contains that col, and if it does then get the value, if not skip it.

It tried 'if qdrow("description") is nothing, but that still looks
for the col "description".


ODBCDataAdapterInboundQuery.Fill(dsInQueue, "In")

For Each Qdrow In dsInQueue.Tables("In").Rows

ABC = Qdrow("description")

Next


If you don't know the columns at design time, you must determine them at
runtime by iterating over the datacolumns in the datatable. To find out if a
column name exists in a datatable, you can use the expression

dsinqueue.tables("in").columns.contains("description")

which returns a Boolean (telling you whether the column exists).




Armin
 
C

Cor Ligthert[MVP]

Hi,

This is exactly why strongly type is created. The field becomes then a class
which has its own name.
Then you are warned at design time of this missing column.

Have a look at documentation about a strongly typed dataset on internet.

Cor
 

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