【问题标题】:asp.net getting error when trying to get or set profile properties尝试获取或设置配置文件属性时,asp.net 出现错误
【发布时间】:2012-11-16 13:50:02
【问题描述】:

我无法弄清楚为什么会收到此错误消息:

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)

我有一个简单的成员资格和角色管理器设置并正常工作,如下所示:

<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>

这很好,但错误发生在这里:

<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>

当我尝试从网站(例如母版页)访问这些属性时,我会收到上面的错误消息。这是我正在做的事情:

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

有人可以向我解释为什么我会收到此消息吗?

【问题讨论】:

    标签: asp.net web-config visual-studio-2012 profile sql-server-2012-express


    【解决方案1】:

    您需要为配置文件定义一个提供者,就像您为角色/成员所做的那样 像这样 -

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

    完整参考请看这里http://msdn.microsoft.com/en-us/library/system.web.profile.sqlprofileprovider.aspx

    【讨论】:

    • 答案总是比问题简单,对吧。我怎么会错过呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    相关资源
    最近更新 更多