Getting results from an ODBC database

cBarry263

Active member
Joined
Oct 29, 2005
Messages
42
Location
Maryland
Programming Experience
Beginner
I have a program in which I want to poll a database via an ODBC connection. I have set up the connection, and the query. THe problem is, I can't figure out how to see the results of my query. Basically, the table in the database has a bunch of rows, each containing a field called "Site_ID". I want to have all these site_ID's returned so that I can loop through them and do some processing. Any help would be greatly appreciated. Here's what I have that is not working:

Dim centralObjDataAdapter As New OdbcDataAdapter
Dim centralDataSet As DataSet = New DataSet
centralObjDataAdapter.SelectCommand.Connection = ODBCconn
centralObjDataAdapter.SelectCommand.CommandText = selectString
centralObjDataAdapter.Fill(centralDataSet, "Central Results")
.
.
NTFNsiteIDs = centralDataSet.Tables("Central Results").Rows(0).Item(0)

I would expect this to return the first site_ID from the table but I am getting an "Object reference not set to an instance of an object" error. I'm not sure how this error applies here.
Also, I'm having a lot of trouble with accessing data and such via ODBC, anyone know of any good websites that explain this?
 
This is the sort of stuff you are supposed to use the debugger for. First off, you need to establish what the null reference is. Place a breakpoint on that line and then evaluate individual expressions in the Watch window. Try them in this order:

centralDataSet.Tables("Central Results")
centralDataSet.Tables("Central Results").Rows(0)
centralDataSet.Tables("Central Results").Rows(0).Item(0)

I'm betting that you'll find that the second one is Nothing, which indicates that the DataTable contains no rows. You could also have checked thos two other ways: testing the return value of Fill, which is the number or records retrieved, or the Rows.Count property of the DataTable. The mistake that many people make is to look at a problem as a whole instead of breaking it up into smaller sections. Also, debugging is a very logical process.
 
Back
Top