Embed any File in a .Net Assembly

This is probably old news; I’m posting it here so I can find it again quickly! Here is a simple way to embed any file in a .Net assembly:

Add a file item to an existing project, go to the file’s properties and change the Build Action to ‘Embedded Resource’

Add the following C# code and you’re up and running. I’ve used this technique to embed parameterised default templates into a .Net executable.

///

/// Reads an embedded file from the executing assembly’s resource and returns it as a string.

///

/// Embedded Resource Filename

/// Namespace of the enclosing assembly

/// Embedded resource as string

public string GetEmbeddedResourceFile(string filename, string rootNamespace)

{

string embeddedResource;

Stream strm = null;

Assembly ass = Assembly.GetExecutingAssembly();

if (!rootNamespace.EndsWith(“.”))

rootNamespace = rootNamespace + “.”;

try

{

using (strm = ass.GetManifestResourceStream(rootNamespace + filename))

{

byte[] buffer = new byte[strm.Length – 1];

strm.Read(buffer, 0, buffer.Length);

embeddedResource = System.Text.ASCIIEncoding.ASCII.GetString(buffer);

}

}

catch

{

// Note: It is generally bad practice to consume all exceptions!

// If any error errors, simply return an empty string

embeddedResource = String.Empty;

}

return embeddedResource;

}

An error has occurred…

I recently got bitten by a rather annoying and hard to track down error. I was in the process of creating a fairly simple ASP.NET 2.0 application which accesses a SQL Server 2005 database. It worked at home but not in the office…

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified)

After doing a few Google searches, it turns out this can be caused by lots of things, and there are quite a few similar error messages for many different reasons. The most often cited one is due to SQL Server 2005 not accepting remote connections by default (which personally I think is ridiculous, it’s a server right, the whole reason for having servers is to allow access by remote clients! But I digress..)

So I fired up the Surface Area Tool 2005 and checked that remote connections were allowed. No problems there.

Another oft cited reason for this error is that a Specific Protocol is not enabled. So I opened up SQL Server Configuration Manager and checked the Client Protocols. All fine.

Of course! It’s probably the firewall. Checked it and in desperation temporarily turned it off. Still no good.

Checked the connection string worked in isolation. Once again nothing wrong there.

After a great deal of hair-pulling and searching I finally found the problem. The ASP.NET role provider was trying to access the role table via a connection string named “LocalSQLServer” defined in the machine.config, and this was pointing to “.\SQLEXPRESS” which was not installed!

The simple fix is to override in your web.config:

<connectionStrings>

<remove name=”LocalSqlServer” />

<add name=”LocalSqlServer” connectionString=”Data Source={local};Initial Catalog=myDB;Trusted_Connection=True” providerName=”System.Data.SqlClient”/>

</connectionStrings>

I had installed several of the ASP.NET starter kits and doing so had updated the machine.config. The reason it worked at home was because an application had installed SQLExpress without me realising! These links helped me track down the problem:

http://weblogs.asp.net/scottgu/archive/2005/08/25/423703.aspx#453222
http://www.aquesthosting.com/HowTo/Sql2005/SQLError26.aspx

Introduction to Test-Driven Development: Updated Link

Rob Farley pointed out that the link to the MSDN Architecture Webcast: Test-Driven Development Using Visual Studio Team System (Level 200) in my previous post was broken. It must have expired within days of me posting it! I’ve updated that post and this one to point to the correct location (I think you will have to have a Passport login to access it). If you can find a spare hour, it is well worth watching. Thanks Rob.

Microsoft Certification Exam 70-441 Status Change

The title says it all! I received confirmation yesterday that I had passed Exam 70-441: PRO: Designing Database Solutions by Using Microsoft SQL Server 2005, which means I’ve completed MCITP: Database Developer in addition to MCITP: Database Admin. Might have to look into the requirements for Analysis Services…

Gemini: A Free and Easy to Use Issue Tracking System

Following on from my previous post Software Development Must Haves, thought I would mention Gemini, a free and easy to use Issue Tracking System. I’ve successfully implemented it at 4 different companies and mentioned it on the ausdotnet mailing list a few times.

Several years ago when I was looking at what systems were available, I did a comparison of the free and not-so-free offerings. For ease of use and bang-for-buck, I think that Gemini is by far the best. Gemini has a free license for up to 10 users, and is under AU$500.00 for a commercial license.

It is web based (but also provides a Windows client), supports a customisable SQL Server backend, web service interface and a utility for importing data. With a little work it can be integrated with your Source Code Control system.

If you are developing software and you are not using an Issue Management System, I strongly suggest you check out Gemini.