Failed to generate a user instance of SQL Server - asp.net

I been trying to get my Visual Studio 2008 to run my Web Application on IIS7 on my Windows 7 machine but I just can't get it to work. I'm using the SQLEXPRESS I been following this Tutorial, but I get the following error message.
"Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path. Please make sure the user has a local user profile on the computer. The connection will be closed."
my connectionString seem to be ok
<add name="testConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|test.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
The site run with out database connection.
Any ideas and thoughts about this?
Thanks guys

Make sure the instance name is called "SQLEXPRESS" exactly.
Make sure the "test.mdf" file exists in the "App_Data" folder.
When you are adding the DB using Server Explorer, use "Test Connection" first.

In IIS7,
they use default asp.net database.
create a new application pool instead of using default.
and keep your database in that.
go to sql configuration manager and in the properties,select local system instead of build in system.
Now,it may work.

Related

SQL server database tables are not accessed

I have created a web-service using asp.NET with SQL Server 2005 database. Then I published it to local IIS, but when I publish it Visual Studio showed me an error stated that:
the process cannot access the file (path to database) because it is being used by another process.
so, I Detach the database. then I published the web-service. Now, when Browsing my web-service from IIS Manager, and try to test some methods, an error page appears to say:
System.Data.SqlClient.SqlException: Invalid object name 'Student'.
where Student is a table in my database.
my connectionString is:
<add name="sbms" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
Does Detaching the database cause this error? or there are errors in my ConnectionString? I search in Google and didn't find solution.
Please help me..
Detaching the database will cause the second error. The SqlClient can't find the table referenced by your code.
The first error comes from the database being opened in SQL Server in single user mode. Re-attach the database to SQL Server. Then change the connection settings to multiuser.
Detailed instructions for setting either single or multiple users

Azure site only works when running locally with remote database

I have a site that's running off windows azure and I have hooked up the database via a linked resource and it looks like the site should be using the database. I've also included the proper connection string in my web.config file.
When I run the website locally and use the azure database's connection string for my default connection, everything works and I've seen that updating data from my local machine is reflected in the remote database.
Whenever I try to access any page that makes database hits (ie, logging in or looking at the basic index (from scaffolded out views)), I get a 500 error. I tried turning on custom errors but the 500 error is all I get. I tried to debug it from my local machine but that didn't help at all since everything worked properly when running the site locally and connecting with the azure database.
I have also pulled down the web.config files and the web.configs on both sides are identical.
I figure that this has to do with a configuration issue, but I'm not sure what.
Is there maybe something I'd have to do with asp.net mvc 5 to make it work with windows azure? It looks like the .net framework is properly set on azure to .net 4.5. I'm guessing it has something to do with the fact that Azure just doesn't know it should be using the database supplied in the web.config.
Here are the web.config connection strings:
<add name="DefaultConnection" connectionString="Data Source=####.database.windows.net,1433;Initial Catalog=####_db;Persist Security Info=True;User ID=####;Password=####" providerName="System.Data.SqlClient" />
<add name="DefaultConnectionDeploy" connectionString="Data Source=####.database.windows.net;Initial Catalog=####_db;Persist Security Info=True;User ID=####;Password=####" providerName="System.Data.SqlClient" />
<add name="DefaultConnection_DatabasePublish" connectionString="Data Source=####.database.windows.net,1433;Initial Catalog=####_db;Persist Security Info=True;User ID=####;Password=####" providerName="System.Data.SqlClient" />
Windows Azure makes use of transforms to manage connection strings. The connection strings are merged in at runtime, and can be controlled from the configure tab on the Web Site management page. whenever you set up an Azure Database as a linked resource, it automatically configures the Azure Database server with firewall rules to allow connection from the web server, and adds a connection string. By using this connection string, naming it the same as the string used in development, it is not necessary to modify any code prior to deployment. If you don't set the Azure Database up as a linked resource, it is necessary to modify the firewall rules by hand, which doesn't play nicely with the scalability of Azure Websites, so it's not recommended.
As per Andrew-counts' suggestion, I went and checked out my EF migrations (after pulling down the Azure web.config file using the "Replace web.config from server" option so that I had the exact copy that the server is using. I then enabled custom errors. It turns out that the pages were failing to load because of an error that stated that the table dbo.AspNetRoles already exists.
It appeared as though it was trying to run the EF migrations despite the fact that the tables already existed. To remedy this, I decided to go and wipe out my current database (I had just setup the database today so no harm doing that) and then, from the package console in visual studio, ran the Update-Database script before deploying anything to the server. When I did that, it correctly added the records inside of the dbo.__MigrationHistory table. I then checked in my changes so that the visualstudio.com build I have connected to my solution would build and deploy to Azure. The problem persisted, but what I realized was that I need to disable the automatic build + deploy that I was using and instead use the right click + publish, since doing it from the visual studio build wasn't connecting to the azure database properly (it wasn't transforming the web.config). When I used the publish from within visual studio, the site worked like a charm.
Thank you for all your help

MVC3 connection string with Entity Framework

As I have been following a MusicStore tutorial, new MVC3 project created a following connection string for me:
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
My model tables got created in .\SQLEXPRESS and all the users/roles tables are stored in the aspnetdb.mdf in the App_Data folder.
I would like to have the model tables and user/roles tables on my .\SQLSERVER or localhost rather then scattered between .\SQLEXPRESS and the mdf file. I'm struggling with the right connection string. Can anyone suggest the right one please?
Everyone who does this has to make sure that name of the connection string should be the name of the context class derived from the *DbContext*
It sounds like you need to run aspnet_regsql on your ApplicationServices database. You can find it in C:\Windows\Microsoft.Net\Framework\4.xxx\aspnet_regsql.exe.
A default project uses ApplicationServices db as default for membership, roles, and profile. The sproc dbo.aspnet_CheckSchemaVersion is a sproc in the ASP.NET SQL Membership Provider schema.
Response to comment #1
Do you have a database instance running on your machine named SQLSERVER? The exception you mentioned is a common one, if asp.net cannot find the database using your connection string.
Response to comment #2
That makes sense. Integrated Security=true tells the connection to use the process running the MVC app (usually IIS app pool) for the sql server login. If you are testing from Visual Studio / Web Developer Express / etc, the web server is likely running on a process with your windows account. So if you can windows auth against the db server, MVC should also be able to.
User Instances are meant for SQLEXPRESS.

How to copy local Membership and Role database to remote MS SQL Database

I have made a MVC application that uses the built-in ASP.NET login functions. It works perfectly on my local machine. I have bought a webhosting service, because i want to publish my website to the internet.
How do i copy the membership and role database / tables to my new MS SQL Database and make it work with my current project?
In my Web.config i have:
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
notice that
Instance=true
This means that SQL Server Express will open the aspnetdb.mdf file, creating it on the fly first if needed.
When i try to login on my website i get the following error:
Sorry, an error occurred while processing your request.
Thanks
You need to run aspnet_regsql located in C:\Windows\Microsoft.NET\Framework\v4.0.30319. Just point it to your remote sql server and it should take care of the rest.
You will also need to edit your connection string for application services to the one for your remote sql server.

Database in App_Data folder -> Unable to open the physical file "xyz.mdf"

I am creating a package of an asp.net mvc3 application to distribute it. Means I want to zip it and send it to someone for review. I want that you just start the solution in VS and it runs. So no need for changes in web.config.
I was using sql server with hard-coded DataSource. (COMPUTERNAME\SQLEXPRESS)
I changed it to .\SQLEXPRESS to make it relativ. Better.
Then i detached the db with SSMS, copied the DB to the App_Data folder, added AttachDBFilename=|DataDirectory|MyDatabase.mdf;
And (as found on msdn) Initial Catalog=; "use it but don't set it"->great feature ;)
This is the result:
<connectionStrings>
<add name="DBService" connectionString="Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|MyDataBaseFile.mdf;Initial Catalog=;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
When I start the asp.net Development Server (hit "play") in VS Web Developer 2010 there is a Database error:
Unable to open the physical file ".........mdf". Operating system error 5: "5(Zugriff verweigert)".
Any suggestions how to attach the file to the solution? What about switching to Compact Edition?
Have you tried adding
User Instance=True
?
Data Source=.\SQLEXPRESS;AttachDbFilename="|DataDirectory|MyDataBaseFile.mdf";Integrated Security=True;User Instance=True

Resources