Navigating database with windows forms.. Tiered applications Provide a means to develop many...

19
WINDOWS FORMS Navigating database with windows forms.

Transcript of Navigating database with windows forms.. Tiered applications Provide a means to develop many...

Page 1: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

WINDOWS FORMSNavigating database with windows

forms.

Page 2: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

VB .net application

Tiered applications Provide a means to

develop many presentations of the same app

Makes changes to the back end easier when it is not tied into the front end (presentation Tier)

Page 3: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

Tiered Applications (Continued)

Provides more portability for code created in each business app.

Provides a way to create a Data connection that is a web service on a network.

Can easily create multiple apps for different platforms at the same time. Apps for mobile devices Apps for Windows Apps for the web

Page 4: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

Tiers continued

You have three basic tiers Presentation (The form or front end) Business

Used to make all the calculated values in the form

Enforces the Business rules of your application Enforces data validation

Data Tier (Provides connections and manages rules (SQL) of the database) The data tier can be created for any DBMS Can be a Windows/Web service providing data to

multiple apps on a network.

Page 5: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

The Data tier

To create a Data Tier Right click on the project and select new Item Select new class

This class will contain references to the TableAdapters and DataSet

The TableAdapter contains the Connection, DataAdapters and Command Objects that are needed for the data Tier.

To access these objects and all methods and properties, require the use of Accessor methods/sub procedures that you must create

Page 6: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

Data Tier (Continued)

Two methods are needed in the data tier1. A function that will return a reference to the datset

to the form (getDataSet()).2. A sub procedure that will provide access to the

Update method of the TableAdapter (setDB())AS a refresher

All SQL statements are managed by the DataAdapter located inside the TableAdapter.

All Connections to the database are managed by the connection object which is also located inside the TableAdapter.

A local copy of the database in the DataSet

Page 7: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

The Data Tier in detail

Page 8: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

DataSets

DataSets hold a local copy of the database.

Any changes of the dataset have to be pushed back to the server

The Update Function in the TableAdapter takes only the rows of data that have changed and then push them onto the database.

The Accept method of the DataSet is needs to be called once an update is performed.

The Accept method clears all flags associated with rows that have been updated.

Page 9: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

DataSet Continued

Data can change on a DBMS on a network frequently

Once an update occurs, the DataSet must accept all changes that Occur in the DBMS

The AcceptChanges() function of the datset must be called after an update is made

Page 10: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

ADDING AND EDITING Form design and changing data in

a database

Page 11: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

Form design for Adding records

When adding or editing a record in the data set all navigation should be disabled. This prevents the user from accidentally

moving the position of the record during an edit.

All other buttons should be disabled i.e. Edit and Delete buttons Separate sub procedures should be created to do

this EnableNav() EnableEdit()

Page 12: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

Navigation sub procedures

When a form opens All textboxes should be disabled and ready for

navigation Buttons that are not needed should also be

disabled (i.e. Cancel button) With the enable navigation function you have a

choice of disabling the control or to make it Read only (programmer preverence)

The enable Edit will Enable all the textboxes and disable all navigation. By disabling navigation during edits, you prevent

accidently editing a different record in the DB The edit or Add button’s text should change to a

save button. This saves on Form real estate.

Page 13: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

Example code of enbleNav()

Private Sub enableNav() txtFirstNAme.Enabled = False txtLastName.Enabled = False txtAddress.Enabled = False txtCity.Enabled = False txtState.Enabled = False txtZip.Enabled = False btnNext.Enabled = True btnPrev.Enabled = True btnCancel.Enabled = False End Sub

Page 14: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

Example code of enableEdit()

Private Sub disableNav() txtFirstNAme.Enabled = True txtLastName.Enabled = True txtAddress.Enabled = True txtCity.Enabled = True txtState.Enabled = True txtZip.Enabled = True btnPrev.Enabled = False btnNext.Enabled = False btnCancel.Enabled = True End Sub

Page 15: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

Adding Records

The Binding Manager object in the form contains an AddNew() Sub Procedure which clears all bound textboxes Moves the position of the record to the end Auto increments all key id fields (if they are

set for auto increment) This sub procedure is needed in the event

handler of the Add Button in order for users to add a record to the DB.

Page 16: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

Example Form for adding a recod

Page 17: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

Adding a record continued

Buttons can also be used for two purposes

With the Add button Click once it places the form in a position to

add a record Once the information is added to the text

fields, the button can be clicked again to save the record (code provided to student).

Page 18: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

Saving a record

There are specific steps that are taken to save a record to a database

1. Call the EndEdit() sub procedure in the Binding Manager object

2. Call the setDB() sub procedure in the DataTier• This sub procedure in the Data Tier will call the

update method in the Table Adapter.• (please read tutorial on how to create the data tier)

3. Call the AccpetChanges sub procedure in the DataSet (also located in the data tier)

Taken these steps in this order will ensure that all the data will be saved to the database

Page 19: Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.

Edit buttons follow the same design as add