Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to read fields from DataTable -- ???

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
0
0
US
I've looked through all my books and can't find how to access individual fields in my DataTable. Seems if you don't want to use some form of binding to a grid or textbox there's no explanation.

So can anyone tell me how to read out the individual fields from a DataTable? I assume it has something to do with a DataRow object, but can't find any documentation on this. Please, show a code snippet or explanation? Thanks.

CraigHartz
 
Here's a snippet for looping through a table and getting values. Hope it helps.

Code:
Dim tbl As DataTable = SomeSetOfData
       
For i As Integer = 0 To tbl.Rows.Count - 1
     Dim dr As DataRow = tbl.Rows(i)
     Variable = rw("FieldName")
Next
 
Uhhh,

Are "dr" and "rw" supposed to be the same thing?

CraigHartz
 
Allright, seriously, this is getting annoying. Is there no equivalent procedure to what we did on ADO in VB6? I mean, then you opened a recordset, and could use the rs.fields("<fieldname>") to get the values of the fields. Is there nothing like this in ADO.NET? Tell me I don't really need to iterate through the entire row to find a field value ?!?!?!

CraigHartz
 
I was able to do something like this in my current application.

I set up the ADO connection as such:

(note that my code goes to an Access database but can be modified to go to any data source I believe)

Code:
'connect to database
Dim conn As New OleDb.OleDbConnection( _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & _
            "somedatabase.mdb")

'query database
Dim myCommand As New OleDb.OleDbCommand("SELECT * from SOMETABLE where SOMEFIELD = 'SOMEVALUE')", conn)

'Create a reader, similar to a DAO recordset
Dim myreader As OleDb.OleDbDataReader

'run the query
myreader = myCommand.ExecuteReader

'Now we check for records.  myreader.Read <> False
'is similar to the DAO rs.EOF = False
If myreader.Read <> False Then

'now you get your value using myreader([fieldname]) 
'as follows

String someString = myreader("VolumeReceived")

End If

Does this help?
 
It might, thanks. I'll give it a try. Certainly looks more promising than using the DataSet option (if I had any hair left I'd be pulling it out now).

CraigHartz
 
Yes, "rw" should be "dr".

I thought a datareader might be an option, but you asked how to get data from a datatable.
 
Once you have a data table populated you can access specific rows/columns with:

datatable.rows(i)(j)

where i is an integer representing the row you want and j is either an integer or a string that indicates the column you want.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
ThatRickGuy,

So, the real answer here is that the DataSet / DataTable option is really only useful for things like filling grids, right? Just too much trouble to try and work with it as a data source for unbound fields and such.

I'll continue to look into the DataReader options. Thanks for all the help, I'll
 
Craig, I felt the same way when I first moved from VB6 to .Net. But then once I got some time in working with them, I found that using them for unbound data can be even easier then with the old ADO controls. No more concerns about .movenext or .recordcount. No more .seek. Now I can goto any specific row by it's position identifier, I can determine the number of rows by looking at the datatable.rows.count property, and I can grab the specific rows I'm looking for with datatable.select(condition).

Also, look into a data abstraction layer. At this point I handle data tables significantly less because all of my database interactions go through the abstraction layer. I work with an Employee object instead of writing code and SQL to get data about an employee now.

And the (IMO) greatest thing ADO.Net brings to the toolset is the Dataset.WriteXML method.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top