Windows Forms Navigating database with windows forms VB
Windows Forms Navigating database with windows forms.
VB. net application Tiered application o Provide a means to develop many presentations of the same app o Makes changes to the back end easier when it is not tied into one class
Tiers continued o You have three tiers n n Presentation (The form or front end) Business o o n Used to make all the calculated values in the form Enforces the Business rules of your application Data Tier (Provides connections and manages rules (SQL) of the database)
The Data tier o o o A windows component added to your project Like the form, a windows component is also a class. This class will contain all the Connection, Data. Adapters and Data. Sets that are needed for the data Tier. n n Each of these are considered to be private To access these objects and all methods and properties, require the use of Accessor methods/sub procedures that you must create
Data Tier (Continued) o Two methods are needed in the data tier 1. 2. o o o A function that will return a reference to the datset to the form (get. Data. Set()). A sub procedure that will provide access to the Update method of the Data. Adapter (Update()) AS a refresher All SQL statements are managed by the Data. Adapter All Connections to the database are managed by the connection object A local copy of the database in the Data. Set
The Data Tier in detail
Data. Sets o o o Data. Sets 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 Data. Adapter takes only the rows of data that have changed and then push them onto the database.
Data. Set Continued o o o Data can change on a DBMS on a network frequently Once an update occurs, the Data. Set must accept all changes that Occur in the DBMS The Accept. Changes() function of the datset must be called after an update is made
Adding and Editing Form design and changing data in a database
Form design for Adding records o When adding or editing a record in the data set n n n 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 o o i. e. Edit and Delete buttons Separate sub procedures should be created to do this
Navigation sub procedures o When a form opens n n n o All textboxes should be disabled Buttons that are not needed should also be disabled (i. e. Cancel button) This is often called enabling navigation There are therefore two sub procedures that must be created n n enable. Nav() disable. Nav()
Example code of enble. Nav() Private Sub enable. Nav() txt. First. NAme. Enabled = False txt. Last. Name. Enabled = False txt. Address. Enabled = False txt. City. Enabled = False txt. State. Enabled = False txt. Zip. Enabled = False btn. Next. Enabled = True btn. Prev. Enabled = True btn. Cancel. Enabled = False End Sub
Example code of disable. Nav() Private Sub disable. Nav() txt. First. NAme. Enabled = True txt. Last. Name. Enabled = True txt. Address. Enabled = True txt. City. Enabled = True txt. State. Enabled = True txt. Zip. Enabled = True btn. Prev. Enabled = False btn. Next. Enabled = False btn. Cancel. Enabled = True End Sub
Adding Records o The Binding. Manager. Base object in the form contains an Add. New() Sub Procedure which n n 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 added to the event handler of the Add Button
Example Form for adding a recod
Adding a record continued o o Buttons can also be used for two purposes With the Add button n n 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).
Saving a record o There are specific steps that are taken to save a record to a database 1. 2. 3. 1. Call the End. Current. Edit() sub procedure in the Binding. Manager. Base object Call the Update sub procedure in the Data. Adapter (located in the data tier) Call the Accpet. Changes sub procedure in the Data. Set (also located in the data tier) Taken these steps in this order will ensure that all the data will be saved to the database
Edit buttons follow the same design as add
- Slides: 18