Well this i did the below to get the error, don't have a clue why the database connection fails.
Create a new ASP.NET Website
Add a new *.mdf database to App_Data
Add some tables to it using Server Explorer in Visual Studio
Right click DataBase and Copy Connection string. Insert it into WebConfig File like below
<connectionStrings>
<add name="DB" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\inetpub\wwwroot\gs\App_Data\db.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
Add some code to get the data from
selectStatement = "select * from users";
SqlDataAdapter da = new SqlDataAdapter(selectStatement,
ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
DataTable dtUsers = new DataTable();
da.Fill(dtUsers);
GridView1.DataSource = dtUsers.DefaultView;
GridView1.DataBind();
and zoot you get the error
I have a sneaky suspicion it has to do with permissions. Give full control to your "Authenticated Users".
In case you are wondering how to do this --- I am on Windows 7 and the steps go like this:
HTH
The top result from Google seems to address your question:
Just in case if anybody is still looking for solution to this error, this works for me:
1) Open the VStudio project for which you need to connect to a SQL database
2)Separately, Go to Start->Run->Services.msc
3) Look for SQL Server (SQLEXPRESS) service and Stop it
4) Start it again
5) Try connecting your database now.
Looks like the reason it works has something to do with User Instance discussion that is going on in this thread.
I was struggling with this error to and I found that the error was in the database instance that was online so I took it offline from SQLserver management studio,I've shared the steps followed and the solution HERE
In my case, I had the database in instance MSSQLSERVER while trying to attach it to SQLEXPRESS. Dropping from the first instance freed the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With