Search code examples
c#asp.netsql-servervisual-studioprofile

SQL Profile giving server not found exception


I'm trying to use Profiles in my program. Creating the necessary tables using aspnet_regsql.exe from the command line worked fine. However, A System.Web.HttpException exception is thrown saying it is unable to connect to the database when I try to run the application. This is strange because I am connected to the database on the Server Explorer in VS 2013. Here is the error message on my application's website.

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Here's the relevant code in my web config file.

<profile defaultProvider="SqlProvider" inherits="[Namespace].AccountProfile">
    <providers>
        <clear/>
        <add name="SqlProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="[stringname]"/>
    </providers>
    <properties>
        <add name="Rows" type="System.String"/>
        <add name="Area" type="System.String"/>
    </properties>
</profile>

And here is the code in my application where the exception is thrown

AccountProfile.cs

public class AccountProfile : ProfileBase
{
      static public AccountProfile CurrentUser
      {
           get { return (AccountProfile)(ProfileBase.Create(Membership.GetUser().UserName)); }
      }

    ....
}

Solution

  • Turns out the Membership.GetUser() part of the code was still trying to use the LocalSqlServer connection string found in the machine.config file instead of the database connection string I provided in the web.config. Adding <clear /> in my connectionString configuration helped me figure this out.

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