Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  ADO.NET for Beginners Part Two
 »  Home  »  Data Programming  »  ADO.NET  »  ADO.NET for Beginners Part Two
ADO.NET for Beginners Part Two
by David Jeavons | Published  07/18/2006 | .NET Newbie ADO.NET | Rating:
David Jeavons
I have been programming database applications for almost 12 years starting out with VB3 and using every major version since then. I currently work for a retailer analysis company writing ASP.NET code for our ever evolving web site. I also enjoy helping other programmers with Visual Basic and .NET and am a moderator at vbCity. 

View all articles by David Jeavons...
Modifying the Address Book User Interface

In order to demonstrate how we can manage data in the database we will add a few controls to the existing form that will allow us to modify some of the fields in the Contacts table. Note that I have purposely only added controls to modify some of the fields in the table. The reason for this is twofold.  

First, it will be a good exercise for you to add the remaining fields to the interface once you are happy with your understanding of working with databases, and secondly, the code samples will be quite long winded if all fields were listed. After all, the aim of this tutorial is to show you some of the techniques that you can employ when working with databases and I am sure that your next database application will be somewhat different to a simple Address Book.

So, without further ado, start by adding three new buttons to the Address Book form as follows:

Name Text
btnAdd Add
btnEdit Edit
btnDelete Delete

We will also modify our existing code slightly (not that there is much of it at the moment). Currently, we are retrieving all Contacts data within the Form Load event which is fine for displaying the initial data to the user. However, when we add new records or amend existing records, it will be quite awkward having to keep writing the same retrieval code again in order to show the changes. So the easiest approach would be to take the code that retrieves the contacts data and place it into a sub routine which we can then call from both the Form Load event and any other event that we see fit.

I have created the following routine called RetrieveContacts and moved the code from the Form Load event to this new routine:

and then added a call to the above routine within the Form Load event:

The last thing we need to do before moving on to modifying our data is to create a form that we can use to add new data and edit existing data. So, if you add a new form to your project and call it "ManageData", then add the following controls to the form:

Control Name Text
Label lblTitle Title
TextBox txtTitle  
Label lblFirstName First Name
TextBox txtFirstName  
Label lblLastName Last Name
TextBox txtLastName  
Button btnSave Save
Button btnCancel Cancel

it should then look similar to the following:

As you can see, we will only be concentrating on adding and updating three of the fields in the Contacts table. It will be a further exercise should you wish to complete the project to add the additional controls and modify the code accordingly to manage all of the fields in the Contacts table.

Sponsored Links