Sample code for a very very simple VB database application

I wrote a short piece for Personal Computer World about making a simple Windows Forms database application. I did this because I get a lot of enquiries about it, and search hits to this site looking for samples.

The piece is actually in two parts. Part one shows how to do CRUD without any databinding or datasets.

You can download the code here – but please don’t bother if you can already do this in your sleep. The app is for Visual Basic Express 2008.

Part two is about using the VB wizards to create an app with a typed dataset, TableAdapters and so on. The database is SQL Server CE, which is well suited to this kind of application. It is the default in Visual Studio 2008 even though it turns out not to be fully compatible with the wizards. Typical Microsoft – simple, but with enough gotchas to frustrate beginners and keep experts in business.

I ran into another little puzzle while doing the sample. I needed to populate the listbox with both a string value and the ID that is the primary key in the database table. The way I would do this normally is to create a custom class to represent the record, implement a ToString() that returns the display value, and add instances of this object to the listbox. I wanted an even simpler way though, so I decided to use a ListView. This lets you add items that have both a key and a value. You can do this with one of the overloaded Add methods for a ListViewItemCollection, documented like this:

Creates an item with the specified key, text, and image and adds an item to the collection.

The strange thing is, the ListViewItem has no key property. So how do you retrieve the value of the key?

The answer is that the ListViewItem.Name property returns the value of the key. So the key is the name. Why not call it the name in both places? Or the key?

I guess that would be too easy.

6 thoughts on “Sample code for a very very simple VB database application”

  1. Would like to see a version written in Visual C++ Express 2008 as this does not include the creation of local database using wizard. It would need all hand coding.

  2. Peter

    You can easily create the db in one of the Express Visual Studios, through the table designer.

    You can also do it in SQL if you prefer.

    If you want to use C++ you might be better off with Sqlite.


  3. Hey,

    I think you could have used your ListBox and then just used DisplayMember to display the value you want to see whilst using ValueMember to present a different value.

    That only works if you’re binding to a particular object that has the 2 properties on it that you want to use as display/value of course.


  4. hi
    i am from iran
    i searching persian site for simple code but i can not find any a bout
    al of code was copy of msdn site

  5. Dear Mr.Tim,
    I just loved your schoolapp. In fact I just began to lear from you. My modifying it I even succeeded in making a programme for my purpose also. Why don’t you make another one with more than one table and without data binding.
    Unnikrishnan C

Leave a Reply

Your email address will not be published. Required fields are marked *