Help with database connection and setup

Help with database connection and setup

Page 1 of 1

1 Replies - 1406 Views - Last Post: 26 October 2009 - 12:23 AM Rate Topic: -----

#1 BeeMarie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-October 09

Help with database connection and setup

Posted 23 October 2009 - 08:52 PM

Hi,

Myself and another student have spent hours trying to figure out how to do this. Our instructor has given us an exam (due by Sun Evening) and the assignment includes stuff he hasn't explained yet. :blink:

Using VB.Net windows form, I have connected my MS Access database and got a dataset using the GUI tools. I have learned how to connect the dataset to datagrids and other controls.

My problem is, I need to get values from different rows and columns, perform calculations, and then write the values to a text file (I know how to do the calculations and text file part.)

The part we don't get is how to get the individual values of the database row/field so that we can do stuff with it in code.

Can anyone help out with this? My fellow classmate spent all day trying to figure it out, and I've been trying for about 4 to 5 hours now myself. None of the instructions out there seem to be appropriate for VB 2008 and Access database with a Windows Form.

Thanks!
~BeeMarie

Is This A Good Question/Topic? 0
  • +

Replies To: Help with database connection and setup

#2 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Help with database connection and setup

Posted 26 October 2009 - 12:23 AM

Hi,

So you have a problem with the dataset, reading form rows and columns....

Where is your code for the dataset?

Basically it works like this,

Say you have a dataaset called dsMyData. If you drop a Table Adapter (named "MyTable") onto your dataset, it creates a data table and a Table adapter to your Table in your database.

So now we have 3 components

1. The DataSet -> dsMyData
2. The Data Table -> MyTable
3. A Table Adapter -> MyTableTableAdapter

Now if you open a form and go to your datasources window (Shift + Alt + D)
you should find the dataset you created in the datasources window.

You can expand the dsMyData and you will find MyTable under the dsMyData tree node.
by default it should look like a small DataGridView icon.

Drag the MyTable onto your form, and a Vew Items wil be created....

1. A DataGridView (if the icon Next to the MyTable in the datasources window looked like a DataGridView)
2. A DataSet ->dsMyData
3. A Binding Source -> MyTableBindingSource
4. A TableAdapter -> MyTableTableAdapter
5. A BindingNavigator -> MyTableBindingNavigator
6. A Fill Toolstrip -> Optional if you only have one querry in your dataset with paramters.

Now let me explain a few things.

1. A DataSet, can have multiple tabes (with relationships)
2. A BindingSource is a tool that Binds Controls to your tabel in your dataset. It can do navigation between records, call add new methods and do delete calls.
3. A Table Adapter does the connection to the database and calls the update, delete and select methods. It has the connectionstring used to call these methods.
4. The BindingNavigator is a visual Toolstrip that works hand in hand with the Binding Source. It Normally does has the methods involved for doing the navigatin adding and deletion automaticall set. In my eyes not such a good thing since it does not confirm if a record is deleted etc.

Now look at your code.

Your form load event has something like this

Me.MyTableTableAdapter.Fill(Me.dsMyData.MyTable)



This function goes and get the data from the database and puts it in the "Me.dsMyData.MyTable" Table.

So if there is data, you can navigate between records in a couple of ways.

With the BindingSource

Me.MyTableBindingSource.MoveNext

'or

Me.MyTableBindingSource.MoveLast

'or

Me.MyTableBindingSource.MovePrevious

'or

Me.MyTableBindingSource.MoveFirst



You can even move to a certain record number provided you know which record number it is
eg.

Me.MyTableBindingSource.Position = 5



Or, you can use the data table in a loop to get the values

dim x as integer

for x = 0 to Me.dsMyData.MyTable.Rows.Count - 1
  msgbox(Me.dsMyData.MyTable.Rows(x).Item("SomeColumnName"))
next



To add items is a bit different and I will not discuss it at this moment. I might write a Tutorial About this soon...

But I hope this helps you in the right direction.. (sorry i know its not sunday anymore..)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1