DotNetWebTutor.com                                                    Link to Us

Simple dataBinding

  • Start a new project and repeat your steps to add a database and bindingsNavigator control
  • Add an access database to your project
    • Select the data menu
    • Select Add New DataSource

menu add new data source

    • Select the database icon and click Next

choose data source type database

    • Select New Connection

choose new connection next

    • If Microsoft Access Database File is not listed in the DataSource click the Change button

change your connection

    • Choose Microsoft Access
    • Click OK

access file

    • Click Browse to find your database file

addconnectionbrowse

    • Click OK

ok

    • Click the Next button

config wizard

    • Visual Studio asks you to copy the file into your project. Click Yes

yes

    • Next it asks you to save the connection string. Click next to save your connection in a connection string

connection string

    • Select the your database table you want to access and click Finish

tables

    • It adds 3 files to your project
      • App.config
      • The database
      • And XSD file representing your dataset

dataset

The database get copied in two places.

  1. Your project folder
  2. The debug folder

Changes made to the database will be made to the copy in your debug folder because that is where the executable is located

  • Add 3 labels and 3 textboxes to your form
    • Set the labels to static text by changing the text properties
      • Survey ID
      • Survey Name
      • Survey E-mail
    • The textboxes will hold data from our database

example

  • Add a bindingsSource control to the page

bindingsource in tray

  • Set the DataSource by clicking the dropdown arrow and expanding the nodes until you get to your dataSet

survey dataset

  • Set the DataMember property of the dataSource to your table: Survey

datamember
This adds a table adapter and adds code in the form_load to fill your data

Now all you need to do it bind your textboxes

  • Select the first textbox
  • Click on the plus sign next to the dataBindings and select the Advanced property

adv

  • Click the button in the advanced property

You can choose to bind any property of the textbox to a datasource

  • Choose text property and select the Bindings dropdown

advbinding

  • Open the Form1 list instance and browse down until you get to the surveyID

surveyID

  • Click OK

no formatting

  • Do the same for the other two textboxes

Now you need to add buttons for navigation

  • Add two buttons and change the text properties to Next and previous

example

  • Add the following code just below the class definition

Private bindMgr As BindingManagerBase

bindingmanagerbase

  • Add this code to the form_load to tie the dataset you created earlier to your bindingManager

bindMgr = MyBase.BindingContext(SurveyDataSet, "survey")
fix for .net 3.5:
bindMgr=MyBase.BindingContext(Me.SurveyBindingSource)

 

bindmgr

  • Now add the following code to the previous button

bindMgr.Position -= 1

  • Add this code to the Next button

bindMgr.Position += 1

  • Test it

simple data binding
You can make changes to the data in the textboxes and it changes the dataset, but it does not change the actual dataStore (your Access database)

  • Add another button and change the text to Update
  • Add the following code to it

Try
Me.BindingSource1.EndEdit()
Me.SurveyTableAdapter.Update(Me.SurveyDataSet.Survey)
MessageBox.Show("Database was updated")
Me.SurveyTableAdapter.Fill(Me.SurveyDataSet.Survey)
Catch ex As Exception
MessageBox.Show("Problem updating database")
End Try

  • Try it by running the EXE file in your debug
  • Make sure you build it first so the EXE file is the latest version
  • Click the Build menu and select Build Solution

build solution
visual basic data binding