0
votes

I cannot connect to SQL Server database.

When I run the aspnet_regsql.exe utility, I get the following error message:

Setup failed.

Exception:
Unable to connect to SQL Server database.

Details of failure

System.Web.HttpException: Unable to connect to SQL Server database. ---> System.Data.SqlClient.SqlException: 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Also, I am unable to use the ASP.NET Website Administration Tool. When I click on the Security tab I receive the following error:

There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient permission. It can also be caused by the role manager feature not being enabled. Click the button below to be redirected to a page where you can choose a new data store.

The following message may help in diagnosing the problem: Unable to connect to SQL Server database.

So, I click on the 'Choose Data Store' button as instructed, which allows me to select a a Provider. The provider is named 'AspNetSqlProvider', it has a Test link beside it. I click on Test and receive the following error message:

Could not establish a connection to the database.
If you have not yet created the SQL Server database, exit the Web Site Administration tool, use the aspnet_regsql command-line utility to create and configure the database, and then return to this tool to set the provider.

I have lost overtime for this problem, but i cannot fix it. I hope everybody can help me. I look forward receive the good answer as soon as possible. Sincerely thanking you very much for your help.

1
What server (and instance) name are you trying to use for your connection? It seems that server that you think exists really doesn't exist....marc_s
Are you developing remotely?Ross Bush
Server name is HOANGLINH\SQLEXPRESS or .\SQLEXPRESSNguyen Ngoc Hoang
@lrb: Remote connections are allowed.Nguyen Ngoc Hoang

1 Answers

0
votes

I encounter the same problem in my ASP.NET MVC project as you, when using the ASP.NET Website Administration Tool. The solution is to check and modify your web.config file. I just change the following attributes (add a field):

1) from

<profile>
  <providers>...</providers>

to

<profile defaultProvider="DefaultProfileProvider">
  <providers>...</providers>

2) from

<membership>
  <providers>...</providers>

to

<membership defaultProvider="DefaultMembershipProvider">
  <providers>...</providers>

3) from

<roleManager>
  <providers>...</providers>

to

<roleManager defaultProvider="DefaultRoleProvider">
  <providers>...</providers>

And, be sure to have something like:

 <connectionStrings>
   <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication1-20160111151822;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApplication1-20160111151822.mdf" />
 </connectionStrings>

and the "DefaultConnection" has a corresponding field connectionStringName="DefaultConnection" under <profile>,<memebership>and <roleManager> tags, which are modified as stated above.

If you still have problems in your MVC project, just create a new full ASP.NET MVC Web Application template, compare the web.config file with yours, and modify your web.config file.