Tuesday, December 29, 2009

Nested web.config files and connectionStrings (entry has already been added)

When using the Entity Framework (EF) recently, I ran into a situation where I received the following error message:
The entry 'myConnectionstring' has already been added...web.config line: 47
After investigating I found out this was due to my use of web.config files in nested directories. Here is the situation...

I have a reusable EF DAL (data access layer) that is used by several web services on a single web site. Several of these web services are in sub-folders with their own web.config files. This allows me to have custom settings for each service as well as being able to easily xcopy an entire directory and know I have all the correct settings. For the EF I have a connectionStrings key in the config files. In addition to the nested web.config files, I have the top-level config which also has the EF connectionStrings key.

When I tried to access a web service I would get the error above because .NET was merging the web.config files and found duplicate connectionString keys. To fix this I just had to add the following element within the connectionStrings section, directly before my key:
<remove name="myConnectionString" />
To facilitate knowing I can easily xcopy any web service, it is good practice to add this to all the web.config files, even the top-level one.

Sunday, December 27, 2009

XDocument / XElement - Save using a StringWriter as UTF-8 rather than UTF-16

One thing I find myself having to do on frequent projects is to save XML to a text file. I tend to use the .NET XDocument or XElement classes and use the Save() method. Once and a while I need to call Save() using a TextWriter. By default, when I do this the XML is written using a UTF-16 processing instruction as:

<?xml version="1.0" encoding="utf-16"?>

More often than not you want UTF-8; .NET XElement won't even read the UTF-16 contents back in using the Load() method (and things like IE won't be able to view it in the browser). To work around this problem I derive a new StringWriter and override the Encoding method:

public class StringWriterWithEncoding : StringWriter
{
private Encoding mEncoding;
public StringWriterWithEncoding(Encoding encoding)
{
mEncoding = encoding;
}

public override Encoding Encoding
{
get { return mEncoding; }
}
}

Now just use the new class and the XElement.Save() method to write as UTF-8:

XElement xml = XElement.Parse("<root/>");
using (StringWriterWithEncoding writer = new StringWriterWithEncoding(Encoding.UTF8))
{
xml.Save(writer);
Console.WriteLine(writer.ToString());
}

Wednesday, December 16, 2009

The row value(s) updated or deleted either do not make the row unique or they alter multiple rows

I was working with a Microsoft SQL database today (not mine thank goodness) that had duplicate rows; the table had no primary key defined. When I went to go delete the duplicate row (or even change it), I got the error:

The row value(s) updated or deleted either do not make the row unique or they alter multiple rows(2 rows)

It was somewhat hard to figure out how to delete the duplicate row. In the end, I was able to delete it by doing:

SET ROWCOUNT 1
DELETE FROM myTable WHERE statmentToSelectTheDuplicateRow

Another reason why to always define primary keys so you don't even get into this situation.

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...