How to fix the "The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is empty." error?
A

2

8

I have an ASP.NET project using SQL Server CE 4.0 with Entity Framework 4.0. It works fine on my local computer.

When I move it to the remote server, however, and try to log in to the admin part of the program, I get the following error:

The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is empty.

It references the following line in the machine.config on the remote server:

Line 237:    <membership>
Line 238:      <providers>
Line 239:        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
Line 240:      </providers>
Line 241:    </membership>

Now, it's true I don't have an "AspNetSqlMembershipProvider" statement in my web.config that references a connection string called "LocalSqlServer". Instead I have the following:

<membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider" connectionStringName="PUGConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>

This actually works locally. The name of the connection string is PUGConnection, not LocalSqlServer.

Why would this work locally, and not remotely? (The remote server is a shared server -- I don't have access to the machine.config.)

If I change the name of the connection string to LocalSqlServer, will it work? And if so, why would it work the other way on my local computer? The machine.config on my local computer looks the same as the one on the remote server, as far as I can tell.

Alkyne answered 26/7, 2012 at 21:59 Comment(6)
This is an annoying one. It's actually defined in machine.config so it is probably missing on the remote machine. Please see #5326407 and https://mcmap.net/q/1323014/-parser-error-message-the-connection-name-39-localsqlserver-39-was-not-found-in-the-applications-configuration-or-the-connection-string-is-empty for suggestions on how to resolve this.Irkutsk
It doesn't seem as if it was missing from the remote machine.config, since the error message specifically referenced lines 237 - 241. This is all very confusing. Whatever's in the machine.config, it expects to find an analog in the web.config?Alkyne
it simply might not be present in the remote machine.config. The Membership provider first looks in the gobal config (Machine.config) then looks in the local web.config, which is why either substituting it or clearing it works.Irkutsk
But if the error message specifically references lines 237 -241 of the machine.config, wouldn't that mean it was there?Alkyne
possible duplicate of Parser Error Message: The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is emptyMorton
@Alkyne - it is probably referencing the lines where the <membership/> element, without the expected child connection string, is.Irkutsk
J
17

You need to remove it from your config:

<remove name="AspNetSqlMembershipProvider" />

Or, better yet,

<clear />
Jaguarundi answered 26/7, 2012 at 22:2 Comment(2)
Thanks, that worked (I used the clear). But ... why did I have to do that for the remote server, but not locally? My local machine.config also had an entry for AspNetSqlMembershipProvider.Alkyne
I needed to this for the role provider. Odd that I've been a .net dev for 4 years and never needed to so far.Brutalize
S
4

You could also try to add the connection string to your config file. Even if it is not used, it should fix the errors that appear by referencing it elsewhere in the config.

<connectionStrings>
    <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

This is usually between the appSettings and system.data tags.

Shafer answered 1/6, 2015 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.