Typed DataSet Goodness

I was working on VisualBlogger once again, and I was really getting frustrated with using app.config to store blog configurations. Using a ConfigSections wrapper was kind of overkill, and so was having the app dependent on an Access database. Andy Smith turned me onto the idea of using a Typed DataSet... something that I had never done before. I had no idea how cool these things are. I just built an XML file with the desired elements, loaded them up into a DataSet, spit out the schema XSD file using the DataSet.WriteXmlSchema method. Using the VS.NET Schema editor, I was able to change the data types, set constraints, and even create automatically incrementing primary keys. From there, I generated a DataSet using the Schema | Generate Dataset option, and voila! Now I have a simple method for accessing typed data, without having to use a database.

So now, the code that I use looks something like this:

1Try

2 Dim BlogTable As Blogs.BlogDataTable = Blogs.Blog
3 Dim row As Blogs.BlogRow
4 Try
5 row = BlogTable.FindByID(CInt(BlogID.Text))
6 Catch
7 End Try
8 If row Is Nothing Then
9 BlogTable.AddBlogRow("DotText", BlogTitle.Text, BlogAlias.Text, BlogUrl.Text, _
10 BlogUserName.Text, Base64.Encrypt(BlogPassword.Text, Base64.Identifier), False)
11 Else
12 row.Title = BlogTitle.Text
13 row._Alias = BlogAlias.Text
14 row.Url = BlogUrl.Text
15 row.UserName = BlogUserName.Text
16 row.Password = Base64.Encrypt(BlogPassword.Text, Base64.Identifier)
17 row.AcceptChanges()
18 End If
19Catch Ex As Exception
20 MsgBox("The settings could not be saved." & Environment.NewLine & Ex.Message & _
21 Environment.NewLine & Ex.StackTrace)
22End Try

Note lines 9 and 10. Generating a Typed DataSet gives me a 1 line method that allows me to add a new record. That's freakin cool. You may notice that I wrapped the built-in FindByID method in a Try/Catch block. This is because there are a few situations where BlogID.Text would be blank, and an empty string doesn't convert to an integer well. If it doesn't work, I don't want an error to be thrown, I just want it to keep going.

I like this Typed DataSet thing. It's pretty cool. Great for situations where a full-blown database is not possible/not practical. This allows me to have an easily-editable configuration system for VisualBlogger that is not subject to the limitations of the System.Configuration.ConfigurationSettings namespace. Speaking of VisualBlogger, it is nearly feature complete. I'm waiting for some information from a few outside sources before I can wrap up the feature stragglers. In the meantime, I'm going to try to add a local autosaving feature, because I've lost three posts in the past 2 days due to errors found in testing. Infortunately, it seems that Passport is down again, so I can't even log into MSN Messenger to talk to the people I need to talk to.

2 Comments

  • I got turned onto Typed DataSets this week myself. I have a wizard-style form with a typed DataSet held in ViewState. It has 7 tables with relationships and all. It's very nearly a mirror of my DB. I use the wizard to add or edit a bunch of rows to it. I use the Xml from DataSet.GetXml() as an input parameter in a stored procedure. This procedure loads the Xml and reconciles the rows with the database. The only problem I have is this: The string format of a DateTime in the DataSet is not compatible with SQL Server 2000. I haven't decided how I'm going to fix this yet, but it shouldn't be too much trouble. Worst case scenario is: I can "manually" change the DateTime format by operating on the xml with regular expressions before sending it into the sproc.

  • BTW, my typed DataSet is only a mirror of my database's structure, not it's data. Though I may load in _some_ data if I'm editing an existing entity.

Comments have been disabled for this content.