Scott Hanselman

Fixing Instance Failure when connecting to SQL Server 2005 Express

August 31, '07 Comments [8] Posted in ASP.NET | Learning .NET | Programming
Sponsored By

I was getting Instance Failure when connecting to my SQL Server Express 2005 Database in my C# ASP.NET application.  Very weird.

Instance failure. - Windows Internet Explorer

<connectionStrings>
    <add name="NorthwindConnString" 
connectionString="Data Source=.\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient"/> </connectionStrings>

Trial and error just taught me that the problem was "Data Source=.\\SQLEXPRESS;" - It's the double \\. That's an escape sequence in C#. Everything worked when I switched the connection string to .\SQLEXPRESS.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Friday, August 31, 2007 2:34:37 AM UTC
That was probably the cause of the problem, but I'm not sure it's related to \\ being an escape sequence in C#. If it were being taken as C# code, wouldn't ".\\SQLEXPRESS" translate to ".\SQLEXPRESS" and ".\SQLEXPRESS" translate to ".SQLEXPRESS"? I don't think C# escape sequences apply to strings in a configuration file.
Friday, August 31, 2007 4:50:16 AM UTC
Jose, I'm starting to think you're right, but interesting it works with \\ under VB...Hm...what is going on?
Friday, August 31, 2007 4:50:19 AM UTC
The config file is an XML file which has nothing to do with C#. Thus in XML the attribute connection string will still be .\\SQLEXPRESS and is invalid.
Friday, August 31, 2007 4:51:20 AM UTC
What happens is : the string is read in c# and is escaped so \\ becomes \\\\. And I know that the hard way :((.
Took 2 hours once to find the stupid thing.
sirrocco
Friday, August 31, 2007 5:11:07 AM UTC
Brett: Ah, so you're saying it was always wrong?

Sirrocco: See, that makes more sense to me.
Friday, August 31, 2007 5:40:42 AM UTC
Yes, in a .config file or XML file it is always wrong and as Sirrocco said the .\\SQLEXPRESS would be read in as .\\\\SQLEXPRESS. However, if you had hard coded the connection string into C# code, in order for C# to interpret it, you would have had to do some thing like:
string connectionString = "Data Sorce=.\\SQLExpress";

What I am basically getting to, the config file (aka XML) is read in using an XML parser where \ is not a escape character thus will be treated litterally. i.e. \ = \.

In the C# source code (note I said source code) above, the C# parser will treat \ as an escape character, thus \\ is read in as \.

To answer your question, yes for a config file it is always wrong as it is XML. However for C# source code it is correct.
Friday, August 31, 2007 12:11:19 PM UTC
It alos seems like the ADO.NET API could give you a better exception message than "instance failure".
Kris
Friday, October 12, 2007 3:01:52 PM UTC
Thank you! You saved me from a bad headache. :) I had copy-pasted the code from a C# area of my program and had forgotten that the double backslash was for the benefit of C#'s string escapes and that XML didn't need it. Thank you!!!

Utah Custom Software Consulting
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.