Category Archives: Development

Snippet Compiler

I’ve found this little tool invaluable. I use it to test out RegEx expressions, String Formatting and tons of other useful little bits of code.

I used to keep a dummy project around for this purpose, but it’s a pain to load VS.NET every time that you want to test a quick string formatting and it’s too painful to test to that location in your real application just to see if it worked and see what it does.

I hope that you enjoy it as much as I do.

More adventures with SQL Server Express

This was fun. It’s a completely different database and set of circumstances than last night’s fun and frivolity.

I finished an application, zipped up the folders and sent it off to a client. In this application there is a desktop app that talks to a web service that talks to a SQL Server 2005 Express database. Sounds fairly simple, right? Right.

Well, the client unzips it all to a directory, sets up the virtual application for the web service and starts trying to run the desktop portion. It starts up, pulls up all of the lookups that we have in the database and it all looks good. Now, I’m on the phone and elated at how smooth this is going. At this point, the client tries to create a new record which is going well (wow, even a little drop down calendar and oooh look at that, it’s all of the types of records that I could create so I don’t have to type there… wow… and more gushing) until they try to save… At this point, there is a wonderful error that pops up –

“Failed to update database ‘database name.mdf’ because the database is read-only.”

Crud! After getting them to check the file properties and other such things, I called Patrick Steele, who has been working with me on this project, to see what he thought about it. It’s been a long day, especially after last night’s SQL Server Express issues, so I’m not thinking all that clearly. Patrick, after running through some of the same questions that I had so I’m feeling a little better about my thinking, has the thought that it might be a security issue and that the ASPNET user might not have permissions to write to that directory. Sure enough, since the ASPNET user doesn’t have permissions the database loads in read-only mode. Fix that and the database and the app work just fine.

SQL Server 2005 Express Issues

Yesterday I had to make a quick change to a SQL Server 2005 Express database that I’m using for an internal to SRT Solutions tool. I decided that the easiest way to do it was to leverage the auto-connect feature of SQL Server 2005 Express by just FTPing the mdb down, making my change and ftping it back up. This has worked a number of times in the past. Not this time. I got a “login failed for user ‘USERNAME'” when I tried to run the application. That was infuriating. I spent the next 4 hours or so working with the SQL Server Management Studio Express (Enterprise Manager lite for Express) assigning, unassigning permissions, users, attaching, detaching, backing up, restoring and everything else that I knew to do. I kept getting “User could not login to user’s default database” and general login failures. My personal favorite that I saw yesterday was “Could not attach, DatabaseName is not a primary database file. (Microsoft SQL Server, Error: 5171)”. Now, I’m usually pretty good with googling the answers, but this time I’m really coming up short.

All of this is proving to me that I’m a programmer, not an administrator. I’m digging the new functionality in SQL Server 2005 but I’m not going to be the one to administer the databases that I write code for.

Long story short – I raised the white flag. I IMed Jason Follas and asked him if he knew what was going on. He figured out that it had something to do with the master file that SQL Express was using for the auto-connect. He found this forum topic. Here’s the relevant snippet:

“When you first run SSE this way, it makes a local copy of master etc. in C:\Documents and Settings\<your user account name>\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS.  That’s why it’s so slow the very first time it loads under a new user profile.  It may even timeout the first time you ever use it under a given profile.

Based on your description, it’s very possible something went bad in those files for some reason.  So, I would first make a backup and then delete the SQLExpress directory under C:\Documents and Settings\<your user account name>\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data.  That will get rid of the local copy of master, etc.  Then, reboot. ”

Well – Lance (the author of the post and author of SQL Server 2005 Express Edition Starter Kit) was absolutely right. I killed the master files (after copious backups) and rebooted the machine. It’s working like a champ now. On reboot, it regenerated the master files needed and started right up.

Thanks Lance and Jason

4 Things Meme

Jim, I will get
you back for
tagging me…


Four Jobs I’ve Had…

Bailing Hay (Grew
up on a farm in Arkansas)


Morning radio show


movies I can watch over and over…



It’s a Wonderful

Rosencrantz &
Guildenstern are Dead

Four TV
shows I love to watch…  (Disclaimer – similar to Jim’s, I watch a lot of TV with my



Danny Phantom

English Premier
League Soccer

places I’ve been on vacation…

, AR



Philadelphia, PA

favorite dishes

Biscuits and

Ham and Cheese

Argentinean Steaks
– they cook them different there. It’s tender and melts in your

Any meal with my

websites I visit daily:




·         (well at least one of
the blogs under there…)

places I’d rather be

Anywhere with my

Anywhere on my


Skiing – I’ve never
been but I’ve wanted to for a while

bloggers I’m tagging





log4net on the Compact Framework

I was reminded of log4net today when working on a project. The client asked a logging framework and the cool part is that it’s a Compact Framework application. Sure enough log4net supports the Compact Framework.

There are a couple of minor differences that you should be aware of if you are interested in using log4net on the Compact Framework. First of all, not all of the appenders make sense – like the RemotingAppender, the EventLogAppender and the OracleAppender. Secondly, there is no mechanism for retrieving assembly level attributes so you have to explicitly initialize and shut down the log4net engine.


static void Main()




            //This line configures the log4net engine.


new FileInfo(@”\log4net.config”));


            frmLogin loginForm = new frmLogin ();


                  Application.Run(new frmMain());



      catch (System.Exception e)


            _log.Error(“Main“, e);




            //This line shuts down the log4net engine





The other thing that was interesting is that the online help on showed conversion patterns with %message%newline to print the message and then put a newline in the file. I had to use %m%n because the first one there printed as


My stringessage



which was not quite what was expected.


Have fun with log4net, it’s a great lightweight logging framework for the compact framework.

Databinding on the Compact Framework

Through careful use of databinding, your UI code can be very light weight.

There are two forms of databinding that we need to discuss, binding to properties of objects and binding to a list of objects – which often binds to properties of the individual objects.

First, let’s deal with the less talked about binding to properties. The code to setup a binding to a particular property on an object is fairly simple. The following snippet binds the text property of the _txtName textbox to the name of a person.


_txtName.DataBindings.Add(“Text”, person, “name”);


This assumes several things. First of all, it assumes that the TextBox has a Text property. Second, it assumes that the person is not null and lastly, that the person has a valid property called name. Once you work through those assumptions, the TextBox in question will not only show but allow you to edit the person’s name with that one line of code. If, however, instead of a person object, you have a table with rows of people, you will bind as follows.


_txtName.DataBindings.Add(“Text”, _dataSet.Tables[“person”], “name”);


As a quick note, on the full framework, I would bind the text as follows.


_txtName.DataBindings.Add(“Text”, _dataSet, “”);


However, this is a shortcoming of the Compact Framework in that it is not able to bind to expressions like “”.


Back to the original thought, as this is a simple textbox, this is only able to bind to one row at a time so it’s going to enlist the BindingManagerBase for the form to pick which row is bound. The BindingManagerBase controls the databinding for a particular object for all of the bound sub-controls of the control from which it was returned. Most often, it is used to control the bindings for an entire form, as I’ve done in the sample, but it can also be used on a particular Tab or Panel. To get the BindingManagerBase, use the BindingContext for the container in question to retrieve it as follows.


BindingManagerBase _bindingManager = null;

_bindingManager = this.BindingContext[_dataSet.Tables[“person”]];


At this point, you can use the BindingManagerBase to control the position in the list and thus the current row bound by the TextBox by setting its Position or to monitor changes to the object by subscribing to the CurrentChanged event.


BindingManagerBase _bindingManager = null;

private void Bind()


            _bindingManager = this.BindingContext[_dataSet.Tables[“person”]];

            _bindingManager.PositionChanged +=

                      new EventHandler(_bindingManager_PositionChanged);

_bindingManager.CurrentChanged +=

         new EventHandler(_bindingManager_CurrentChanged);


            _txtName.DataBindings.Add(“Text”, _dataSet.Tables[“person”], “name”);

            _txtEmail.DataBindings.Add(“Text”, _dataSet.Tables[“person”], “email”);


private void _btnNext_Click(object sender, System.EventArgs e)




private void _btnPrevious_Click(object sender, System.EventArgs e)





This will allow you to cycle through the list easily.


The second form of databinding, binding to lists, is even easier. The code is as follows.


private void BindList()


            _lstMain.DataSource = _dataSet.Tables[“person”];

            _lstMain.DisplayMember = “name”;



Setting the DataSource on the list requires that the list (or table in this sample) implements IList or IListSource, which the DataTable does. Setting the DisplayMember is similar to the binding of the properties that we did earlier in that it will show the name column or property on the person object or row in the list.


There are some benefits to combining these methods of databinding. For example, if you bind the table to the list and bind the textbox to the same table – the listbox will automatically keep track of the positioning of the BindingManagerBase.


In conclusion, there is very little need to write a lot of UI code anymore. Instead, leverage databinding to your advantage to keep your UI lightweight and responsive. 


For a full code listing download the sample code below.