Monday, January 26, 2009

Another one!

I started getting a very unexpected error today in the application I’m working on. I did a quick search on google and resorted to asking a question on the StackOverflow website

The key ‘UserID’ does not exist in the appSettings configuration section.

Basically, I started getting this error while trying to open 2 of some 10+ forms in my Window Forms application in designer.

To prevent possible data loss before loading the designer, the following errors must be 
resolved:
The key 'UserID' does not exist in the appSettings configuration section.

While waiting for a kind soul to reply to my question, I continued searching through Call Stacks, trying to find the cause of a problem. And I found it eventually. What the VS designer was really complaining about, was the fact that I was calling a stored procedure from the user control’s InitializeComponent.

Well, I have a couple of comboboxes on the user control which a populated from the database, so I decided to choose the simplest way to make sure they were populated by the time the user control was shown. Looks like it was not such a good idea. I ended up moving the calls into a separate function, and calling that function from a hosting form after the user control is shown in the hosting form. This appears to work for me. Took me a whopping 58 minutes to fix an error I introduced by being careless and ignorant, hope it’s not project-breaking thing.

by . Also posted on my website

Tuesday, January 20, 2009

Changed appearance

Inspired by this blog entry I found

Using SyntaxHighlighter on BLOGGER

I spent some time updating my blog and posts to use the SyntaxHighlighter. I think it looks a little better now, so I'll keep it this way.

by . Also posted on my website

Sunday, January 11, 2009

My blog title is proved once again.

A small application I’m working on at the moment needs to present the user some colours and the ability to change them and save changes. So, I decided to use a DataGridView with a custom colour picker column (that’s a separate story I think). The colours are stored in the database table as integers in ARGB format. So, to present them, I need to convert them to System.Drawing.Color.

I have a piece of code like that

public DataTable GetColours()
{
// snip
DataTable colors = new DataTable();
// snip
colors.Columns.Add(MyColourTypes.BackGroundColour.ToString());
// snip
DataRow newRow = colors.NewRow();
int bgColour;
if (int.TryParse(iMyColourValue.ToString(), out bgColor))
{
newRow[MyColourTypes.BackGroundColour.ToString()] = Color.FromArgb(bgColor);
}
else
{
newRow[MyColourTypes.BackGroundColour.ToString()] = Color.Empty;
}
}

So if a value in the database is int, I convert it to Color and add to DataTable, otherwise I put Color.Empty in the DataTable (Colours can be nulls).

However, my DataGridView complained about the invalid cast exception on the line

Color lclcolor = (Color)value;

Where value is the entry from my DataTable.

So I thought that the colours in the database may be not “known colours” and spent at least an hour searching for a “proper” way to convert a colour from integer to System.Drawing.Color. It took a while till I noticed in the watch window that even when the value is “Color[Black]” the debugger refuses to unbox it as a System.Drawing.Color. Further investigation proved, that value is indeed not a Color structure, but more looks like a string. Another minute of intense thinking, and I replace

colors.Columns.Add(MyColourTypes.BackGroundColour.ToString());

with

colors.Columns.Add(MyColourTypes.BackGroundColour.ToString(), typeof(Color));

A rainbow of colours appear in my DataGridView.

Moral: when you want to store something more complex than a string or an int in a DataTable, don’t forget to tell the table what type of object you want to store.

Good thing: I learned a few things about converting colours between different format