Wednesday, April 25, 2007

What to do when error like “ System.Data.SqlClient.SqlException: Failed to update database "C:\....\ASPNETDB.MDF" because the database is read-only"

What to do when error comes like “Exception Details: System.Data.SqlClient.SqlException: Failed to update database "C:\INETPUB\WWWROOT\....\APP_DATA\ASPNETDB.MDF" because the database is read-only”
or
“Exception Details: System.Data.SqlClient.SqlException: Failed to save to database "C:\INETPUB\WWWROOT\....\APP_DATA\ASPNETDB.MDF" because the database is read-only”

“Database is read-only” types of errors are common errors encountered when hosting the website on IIS with MS SQL Express database. These types of errors may occur when you have the NTFS file system and ASPNET user account does not have proper access permissions. If you do not provide write permissions to ASPNET user account to the hosted directory this error will occur. Simply add ASPNET user account to permission list to the directory and assign write permissions to this account. There are many ways to assign permissions and overcome this problem.

First do not host (place) your website to wwwroot directory. Either save it to the root of primary drive (C:\) or host it some where else because sometimes default access permissions do not provide ASPNET user account the write permissions. If you are hosting your website to wwwroot directory then make sure that ASPNET user account has write permission to App_Data Directory or .MDF file. You can check this by right clicking on App_Data directory and then selecting security tab. If you are able to see ASPNET user account in “Group or user names” then click on it and make sure that ‘write check box’ is checked (selected) in “permissions for ASPNET”. If it is not checked (selected) then check(select) it. If you do not find ASPNET user account into “Groups or user names” then add it and give it write permissions.

Second if you are hosting your website to any directory other than wwwroot then give ASPNET user account write permissions to website directory or App_Data directory as follow:

1. Right clicking on App_Data directory and then selecting security tab.
2. If you are able to see ASPNET user account in “Group or user names” then click on it and make sure that ‘write check box’ is checked (selected) in “permissions for ASPNET”. If it is not checked (selected) then check(select) it.
3. If you ado not find ASPNET user account into “Groups or user names” then click on ‘Add’.
4. In ‘Select Users or Groups’ windows click on ‘Advance’.
5. Now click on ‘Find Now’.
6. Here you will see the list of users. Now select ASPNET and click on ‘Ok’ to close third window and click again on ‘Ok’ to close second window (Select Users or Group).
7. Now click on ASPNET user account and check (select) write check box and click on ‘Apply’ and then ‘Ok’.

Now you have enough permission to your ASPNET user account to write or update Express Database. You can run your website now.

Third, you can place your website in shared folder but the access (write) permissions to ASPNET user account are required.

Thus you can overcome these types of errors.
If you still have problem, Please feel free to seek help.

To turn on the security tab in folder properties please follow the process:

. Click 'Tools' menu of explorer and select 'Folder Options'
. Select 'View' tab and uncheck (remove) the option 'Use simple file sharing (Recommended)' from the 'Advanced Options'. This option is the last in the list.

No comments: