Question

I have a ASP.NET Application which normally can connect Database,can create users using CreateUserWizard but starting using profiles i stuck to below exception during user profile update:

[SqlException (0x80131904): Login failed for user 'My-PC\User'.]
[HttpException (0x80004005): Unable to connect to SQL Server database.]

I'm sure about below items:

  1. Connection string is ok and app can access db
  2. User are created using CreateUserWizard in database with no error if i don't want to update their profile

Only when I want to call this.SetPropertyValue("FirstName", value); everything go wrong and below error shown:

SQLExpress database file auto-creation error:

The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory. The provider attempted to automatically create the application services database because the provider determined that the database does not exist. The following configuration requirements are necessary to successfully check for existence of the application services database and automatically create the application services database:

If the application is running on either Windows 7 or Windows Server 2008R2, special configuration steps are necessary to enable automatic creation of the provider database. Additional information is available at: http://go.microsoft.com/fwlink/?LinkId=160102. If the application's App_Data directory does not already exist, the web server account must have read and write access to the application's directory. This is necessary because the web server account will automatically create the App_Data directory if it does not already exist.

If the application's App_Data directory already exists, the web server account only requires read and write access to the application's App_Data directory. This is necessary because the web server account will attempt to verify that the Sql Server Express database already exists within the application's App_Data directory. Revoking read access on the App_Data directory from the web server account will prevent the provider from correctly determining if the Sql Server Express database already exists. This will cause an error when the provider attempts to create a duplicate of an already existing database. Write access is required because the web server account's credentials are used when creating the new database.

Sql Server Express must be installed on the machine.
The process identity for the web server account must have a local user profile. See the readme document for details on how to create a local user profile for both machine and domain accounts.

Here is web.config:

<configuration>
  <connectionStrings>
    <add name="MUQ_SMSConnectionString" 
         connectionString="Data Source=MY-PC\SQLEXPRESS12SP1;Initial Catalog=MyDB;" 
         providerName="System.Data.SqlClient;Integrated Security=True"/>
  </connectionStrings>
  ............
</configuration>

Here is profile update method:

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    ProfileCommon userProfile = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);
    userProfile.FirstName = textBoxFirstName.Text.Trim();
    userProfile.LastName = textBoxLastName.Text.Trim();
    userProfile.MobileNumber = textBoxUserName.Text.Trim();

    if (radioButtonMaleGender.Checked == true)
        userProfile.Gender = MALE_GENDER_TAG;
    else if (radioButtonFemaleGender.Checked == true)
        userProfile.Gender = FEMALE_GENDER_TAG;
    else
        userProfile.Gender = UNKNOW_GENDER_TAG;

    userProfile.Save();
}

Here is the definition of profile and overriding of default provider:

<membership defaultProvider="MySqlProvider" >
      <providers>
        <add name="MySqlProvider"
             type="System.Web.Security.SqlMembershipProvider"
             applicationName="/"
             connectionStringName="MUQ_SMSConnectionString"
              requiresUniqueEmail="False"
             requiresQuestionAndAnswer="false" />
      </providers>
    </membership>
    <anonymousIdentification enabled="true" />

    <profile enabled="true">
      <properties>
        <add name="MobileNumber" type="string" allowAnonymous="true"/>
        <add name="FirstName" type="string" allowAnonymous="true"/>
        <add name="LastName" type="string" allowAnonymous="true"/>
        <add name="Gender" type="string" allowAnonymous="true"/>
      </properties>
    </profile>
    <roleManager enabled="true"/>

Note: I have another problem which I suspect that it may be related to this error. When I want to edit security options of app in Administration Panel of Application, SQL Server crashes and report for errors in windows.

Currently I'm using SQL Server 2012SP1 Express on Windows 8.1. I had SQL Server 2008 R2 Express and then installed 2012 Express but nothing changed.

Was it helpful?

Solution

When you use the AspDotNetProfile provider without providing a provider configuration in your web.config file, you are using the provider configuration in your machine.config file. Here's a sample from the default machine.config file distributed with .NET 4.0:

    <profile>
        <providers>
            <add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </providers>
    </profile>

The connection string that is referenced in this definition can also be found in machine.config:

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

It appears from your question that you haven't overrided the default profile definition in your web.config. So, what's happening is that the profile provider is looking for a SQL Server Express instance named SQLEXPRESS on the local machine. The reason is must be an Express instance is because User instances (see docs for AttachDBFilename) are only supported on the Express edition.

So, you have two choices: 1. Override the default provider configuration. this can be done by removing the default configuration and re-adding a configuration with the same name. 2. Set up a SQL Server Express instance with the default instance name on your web server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top