asp.net mvc Invalid value for key 'attachdbfilename' - asp.net

I am currently reading Manning's "ASP.NET MVC 4 in Action" book and trying to get the first example to work.
Within my test application, I built a simple Model and created some Views. I then imported "SQL Server Compact" using NuGet.
When I finally try to run the application I get the following error:
Invalid value for key 'attachdbfilename'
This occurs on every interaction with the Database (SELECT or other CRUD operations) I am running. Any ideas?

Though, I am bit late to respond to this question but I was facing the same issue and I resolved it by modifying the connection string like below
<add name="MovieDBContext"
connectionString="Data Source=.;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
Hope this would be helpful for others too.

I tried finding a solution for this particular error without success.
Fixed it in the end by updating the .Net Framework 4 to 4.0.2. Patch and details can be found Here is the link
Hope it helps

I think the problems here is string in web.config is not correct
According to your set up sql, the string will work if you set something similar like this
<add name="MovieDBContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
Data Source=.\SQLEXPRESS; --> sometime, it will be Data Source=.;
after that, you need config right to access FOlder App_Data
If u test on Window 7, right click folder. property
Security tab -> add user network Service with full right

Go to section of Web.config and modify the section to the following to get SQLServerCE4 to be used:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0"/>
</parameters>
</defaultConnectionFactory>
</entityFramework>

I guess problem occurs only when using SQL Express on .NET Framework v4.0.30319 anyways
SQL Server Express Local Database Runtime support in SqlClient is added in .NET Framework update 4.0.2 read - http://support.microsoft.com/kb/2544514
Alternately use connection string similar to this
<add name="EmployeeDbContext" connectionString="Data Source=.;Initial Catalog=Employees;AttachDBFileName=|DataDirectory|\Employees.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />

<add name="myconstr" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|mydata.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
above connection string was giving an error but as soon as added"\" before the name of my database the error got resolved.
Try this:
<add name="myconstr" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\mydata.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

You are using SQL Server Compact 4.0:
Be sure you have installed SQL Server Compact 4.0.
Install VS 2010 SP1 Tools for SQL Server Compact.
Change your connection string to:
<connectionStrings>
<add name="MyModelDBContext" connectionString="Data Source=|DataDirectory|mymodel.sdf" providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
Right click on controllers folder and -> add -> controller
Type:
YourController
MVC-Controller with read/write ...
MyModel
new datacontext -> APP.Models.DatabaseContext

Here is a blog post I found about that error. It is a bit old, but uses the express version of sql server. MSDN Link
That blog post talks about expecting the server name of the sql database to be a local address and not /sqlexpress
There is also this blog post that talks about having an incorrect connection string. So maybe check your connection string to the database. and your problem could be there.

Facing the same issue, I have looked over my connection string and found that "UNC-style" paths e.g. \\MyServer\MyDBServer are not allowed, one has to use the MyServer\MyDBServer syntax for a remote host or the .\MyDBServer syntax for locally hosted DB Servers.

Related

ASP.NET VB change db connection on web forms framework

I have created an instance of ASP.NET Web Forms in my Microsoft Visual Studio. By default the program generates user login page and registration which are linked to the .mdb file.
I want to reconfigure the standard connection and link it to my instance of MSSQL 2012 SQLExpress server.
How can I do it?
Ok so I will give some direction and link you to a page.
First let me ask you this the connection you are using presently is it in a web.config file. If so this is where you will change the server/instance and database/catalog name.
Web.Config modification for SQL Server Instance
or this link
Web Config modification for SQL Express 2
So if you in the web.config file you should see something similar to this.
<ConnectionStrings>
<add name="" connectionString="Server=(localhost)\SqlExpress;Max Pool Size=300;Initial Catalog=database;User ID=username;Password=password;" providerName="System.Data.SqlClient"/>
</Connectionstrings>
or
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source= (LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\database.mdf;Initial Catalog=database;Integrated Security=True" providerName="System.Data.SqlClient" />
you need to change the follow:
Server or Data Source
and
Initial Catalog or AttachDbFilename
Now your credentials you may use any username and password that has access to the sql server express instance.
I hope this helps.

ASP.NET MVC 4 application database connection issue after session timeout

I have an MVC 4 website using .NET framework 4.5 that works fine at first. It allows me to login and go about my business adding, deleteing and editing records. As such, it is obviously initially connecting to the database without a probelm. However, if I leave it inactive for a while and come back at a later time, and then click a link to a page that shows data from my database, I get the following error:
The SSE Provider did not find the database file specified in the connection string. At the configured trust level (below High trust level), the SSE provider cannot automatically create the database file.
The website works fine on my localhost, but not when I host it online. I use shared hosting for this. I've done extensive searching online and found lots of people getting the same error, but they don't seem to have a website that works initially and then suddenly stops working. The solutions I've found online say something along the lines of:
add these two lines to your web.config file
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Data Source=p3swhsql-v15.shr.phx3.secureserver.net; Initial Catalog=DatabaseName; User ID=****; Password='****';"/>
by default the .net site is trying to find your database in the local filesystem in this case would be the app_data subfolder under the wwwroot directory. simply removing the connectionstring and replacing it with the database connection solves the issue. My example utilizes an SQL db on godaddy's server. however i'm sure you can replace the connection string with the appropriate string for your connection. good luck and i hope this helps anyone that may be having this issue
What my program seems to be doing is using the online database when I first login, and then reverting to trying to use the database in the local filesystem once the session timesout. This is very frustrating. My web.config file looks as follows:
<connectionStrings>
<remove name="DefaultConnection"/>
<add name="DefaultConnection" connectionString="Data Source=winsqls01.cpt.wa.co.za;Initial Catalog=SportFantasySA;Persist Security Info=True;User ID=*****;Password=*****" providerName="System.Data.SqlClient"/>
</connectionStrings>
Please help. I'm desperate to get this working. I understand that it can't create the sql express database in the hosting space, that's why I'm using the connection string for the database on my hosting company's server.
If you have hosted it on Godaddy go for the connection string as
<providers>
<remove name="AspNetSqlMembershipProvider" />
<add name="AspNetSqlMembershipProvider" connectionStringName="LocalSqlServer"
..other attributes here... /></providers>
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer"
connectionString="Data Source=xxxx; Initial Catalog=xxx; User ID=xxx; Password=xxx;"
providerName="System.Data.SqlClient" />

Deployment of SQL Server Express database fails

I have to deploy ASP.Net application with remote database connection in IIS server.
Deployment has worked fine without database. And I was able to run that application on the IIS server.
Coming to the application with remote database I have given the connection string in C# code. And it is connected.
My web.config has thsi connection string :
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
I have replaced that with my database connection credentials as follows:
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=RemoteHost,1433;Initial Catalog=DBNAME;user Id=MyUserId;Password=MyPassword"
providerName="System.Data.SqlClient" />
</connectionStrings>
In Package/Publish options I have selected (Import from web.config) and deployed that. Then I got the following error:
Web deployment task failed.(Object of type 'dbFullSql' and path 'Data
"Data Source=RemoteHost,1433;Initial Catalog=DBNAME;user
Id=MyUserId;Password=MyPassword" cannot be created.)
Object of type 'dbFullSql' and path "Data Source=RemoteHost,1433;Initial Catalog=DBNAME;user
Id=MyUserId;Password=MyPassword" cannot be created.
The source is invalid.
Script failed for Database 'DBNAME'.
Script failed for Database 'DBNAME'.
Index was outside the bounds of the array.
I am new to ASP.Net. Anybody please help me.
Thank you in advance
I think you didn't configure Web deployment package correct you can do that right Click on Project ,Select Properties and go to package/Publish SQL
I have add link for Walkthrough: Deploying a Web Application Project Using a Web Deployment Package (Part 1 of 4) for VS2010
http://msdn.microsoft.com/en-us/library/dd483479(v=vs.100).aspx

EF Code First with SQL Server Express 2012 ConnectionString

Everything works just fine with the following connection string.
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
I recently installed SQL Server 2012 Express on my local machine to test with, but I cannot make the connection. Here is my connection string using Windows Authentication.
<add name="ApplicationServices" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
I'm a total noob who tried his best to search the forums, but am unable to defer my solution from the 'Questions with similar titles' section. Any help is greatly appreciated.
EntityFramework code first uses a defaultConnectionFactory in the app.config or the web.config file which will automatically connect to .\SQLEXPRESS. This configuration looks like this:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
</entityFramework>
If you're using EF Code First, you probably have a class derived from DbContext to use as context. By design, EntityFramework will look for a connection string having the same name as your context class and use that one instead of the defaultConnectionFactory. This is how I'm using it:
<connectionStrings>
<add name="ObjectContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=My.Db.Name; Integrated Security=True; MultipleActiveResultSets=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
What I'm suggesting is to check that EF doesn't use it's defaultConnectionFactory and even force overriding that by adding the connection string named after your context.
You should also check this blog article which gives great details about the EF configuration file.
Are you sure this part of the connection is right?
"Data Source=.\SQLEXPRESS [Name];
I've not used SQL Server 2012, but try to remove the [Name] text.
This worked for me
add name="MovieDBContext" connectionString="Data Source=.\SQLEXPRESS;
Initial Catalog=Movies;
Integrated Security="True" providerName="System.Data.SqlClient"
I was working through this tutorial and had to work it out. The MovieDBContext is created in code in the model of an mvc application
public class MovieDBContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
This automates the IDE creating the controller class but it wouldn't work till I got the connection string right in the web.config file.

"The connection string specifies a local Sql Server Express instance", Except it Doesn't

I'm trying to deploy an asp.net application to a server using a SQL Server instance for the ApplicationServices membership database.
The problem is, I'm getting an error that says
The connection string specifies a local Sql Server Express instance
using a database location within the applications App_Data directory
I got this error when I initially tried to deploy the aspnetdb.mdf itself with the application.
I got this error when I then scrapped that plan and decided to do a web.config transform so that in Debug I use the Express database, but on Release the connection string goes to SQL Server.
I got this error again when I decided out of curiosity to remove all references to the express database from the code, so there could be no possible way anything would be looking for the Express database. No luck.
Does anyone have any ideas about this? I have deleted and re-installed the web site in IIS each time, noting that there is no App_Data being deployed and no mention of the .mdf file in web.config - to no avail. It still thinks there's a connection string telling it to look for a SQL Server Express database :/
Edit: Here's the connection string I'm using. Pretty standard, I think, but I could always be wrong.
Data source=HERP;Initial Catalog=DERP;Integrated Security=True
Could the error refer to the default connection string from the machine.config file (the LocalSqlServer one)? This could be happening, considering that the default membership provider uses this connection string:
The following default membership element is configured in the
Machine.config file [...]:
<membership>
<providers>
<add name="AspNetSqlMembershipProvider" [...]
connectionStringName="LocalSqlServer" [...]
Do not forget to clear the connectionStrings first:
<connectionStrings>
<clear />
<add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

Resources