'Connection string "SQLServerConnectionString" was not found.' - Using Azure DB - asp.net

Basically, I'm busy with a university project and this is the error I keep running into. I've tried multiple solutions from Microsoft's sites, but got nothing.
I added the connection string to my web.config, however, it still doesn't come through for me.
The connection link I took directly off of Azure's connection strings, on the DB I created.
<connectionStrings>
<add
name="SQLServerConnectionString"
connectionString= "Server=tcp:[dblink],1433;Initial Catalog=SACLADB;Persist Security Info=False;User ID=[id];Password=[pass];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
providerName="System.Data.SqlClient" />
</connectionStrings>
The code used to attempt and connect the database which should use the web.config connection string.
#{
ViewBag.Title = "Papers";
var db = Database.Open("SQLServerConnectionString");
var selectQueryString = "Select * FROM Papers ORDER BY Author";
}

Found the error of my ways;
I was editing the wrong Web.Config. I wasn't editing the one in the root folder where it reads the connection strings, however, all issues are now fixed.

Related

Proper format for SQL Server Connection String on 1and1.com

I have spent several hours trying to search this on the web in addition to calling and emailing support at 1and1.com web hosting with no success.
I have ASP.NET pages which connect to MS SQL Server database using the entity framework. On my local machine, all works fine (naturally). However, when I modify the connection string in my web.config to point to my SQL Server database on 1and1.com, I get the following error:
"Unable to load the specified metadata resource."
Here's my current connection string (as defined in my web.config file):
<add name="TimeDataLicenseEntities"
providerName="System.Data.EntityClient"
connectionString="metadata=res://*/
Model1.csdl|res://*/
Model1.ssdl|res://*/
Model1.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=dbXXXX.db.1and1.com,1433;
Integrated Security=false;
initial catalog=database_name;
user id=dboXXXX;
password=valid_password;multipleactiveresultsets=True"" />
(Please note that where there are 'XXXX' the values are different in the actual config file as well as for 'database_name' and 'valid_password')
I'm using the Entity Framework in the code so I'd prefer solutions that correct the content of my web.config file.
Here is what ended up working for me:
<add name="TimeDataLicenseEntities"
providerName="System.Data.EntityClient"
connectionString="metadata=
res://<assemblyname>/Model1.csdl|
res://<assemblyname>/Model1.ssdl|
res://<assemblyname/Model1.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=dbXXXX.db.1and1.com,1433;
Integrated Security=false;
initial catalog=database_name;
user id=dboXXXX;
password=valid_password;
multipleactiveresultsets=True"" />
By including the assemblyname for my code in the sections above (the brackets ('<' and '>') should not be included), I was able to finally get it to work.
Thanks all for your assistance.
Here is what actually works on 1and1 server from ASP.NET Code first design. So, simply replace your serverName, DatabaseName, UserID, and Password.
<add name="DefaultConnection"
connectionString="Data Source=dbXXX.db.1and1.com,1433;Initial Catalog=dbYYYYY;Integrated Security=False;user id=dboZZZZZ;password=YOURDBPASSWORD;MultipleActiveResultSets=True;Application Name=EntityFramework"
providerName="System.Data.SqlClient" />
Hope this can help anyone looking to use 1and1 asp.net server since their customer service is pretty useless.

ASP.NET Wepapplication with on-disk database - how?

How can I use an .mdf file (called SQL Server Database in VS2012) for Entity Framework?
I get this error message:
Invalid value for key 'attachdbfilename'.
This is my connectionstring:
<add name="DbContainer" connectionString="metadata=res://*/Database.Db.csdl|res://*/Database.Db.ssdl|res://*/Database.Db.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDb)\v11.0;initial catalog=DataContext;Integrated Security=True;AttachDBFilename=|DataDirectory|\DataContext.mdf;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />.
In Global.asax.cs, Application_Start I set the correct path for |DataDirectory| like this:
string newPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data");
AppDomain.CurrentDomain.SetData("DataDirectory", newPath);
Do any of you know to make this work?
Btw. it works when running my web application locally (i.e. debug), and does not work when publishing to my online domain.
If it works locally but not online, the problem is from data source part of your connection string. And if you want to attach your db at runtime, not by management studio, remove the initial catalig part. So change it to something like the following:
<add name="DbContainer"
connectionString="metadata=res://*/Database.Db.csdl|res://*/Database.Db.ssdl
|res://*/Database.Db.msl;provider=System.Data.SqlClient;
provider connection string="data source=RemoteServerInstance;
Integrated Security=True;AttachDBFilename=|DataDirectory|\DataContext.mdf;
MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />

Web deployment task failed

I'm getting this error when I click Build Deployment Package.
I've read a bunch of related post regarding this issue but doesn't seems to answer the questions on my mind T_T.
This is my connection string on my Web.Config
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="ProjectPALEntities" connectionString="metadata=res://*/Models.ProjectPal.csdl|res://*/Models.ProjectPal.ssdl|res://*/Models.ProjectPal.msl;provider=System.Data.SqlClient;provider connection string=';data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\ProjectPAL.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework';" providerName="System.Data.EntityClient" />
</connectionStrings>
This is Package/Publish SQL Settings Under Project Properties. This is where I'm having a problem.
This is the connection string for the source database above.
data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\ProjectPAL.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework';
QUESTIONS:
In the Connection string for the source database which is the correct one to use?
data
source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\ProjectPAL.mdf;integrated
security=True;user
instance=True;multipleactiveresultsets=True;App=EntityFramework'; (this one is what I'm currently using)
metadata=res:///Models.ProjectPal.csdl|res:///Models.ProjectPal.ssdl|res://*/Models.ProjectPal.msl;provider=System.Data.SqlClient;provider
connection string=';data
source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\ProjectPAL.mdf;integrated
security=True;user
instance=True;multipleactiveresultsets=True;App=EntityFramework'; (with metadata or none of the above T_T)
Where could I get the Connection string for destination database? Sorry It's my first time to deploy a website. I researched but I can't find an answer T_T.
How could I possibly fixed this error?
Other Informations:
I changed &quot with ' (single quote) after reading this reference Explicit connection string for EF
I'm using Membership Provider and Role Provider and all my data is saved in ProjectPALEntities
If you need more Info just tell me. :)
Ok here's what I did.
I checkout new application from my repository and repeat every steps I did. When I click Import from web.config it automatically created this connection string for me under Connection string for source database.
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ProjectPAL.mdf;Integrated Security=True;Application Name=EntityFramework;User Instance=True
for the Connection string for destination database I just leaved it blank because I dunno what to put there.
When I clicked Build Deployment Package the error message doesn't shows anymore.
I wish I knew what to put ont the Connection string for destination database T_T

ASP.NET EFCodeFirst not using correct connection string

I am trying to publish a website using ASP.NET MVC3 EF and CODEFIRST with a SQL Server 2008 backend. On my local machine I was using a sql express db for development, but now that I am pushing live, I want to use my hosted production database. The problem is that when I try to run the application, it is still using my local db connection string. I have completely removed the old connection string from my web.config file and am using the <clear /> tag before creating the new connection string. I have also cleaned the solution and rebuilt, but somehow it is still connecting to the old db. What am I missing?
This is the new connection string:
<connectionStrings>
<clear />
<add name="CellularAutomataDBContext"
connectionString=" Server=XXX;
Database=CellularAutomata; User ID=XXX; Password=XXX; Trusted_Connection=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
UPDATE
When I debug and look at the DBCONTEXT object, this is what is showing up for its connection:
Data Source=.\\SQLEXPRESS;Initial Catalog=CellularAutomata.Models.D1K2N3CARuleDBContext;Integrated Security=True;MultipleActiveResultSets=True"
I am unsure why this is happening because I cannot find it being set to this anywhere. Also, under configuration it says LazyLoadingEnabled = true, I assume this may be part of the problem, maybe it is not loading the new connection string. Where do I change these parameters?
UPDATE 2
EFCodeFirst is using a default connection string, I can't figure out how to get it to accept the connection string that I specify in the web.config file.
So, When using EF CodeFirst, there is a default connection string that it uses. If you want to be able to use a custom connection string, there are a few parameters guidelines that you must follow.
name ="this must match the name of your database context class"
connectionString="Server=yourserverurl; Database=yourdatabasename; User ID=youruserid;
Password=yourpassword; Initial Catalog=the name of the database to use;
Trusted_Connection=False"
providerName="System.Data.SqlClient"
So far this is working for me.
The connectionString you show is not an EF connection string. The EF won't use it. So you're changing the wrong thing.
An EF connectionString will include providerName="System.Data.EntityClient"
It will look for the same name as your context and depending on what else you
are using other names as well. I usually use the following for controlling
specific features with either the same or specific connection strings
(I keep app services in a different db for example so EFCF can drop tables as needed):
<connectionStrings>
<add name="MyAppContext" .../>
<add name="ApplicationServices" .../>
<add name="DefaultConnection" .../>
</connectionStrings>

SQL Express connection string hell ASP.Net

SQL Express 2005 is running locally. I have a project written by another person running on the same machine. All I want to do is connect to it, can't be that hard right?
This is the one I use in my old classic ASP code to hit another database running on the same instance:
Provider=SQLOLEDB;Data Source=MYLAPTOP\MSSMLBIZ;Persist Security Info=True;User ID=TestUser;Password=letmein;Initial Catalog=TestDB
But trying a version of that makes the .net code throw a wobbler as he's written it using SQLServer drivers so its not liking the Provider stuff.
Here is the orginal connection string from his code:
Server=(local);Initial Catalog=TheDatabase;User Id=TheUser;Password=ThePassword;
I've been to http://www.connectionstrings.com/sql-server-2005 and tried several of the options from there, these all get "SQL Server does not exist or access denied" (what a lovely mixed error message that is!):
Data Source=localhost;Integrated Security=True;Initial Catalog=TheDatabase
Data Source=localhost\SQLEXPRESS;Integrated Security=True;Initial Catalog=TheDatabase
Data Source=MyLaptop\SQLEXPRESS;Integrated Security=True;Initial Catalog=TheDatabase
Server=MyLaptop\SQLEXPRESS;Initial Catalog=TheDatabase;User Id=TheUser;Password=ThePassword;
I've created logins for MyLaptop/IUSR_MyLaptop, MyLaptop/ASPNET, MyLaptop/IWAM_MyLaptop in SQL Express and given them all read/write permissions to my DB and set their default DB to be TheDatabase.
What the heck am I doing wrong and how can I debug the problem some more?
UPDATE: Special Thanks to Chris for all his pointers, got there in the end, if you are having similar problem please read all the comments there are lots of links and tips on how to track them down.
Can you advise exactly what is in the config?
Are you using the block - in which case a valid connection string would be:
<add name="connection" providerName="System.Data.SqlClient" connectionString="Data Source=localhost\MSSMLBIZ;Initial Catalog=TheDatabase;Integrated Security=True" />
or
<add name="connection" providerName="System.Data.SqlClient" connectionString="Data Source=localhost\MSSMLBIZ;Initial Catalog=TheDatabase;Integrated Security=False;User Id=TheUser;Password=ThePassword;Application Name=AppName;" />
Or are you getting the connection string from app settings - in which case I guess your provider is set in code inside the app itself?
With that error message in your comment you should run through the items in http://blogs.msdn.com/sql_protocols/archive/2007/05/13/sql-network-interfaces-error-26-error-locating-server-instance-specified.aspx
I presume the instance is running and does allow connections over tcpip?
Shouldn't your datasource read: Data Source=localhost\sqlexpress too?
You don't mention granting rights for 'TheUser' to access the database on the server - if you're restored from another server you may had a sid mismatch.
Try running
sp_update_users_login 'report'
against the db in question.
If it returns the user account in the report try:
sp_update_users_login 'update_one', 'theuser', 'theuser'
to remap things.

Resources