{"id":712,"date":"2008-07-08T11:22:10","date_gmt":"2008-07-08T10:22:10","guid":{"rendered":"http:\/\/www.itwriting.com\/blog\/712-sample-code-for-a-very-very-simple-vb-database-application.html"},"modified":"2008-07-08T11:22:10","modified_gmt":"2008-07-08T10:22:10","slug":"sample-code-for-a-very-very-simple-vb-database-application","status":"publish","type":"post","link":"https:\/\/www.itwriting.com\/blog\/712-sample-code-for-a-very-very-simple-vb-database-application.html","title":{"rendered":"Sample code for a very very simple VB database application"},"content":{"rendered":"<\/p>\n<p>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.<\/p>\n<p>The piece is actually in two parts. Part one shows how to do CRUD without any databinding or datasets.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.itwriting.com\/images\/simpledb.gif\" \/> <\/p>\n<p>You can <a href=\"http:\/\/www.itwriting.com\/pcw\/pcwdbexample.zip\">download the code here<\/a> \u2013 but please don\u2019t bother if you can already do this in your sleep. The app is for <a href=\"http:\/\/www.microsoft.com\/express\/vb\">Visual Basic Express 2008<\/a>.<\/p>\n<p>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 <a href=\"http:\/\/www.itwriting.com\/blog\/689-where-is-your-sql-server-ce-database.html\">not to be fully compatible with the wizards<\/a>. Typical Microsoft \u2013 simple, but with enough gotchas to frustrate beginners and keep experts in business.<\/p>\n<p>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:<\/p>\n<blockquote>\n<p>Creates an item with the specified key, text, and image and adds an item to the collection.<\/p>\n<\/blockquote>\n<p>The strange thing is, the ListViewItem has no key property. So how do you retrieve the value of the key? <\/p>\n<p>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?<\/p>\n<p>I guess that would be too easy.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:0767317B-992E-4b12-91E0-4F059A8CECA8:47685823-d3cf-468e-8d19-fd812ac0bb89\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">Technorati tags: <a href=\"http:\/\/technorati.com\/tags\/visual+basic\" rel=\"tag\">visual basic<\/a>, <a href=\"http:\/\/technorati.com\/tags\/database\" rel=\"tag\">database<\/a>, <a href=\"http:\/\/technorati.com\/tags\/sql\" rel=\"tag\">sql<\/a>, <a href=\"http:\/\/technorati.com\/tags\/sql+server+ce\" rel=\"tag\">sql server ce<\/a>, <a href=\"http:\/\/technorati.com\/tags\/visual+studio\" rel=\"tag\">visual studio<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.itwriting.com\/blog\/712-sample-code-for-a-very-very-simple-vb-database-application.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Sample code for a very very simple VB database application<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80,97],"tags":[],"class_list":["post-712","post","type-post","status-publish","format-standard","hentry","category-software-development","category-windows"],"_links":{"self":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/comments?post=712"}],"version-history":[{"count":0,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/712\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/media?parent=712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/categories?post=712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/tags?post=712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}