Accessing data from a datatable

Hi,

I've been really struggling with this concept for a while now so i really need help.

Im trying to retrieve data from one of my datatables but i just dont understand all this datarow/datacolumn stuff. I would have thought it would have been a simple exercise but its not.

Can someone do some sample code for me. Lets just say i have the following in my project.

Dataset called Dataset1

Datatable called Datatable1 which looks like this:

Topic Data1 Data2 Data3

A 100 200 300

B 400 500 600

C 700 800 900

If i want to retrieve the value from the "Data2" column and the "B" row (which would be 500)... how do i do that

It doesnt make sense to me... the code ive seen doesnt seem to reference columns at all... it just uses rows

I have been doing something like this:

Private Sub Button1_Click(blah blah blah)

Dim Column1 As DataColumn

Column1 = Me.Dataset1.Datatable1.Columns("Data2")

Dont know what to do from here

End Sub

I have searched the internet for hours and found nothing. The help on VB2005 sucks big time and i even went to the local bookstore for books on VB2005 but couldnt find any. Thats another question.. can anyone suggest a good VB2005 book to help me learn

I would consider you a GOD if you can help me out here.

Thanks in advance,

Aaron



Answer this question

Accessing data from a datatable

  • paraGOD

    Put everything in the form on load event. There are other places you could put it but that would simply confuse you at this point. If you were to put the sub routine in the combobox_dropdown event the only 'computer power' it would take would be to retreive the values from the database. But this what the datasets and table adapters were designed to do. Simply try it and aee how well or not it works.



  • Maurice Maglalang

    Why do you need to use a datatable .... Would a data reader not be sufficient if you are simply reading data from the database....

  • Jivitesh

    OK ive had a bit of succes here. I watched the video Lesson 9: Databinding Data To User Interface Controls. It was EXTREMELY helpful!!

    I understand how it works now and found out what i was doing wrong (or more accurately.. what i wasnt doing that i should have been doing). It seems i wasn't filling my dataset with data. I assumed this was all done automatically. I added the following code which is activated by a control button (Im just listing it here for those of you who may be having the same problem i had).

    Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click

    Me.TestTableTableAdapter.Fill(Me.TestDataSet.TestTable)

    End Sub

    I then run my little check subroutine that confirms i have some data in those datatables:

    Private Sub TestButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestButton.Click

    Dim Value As Integer

    Value = TestDataSet.TestTable.Rows.Count

    MsgBox("There are " & Value & " rows in your datatable")

    End Sub

    So finally i have some data.

    Whats the best way to get a TableAdapter to fill a dataset automatically without the need for a seperate control to run the sub-routine Is it wise to use it with something like the Load form event Does it take a lot of computing power to do if say i put it in a sub-routine that is activated by a combobox_dropdown event i.e. it would run everytime i clicked the drop down arrow

    Regards,

    Aaron


  • trogers

    Dont know how to use data readers.


  • habnix

    Thanx Jack!! You are a GOD!

    I finally understand how it works now.. its amazing how a simple example explains it better than pages of words and help files!!!

    I tried that code (i changed the names to suit my project) but i get an error saying "There is no row at position 1". Does this mean i've set up my database wrong Im sooo close to getting it to work now! It would be great if you could help me out again.

    Thanks for your help so far. I really appreciate it.

    Aaron


  • Javelin

    Well thats the thing... what code to get the data out of the database into the dataset I've done it all through the wizards so whatever code goes on i have no idea. I simply created a database by going to the project menu and adding a SQL database in add items. I then choose to have tables as objects in the database. After that i add a table, define the columns and fill the table with data by manually typing it in. I then bind the dataset to the form. Am i missing something.. do you still need to use dataadapters and the like Don't you just use them for transferring data from on dataset to another and processes like that

    Basically my database is going to be very simple and doesnt require complex code. It is simply there to store data in tables which i can then pick out to use in mathematical equations so i dont need all the tableadapters and all that stuff as i wont be adding or changing any values in the database.. i just need to get data from it.

    Regards,

    Aaron


  • GTH

    Actually there is a bit of a problem now. It appears that the datatables have no data in them! I have filled the tables with data and bound the data table to my form by dragging the icon to it... but the data isnt showing in the tables. I wrote some simple code to count how many rows of data i have but i always get the same result... ZERO! heres the code...

    Private Sub TestButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestButton.Click

    Dim Value As Integer

    Value = TestDataSet.TestTable.Rows.Count

    MsgBox("There are " & Value & " rows in your datatable")

    End Sub

    This explains the error earlier i.e. 'there is no row at position 1'.

    I am really baffled. I created a new table in my database... named the columns and set the datatypes..filled the rows with data.. bound the datatable to my form.. and nothing! . VB can find the table and columns alright but it cant find any data. When i preview the data its there but it just cant find it for some reason.. i think i missed a step somewhere where you declare something.

    Can anyone help me out

    Regards,

    LOST


  • RoseyA

    I admit my experiences with DataSets are a bit rusty since I use DataReaders far more often.

    First I would probably recommend using TypedDataSets where possible since they are cool and type safe.

    Secondly the DataSet will always represent in memory the physical schema of your database but you have to Fill it with a DataAdapter for any data to be in it.

    Depending on a lot of things in your project this might happen automatically or you might have to manually do it. I'd really need more of your code to see where everything was happening.

    The MSDN docs aren't very useful for figuring out how to do something complex, but they are very good for referencing classes and how to use them. Look up the DataAdapter and TableAdapter and try to understand them better. Please post the code you use to get the data out of the DB and into the DataSet



  • wqmao

    Hi Aaron,

    You can do the following:

    Dim CurrentRow As Integer

    Dim row As DataRow

    CurrentRow = 1

    row = Me.dataset1.Tables("Datatable1").Rows(CurrentRow)

    Value = row("Data2")

    Regards,

    Jack


  • Accessing data from a datatable