Good practice to save data – Windows Forms


The Setup

I’m going to explain about data entry in VB.Net Windows form, retrieving data, manipulating it, validating it and saving it back to your database.

Configure the form

We now need to get the data on our form so that it can be displayed to the user, so add a new Windows Form called “frmMain” to your project.

Hover over the main table in the data set and choose “Details” from the drop down. Now drag the ID and some other fields from the data source to the form. This will create the following controls on your form:

Binding Navigator
Data Set
Table Adapter
Binding Source
The controls to display the data (these alter depending on the database column details)

You now have a form that you can now compile to return the data, the code view will show the loading of the data and saving of the data too. We’re now going to make the form handle errors during the save gracefully and also prompt the user to save the data if they navigate to a different form or try and close the form without saving.

Loading the data

As we are using a binding source, we take a snapshot of the data and store it in the binding source. If you require the information updated more often that this, then you’ll need to fill the data adapter more often. If you are confident that you don’t need to update the snapshot in-between viewing the data, then filling the data adapter once per form is fine. Add a refresh tool strip button to the binding navigator so the user can refresh the data at their convenience.

Note: filling the data table will clear all of your uncommitted changes, so also remember to save the data before it is overwritten (by another source).

The Save Function

Use the code in the Save Record function post, this will return true if the save is success or false is unsuccessful. A successful save occurs of the data is recorded in the database, the data doesn’t need to be saved or the user chooses not to save the data. An unsuccessful save can happen if there are data validation problems, if the database rejects the save or if the user chooses to cancel the save.

When to save?

The save function should be called in the following places:

After the save binding navigator button is clicked
Before navigating between records
Before adding a new item to the binding source
When the form is closing

By using the save function, you will be able to code around all eventualities of the save outcome.

	If True Then
	 If Me.SaveControl.Enabled = True Then
	 ' Prompt the user to save the record
	 SaveRecord(True)
	 ' Allow adding a new item
	 Me.BindingNavigatorAddNewItem.Enabled = True
	 ' Allow deleting deleting the item
	 Me.BindingNavigatorDeleteItem.Enabled = True
	 End If
	End If

Ensuring the save is called at the right time

To ensure the save is called at the right time (as listed above), we set the save button on the form to disabled by default and when the data in a control is altered we set it to enabled. This will then allow the user to save the data. We do this to display to the user that the data has changed from the database.

	Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
	 ' Set the save control to be enabled
	
	 Me.SaveControl.Enabled = True
	End Sub

Validating the data

By calling the Validate method, we can be sure that the data being committed to the database will be accepted without any errors. For each control that should be validated, we need to make sure the following is correct:

Data present if Allow Null is set to false on the table
The data is the right type
The format of the data is correct (i.e. an email address)

A good example of this can be found in the following MSDN article. It details how to validate an email address and display a warning to the user if it doesn’t contain a valid email address.

If you use global unique identifier as the field type for your ID column, then you must make sure that the field is displayed on the form. It appears to be a bug in Visual Studio that it doesn’t set the guid value for these columns automatically, so these need to be set in the validation method by calling the following:

	'Set the ID value as a new GUID
	Me.txtID.Text = new Guid().ToString

Further information

I have an VB.Net example that includes using all of the above in a nice little form, drop me an email if you like to take a look.

3 Comments

Filed under Windows Forms

3 responses to “Good practice to save data – Windows Forms

  1. Hi dear author,

    I have a question: how do I insert a document file (can be either ms Word or ms Powerpoint) into ms Access database? I wish to insert it as an object, so that later I can retrieve (or so called “download”) it from the other page. Any suggestion for me? Thank you very much..

    Regards,
    Ceci

  2. Andrew Cowan

    Hi Martin,
    I have sent an email to you regarding a method that my tutor at university has taught us. It differs to yours in that it does not handle the capturing of errors very well. If it is not too much trouble I would very much appreciate you looking at the email I have sent you and giving me some hints as to how I might be able to solve this problem.
    Regards,
    Andrew

Leave a comment