Select Value From Specific Table Column in TableAdapter

G

Guest

I'm not really sure how to ask this question because I'm still getting my
feet wet with data access and VB.NET, but here goes:

To start off with, I'm using VB 2005 Express to connect to an Access database.

I have a dataset in which a parameterized query returns the correct result
set in the forms of a tableadapter and a bindingnavigator. In other words, I
can perform my query and see that the tableadapter and bindingnavigator
contain the row(s) of data I expect.

What I want to do is make a decision in my program based on the value in one
of the columns in the tableadapter or bindingnavigator. Here's a short
example:

In my tableadapter and bindingnavigator, I can see the following columns and
values after running my query:
id = 2
partnumber = 12345
itemtype = OPT

Before I display a certain form, I want to know what the value of the
"itemtype" column is so I can customize the information on the form. So, in
pseudocode, I want to do:

If itemtype = 'OPT' Then
aControl.Enabled = TRUE
Else
aControl.Enabled = FALSE
End If

Where I'm stuck is the If itemtype = 'OPT'. How in the world do I find out
what the value is in that column in my tableadapter or bindingnavigator or am
I barking up the entirely wrong tree here?

Thanks in advance for any assistance.

Rich
 
G

Guest

OK, so, after a day of poking around for different ways to ask this question
and looking for answers in dozens of places, I finally found one site that
got me on the right track. Do I remember it? No, and the URL is on my work PC
so all I have to offer is my solution.

In the class where I wanted to make my programming decision, I created an
instance of the DataTable object and an instance of the DataRowCollection
object:

Protected dt As DataTable
Protected dr As DataRowCollection

Then I grabbed the table I wanted then its row collection:

Me.dt = ProductsDataSet.Tables("N_Options") 'The table I want
Me.dr = dt.Rows() 'The table's row collection

Once I had the collection of rows in the table (because of the nature of the
query, I know it _should_ only be a single row), I was able to get the value
of the column I was interested in:

Dim myOType As String
myOType = Me.dr.Item(0).Item("O_Type").ToString()

Now that I have myOType, I can make whatever programming decisions need to
be made.

Is this the "correct" way to do it? I don't know. But I know it works and
that it allows me to do what I need to do programmatically. So if you're
stuck in the same kind of situation I was, hopefully this will help you out
in much less time and with much less frustration than I invested.

Rich
 
C

Cor Ligthert [MVP]

Rich,

A TableAdapter holds no data, it adapts data in a datatable.

A datatable is a part of a dataset (there are more datatables in a dataset).
However in connection with a tableAdapter there is just one. (With the
dataset it is the dataAdapter)

The TableAdapter creates strongly typed datasets. However all datasets can
be used non strongly typed.

Let us see what the dataset looks like.
It holds as I wrote already datatables
It holds as well relations between those tables.
Tables holds
Columns which describes the items and
Rows which hold the items.

Therefore the first item in a dataset is
ds.tables(0).Rows(0).Item(0)

The last is
ds.tables(ds.tables.count -1).Rows(etc......................

In a strongly typed dataset the first item is (I often write this wrong so
check this one)
ds.MyTableRow(0).myItem

Cor
 
G

Guest

Cor,

Thanks for your response. You've given me a much more straightforward (and
probably closer to "standard practice") way to access the information I want.
Instead of creating DataTable and DataRowCollection objects (which end up
being redundant with the existing dataset), I can get the value of the column
I want by doing this (for example):

MessageBox.Show(ProductsDataSet.Tables("N_Products").Rows(N_ProductsBindingSource.Position()).Item("N_Description"))

Rich
 

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