Getting database connection error in three tier architechture in mvc 3 - c#

I'm trying to implement basic three tier architecture with mvc 3. but getting an error while running the solution ( showing no error while building the solution). The organization of my projects & error message can be seen in the attached image.
Please help.
OK guys, This is an update for.
As specified by some comments, I copied the original connection string from app.config to my web.config file & getting this error as shown in the image.
I tried to search a lot but no clue! Any help will be highly appropriated.

#Maarten is right. you always copied the normal connection string (that is a part of entity-connection string). you need to copy the entire connection string that was generated in your App.config file in Data layer and paste it in the web.config file in your presentation layer. the entity-connection string like as :
<add name="NorthwindEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.\sqlexpress;initial catalog=Northwind;persist security info=True;user id=sa;password=Blister01;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
don't copy-paste it, it is just a sample

The connection string you've copied is a normal connection string. You are using an edmx file which means you need an entity-connection string.
You need to copy the entity-connectionstring into the web.config. You can find the EF-connectionstring in the app.config file in the Data layer where EF added it for you.

you just add connection string connectionStrings section in web.config in Presentation layer!
See this image

EDITED: try with this
<add name="NORTHWINDEntities" providerName="System.Data.SqlClient" connectionString="data source=(local)/sqlexpress; initial catalog=NORTHWND;Integrated Security=SSPI" />

Related

Get Azure connection string in datalayer

Someone thought, a long time ago, it was a good idea to add connections string hardcoded in the datalayer of our web api. Because of this legacy I cannot remove this class. This class inherits IDbContextFactory and it needs to retrieve a connection string, which now is hard coded. To make it more... dynamic, I want to use the Azure connection strings for this.
1 I added the connection string to the configuration of the app service
2 According to a lot of websites I can just add the following code to retrieve the connection string:
connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
This does not work; it gives a "Object reference not set to an instance of an object." error.
Other say you need to add a prefix and the environment:
connectionString = Environment.GetEnvironmentVariable("SQLCONNSTR_DefaultConnection");
This does not work either, obviously. And yes, I selected sql server in the dropdown.
Other thing I tried, as someone suggested, is to add the connection string to the web.config. And again; this didn't work.
Good to know is that this is .NET 4.6.2, so all the beautiful solutions for .NET Core 1/2 aren't going to work.
First you have to include the same connection string in the web.config with empty connectionString value. Then you can overwrite from the application settings section.
`<connectionStrings>
<add name="DefaultConnection" connectionString="" />
<add name="CMSEntities" connectionString=""
providerName="System.Data.EntityClient" />
</connectionStrings>`
For entity framework specify providerName as "System.Data.EntityClient" and settings chose Custom instead of SqlServer

SQL Network Interfaces, error: 50 after moving to new PC

I am at a loss as to how to fix this issue.
When running my MVC application and trying to access the user database I get the dreaded SQL Network Interfaces, error: 50.
I can see all the data in the Server Explorer in VS, but I can never get the application to see it.
I have spent sometime reading this;
SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance 9
I have renamed and created new databases.
I have added/changed the entry in the applicationHost.config file.
I have changed the connection string every which way from Sunday.
I just cant figure it out.
Here is my connection string in the Webconfig file.
<add name="UserConnection" connectionString="Data Source=(LocalDB)\V11.0;AttachDbFilename=|DataDirectory|\ShopUsers.mdf;Initial Catalog=ShopUsers;Integrated Security=True" providerName="System.Data.SqlClient" />
I even downloaded and ran the ContosoUniversity sample code to see if it works, and it does.
Here is that connection string, (that works fine)
<add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity1;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
It seems there is something amiss between IIS and the db Server.
Any new ideas would be greatly appreciated.
Oh ya, I did have this working just fine on my old PC.
Seems like you have the database file created already (I suspect you do because of the following segment: AttachDbFilename=|DataDirectory|\ShopUsers.mdf;). If so, then ensure you have it in the same folder beneath your project as the original one on your old PC, usually the |DataDirectory| stands for App_Data (look the answer to this question).
If, you need a brand new database however, you should have tried the following connection string:
<add name="UserConnection" connectionString="Data Source=(localdb)\v11.0\ShopUsers;Initial Catalog=ShopUsers;Integrated Security=True" providerName="System.Data.SqlClient" />
This will create the database for you.

Interchange Connection string (Short cut to do something similar to Rename Refactoring)

I have two connection strings saved in Properties that I need interchangeability.
One is for a database that is used to test my application and the other is for the actuall database that the application will be used on.
My problem is that I have to use the connection string many times in different classes and as it stands now I am manually changing to the connection string by copy and paste.
Is it possible to do something similar as Rename Refactoring (C#) ,but not actually rename, but replace the connection string with the second one?
Ie, my first connection string is this,
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString_One))
Replace every connection string value into the second connection string,
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString_Two))
One is for a database that is used to test my application and the
other is for the actuall database that the application will be used
on.
If I understand correctly, you are trying to replace the connection string in the production build. Here's the step how to do it.
Save the connection string in config file
Sample code: (web.config OR app.config)
<connectionStrings>
<add name="ConnectionString" connectionString="{testing}" providerName="System.Data.SqlClient"/>
</connectionStrings>
Create a build transformation (by default, it would have debug build and release build
Replace the testing connection string with production connection string in release build
Sample code: (web.release.config OR app.release.config)
<connectionStrings>
<add xdt:Transform="Replace" xdt:Locator="Match(name)" name="ConnectionString" connectionString="{production}" providerName="System.Data.SqlClient"/>
</connectionStrings>
Hope it helps.
Read more at: https://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx

Regarding missing connection string in web.config

I am learning basics of MVC. And forgive me If I am not able to ask question properly because I am not aware of various technical terms. Anyways I am trying build one simple page where I will have two tables courses and Instructors. I am able to do most of the stuff and it seems to be working. Also I do have a solution given by my faculty of the same problem. When I am comparing my solution and faculty solution then in the web.config file of the actual solution given by my faculty I see something like
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcCRUDwithSQL-20140217025002.mdf;Initial Catalog=aspnet-MvcCRUDwithSQL-20140217025002;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="CourseContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=CourseContext-20140217145250; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|CourseContext-20140217145250.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
Where as in my solution that I just have one add name tag. I don't know why the another tag did not get created automatically. Or do I need to add that tag manually.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication9-20150313042831.mdf;Initial Catalog=aspnet-WebApplication9-20150313042831;Integrated Security=True" providerName="System.Data.SqlClient" />
Have I done something wrong that the connection string related to CourseContext did not get generated automatically. Or we need to add for CourseContext manually? Please help me. Since most of the files and its content are generated automatically I must have done something wrong that connection string pertaining to CourseContext did not get generated.
Use the steps below to generate the connection string via visual studio server explorer
Click on server explorer
Connect to database
Choose server name from dropdown
Choose your authentication type
Select your database from dropdown
Test connection
Copy the connection string from the properties and paste in your web.config
Also take a note of #Guffa answer, he's got a very good point
You don't need one connection for each table, you only need one connection for each database. A database can contain many tables (the project I'm working on right now has 9 tables, and that is a really small project).
The Visual Studio template starts out with a default connection string, but no database. It looks like it's that connection string that is still left in the solution.

setting connection parameters in web config

I want to know if it's possible to set a connection in a web config of my web project that depends of a dll and not from the connection String.
UPDATE
Ok I have a dll, that make's a connection with my database, i want to know if it's possible to configure my web.config to get the connection from the dll and not from the string connection.
This is only possible if you edit the web.config in your code to set the connection based on your dependency at run-time.
Yes - in fact it's the proper way to do it. You simply have teh DLL read the application's Web.config:
ConfigurationManager.ConnectionStrings["MyConnectionString"];
Where MyConnectionString is:
`<connectionStrings>
<add name="myConnectionString" connectionString="<your connection string>" providerName="<provider name>" />
</connectionStrings>`
EDIT TO ADD
I answered before the edit to your question. However, I still question your approach. The whole purpose of a DLL (in my experience) is to encapsulate code that can be used by multiple applications. The application calling the DLL should be responsible for setting the connection string.
However, if there's some reason that you must have the connection string in the DLL (hardcoded, I assume?), you would need to expose it (like via a property) so that consumers of the DLL would have access to it. In that event, the application could use the property to get the connection string.
Stephen Kinningham's answer is probably the route to go for what you appear to be attempting, but honestly, I can't think of a reason off the top of my head why you would want to.
<connectionStrings>
<add name="MyConnection" connectionString="Data Source=SQLSERVERNAME;Initial Catalog=MYDBNAME;Persist Security Info=True;User ID=USERID_OR_ADMIN;Password=PASSWORD;" providerName="System.Data.SqlClient"/>
</connectionStrings>

Categories