Recently playing around the Membership feature of ASP.NET, and trying to follow the sample article on MSDN(“Walkthrough: Creating a Web Site with Membership and User Login“). But I got error message when I launch the ASP.net Web Site Administration Tool, which occurred on the Security tab as the below:
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 went ahead to click the Provider tab, and then click the Test link of AspNetSqlProvider under the Provider section. But I got 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 understand that this error was caused by the database connection. First I had to make sure I have already installed the aspnetdb in my local MS SQL server. I closed the Web Site Administration Tool, and launched a DOS command prompt. Next I ran “aspnet_regsql” under the directory of “C:\Windows\Microsoft.net\Framework\v4.0.30309\” which launched a MS SQL connectionwizard. After I was done with that wizaard, I then verified in my MS SQL 2008 R2 server. I found there was a new database instance called “aspnetdb” created and have some tables like “dbo.aspnet_Applications”, “dbo.aspnet_Users” etc.
But when I re-launched the ASP.NET Web Site Administration Tool, I got the same error message as I showed above.
I concluded that it was still caused by the Connection String. But I could not find any connection string or membership provider information in my application’s web.config file. I then checked the machine.config file on my development computer, I found the connection string read like the below:
<connectionStrings> <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/> </connectionStrings>
From the connection string above, I believe the asp.net is trying to look up membership information from the aspnetdb.mdf file under App_Data directory. But I could not find this file under the App_Data folder of my application. According to the Walk Through article on MSDN, the Web Site Administration Tool site should be able generate an aspnetdb.mdf for me if I finish the Security Wizard. But how can I run the Security Wizard at the first place?
Since I already created the aspnetdb already, and wanted to continue with the other steps on the Walk Through article, I decided to fix this connection string and move one.
Open the web.config file under the current application root folder, and put the following inside it:
<connectionStrings> <remove name="LocalSqlServer" /> <add connectionString="data source=.;Initial Catalog=aspnetdb;Integrated Security=True;" name="LocalSqlServer" providerName="System.Data.SqlClient" /> </connectionStrings>
Now when I test the aspnetsqlprovider on the same application’s Web Site Administration Tool, it ran successfully.
But I still could not have the aspnetdb.mdf file generated automatically. Maybe I need to play around more to get it created next time.Tags: asp.net, membership