Search code examples
asp.netweb-configvisual-studio-2012profilesql-server-2012-express

asp.net getting error when trying to get or set profile properties


I'm having trouble figuring out why I'm getting this error message:

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)

I have a simple membership and rolemanager set up and working properly like this:

<connectionStrings>
  <add name="GustaafConnectionString" connectionString="Data Source=ROBBIE-PC\PHL;Initial Catalog=Gustaaf;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

<authentication mode="Forms">
  <forms timeout="1440" protection="All" slidingExpiration="true"/>
</authentication>
<anonymousIdentification enabled="true"/>

<roleManager enabled="true" defaultProvider="RoleProvider">
  <providers>
    <add connectionStringName="GustaafConnectionString" applicationName="Gustaaf" name="RoleProvider" type="System.Web.Security.SqlRoleProvider"/>
  </providers>
</roleManager>

<membership defaultProvider="MembershipProvider">
  <providers>
    <clear/>
    <add name="MembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="GustaafConnectionString" 
         applicationName="Gustaaf" enablePasswordRetrieval="true" enablePasswordReset="false" requiresQuestionAndAnswer="true" 
         requiresUniqueEmail="false" passwordFormat="Encrypted"/>
  </providers>
</membership>

That's great and all, but the error happens here:

<profile>
  <properties>
    <add
      name="numberOfVisits"
      type="Int32"
      defaultValue="0"
      allowAnonymous="true" />
    <group name="Address">
      <add name="City"
           defaultValue="NA"/>
      <add name="PostalCode" 
           type="Int32"
           defaultValue="0"/>
      <add name="Street"
           defaultValue="NA" />
      <add name="Number" 
           type="Int32"
           defaultValue="0" />
    </group>
    <add name="PhoneNumber"
         defaultValue="NA"/>
    <add name="DateOfBirth" type="DateTime"
         defaultValue="GetDate()"/>
  </properties>
</profile>

As soon as I try to access these properties from a website like for example the masterpage, I get the error message above. Here's what I'm doing:

protected void Page_PreRender()
{
    if (Profile.IsAnonymous)
    {
        Profile.numberOfVisits++;
    }
}

Could someone explain to me why I'm getting this message?


Solution

  • You need to define a provider for the profile like you have done for Roles/Membership so something like this -

    <profile defaultProvider="SqlProvider">
        <providers>
        <clear/>
        <add name="SqlProvider"
            type="System.Web.Profile.SqlProfileProvider"
            connectionStringName="GustaafConnectionString"
            applicationName="Gustaaf" />
        </providers>
        <properties>
            <!-- Properties Here -->
        </properties>
    </profile>
    

    See here for full reference http://msdn.microsoft.com/en-us/library/system.web.profile.sqlprofileprovider.aspx