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.

VN:F [1.9.18_1163]
Rate this post
Rating: 10.0/10 (1 vote cast)
Sample code for a very very simple VB database application, 10.0 out of 10 based on 1 rating

Related posts:

  1. A Silverlight database application with image upload
  2. Very simple VB 2008 database app with databinding
  3. blist: online database, Flash application, beautiful but flawed
  4. Access Web App: at last a simple web database app builder from Microsoft
  5. Amazon S3 Delphi sample

6 comments to Sample code for a very very simple VB database application

  • Peter

    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.

  • tim

    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.

    Tim

  • 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.

    Mike.

  • Kpac

    Wow Thanks mate! Between here and http://www.vbasic.com I can code just about anything=)

  • mohhamad

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

  • Dear Mr.Tim,
    I just loved your schoolapp. In fact I just began to lear vb.net 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.
    Sincerely,
    Unnikrishnan C
    India.