Loading ...

An error has occurred while establishing a connection to the server. SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified | CodeAsp.Net

An error has occurred while establishing a connection to the server. SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified

 /5
0 (0votes)

Recently I faced a strange issue: while working on a sample project in ASP.NET 2.0 and VS 2005(which just had one form loading records from a single table from a SQL Server 2005 DB), I face this error as soon as I form started loading in the browser:

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)

My form simply connects to the relevant table in the DB and tries to get the records, but the app throws up this googly of an error as soon as it tries to connect to the DB. I checked the connection string 3-4 times, changed user name and password, but still the same error. A real googly for me!

Then while checking the code at a breakpoint, I noticed that the connection string had this value:

"data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"

I was stumped!! I did not install SQL Express and was not using the specified database mentioned in this "ghostly" connection string, all I had was a simple clean instance of SQL Server 2005 Enterprise edition!

I was sure this connection string was coming from some "higher" location, overriding my value, and then while searching google I came across this awesome post from Scott:

http://weblogs.asp.net/scottgu/archive/2006/11/20/common-gotcha-don-t-forget-to-clear-when-adding-providers.aspx

Though I was not using providers, using <clear/> fixed my issue! Here is the explanation:

By default ASP.NET 2.0 registers a set of default SQL Express providers within the root web.config file on your machine that create a SQL Express database within your /app_data directory to store/manage membership/role/profile data when you first access it.  Because this is registered at the machine-wide level, all provider collections by default inherit this registration.  Unless you explictly <clear/> or override the inherited value, your application will have this default membership/role/profile provider registered.

Because the above web.config file simply added a new provider -- and didn't clear or replace the default provider registration -- the above application now has two Membership providers configured.  When you do a Membership.CreateUser() call in your code, ASP.NET will attempt to create the user in both membership databases.  If you don't have SQL Express installed on your system, the create-user attempt will fail for this database - which leads to the errors and/or weird behavior above.


Now, I am not using Memership Providers, so I am confused as to why would this error occur in my case? Also, should not the application web.config override the machine.configs value?

Comments (no comments yet)

Top Posts