Visual Studio 2017 - adding references

demondan

Member
Joined
May 24, 2018
Messages
17
Programming Experience
1-3
Hi guys
I am trying to access a connection string created in the web.config file of my project. Using:

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringNameFromWebConfig"].ConnectionString);

Problem is System.Configuration.ConfigurationManager is not recognised, I've added in 'using System.Configuration' at the top of my page which is greyed out because its not in use - apparently.

I've done some searching and most of what I found tells me to add the reference to the System.Configuration. This is where I am stuck, first of all I don't have a reference node (which I've read to right click on) but I can right click on the project node and there is an add reference option which I can click however there is nothing available to add. I've got tabs on the left - Projects, Shared Projects, both with a sub tab named solution and then I have browse with a sub tab recent. All are empty, am I meant to point Visual studio at a folder/file? Do I need to install something?

Im sure this should be an easy answer but I am once again stuck :-/

If you can help that would be greatly appreciated!

cheers
Dan


 
Firstly, you should gain an understanding of exactly what namespaces and assemblies are and what it means to import and reference them. Check this out:

http://jmcilhinney.blogspot.com/2009/12/referencing-assemblies-and-importing.html

Secondly, you should learn how to use the MSDN documentation to determine what assembly and namespace a type is in, if you haven't already. Here's the documentation for that ConfigurationManager class:

https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(v=vs.110).aspx

Note that it states at the top that it is a member of the System.Configuration namespace and it's declared in the System.Configuration.dll assembly. There is not a one-to-one correspondence between namespaces and assemblies. You'll find that there are some members of that namespace declared in other assemblies that are generally referenced by default but that particular assembly is not referenced by default.

As for the issue, what type of project did you create in the first place? You say that you have a web.config file so it's obviously a web project of some sort. If you don't have a References node in the Solution Explorer then that suggests that you created a web site rather than a web application. I just created a new web site in VS 2017 and this is what my Solution Explorer looked like:

Web Site.PNG

Here's what the Solution Explorer looked like after creating a new web application:

Web Application.PNG

As you can see, similar but not the same and the latter has a References node while the former does not. There's a reason that web sites are listed under Web -> Previous Versions in the New Project dialogue. You really should avoid creating them and work exclusively with web applications.

That said, if you have a web site then you should still be able to add the required reference. In my web site project, I right-clicked the project node in the Solution Explorer to open the Reference Manager dialogue and then selected the Assemblies -> Framework node on the left. This listed all the .NET Framework assemblies available and I simply scrolled down to System.Configuration and check it:

Reference Manager.PNG

If you did something different along the way then please explain exactly what you did. In future, please explain exactly what you did from the get-go because there are a lot of different options in VS so if you don't tell us which ones you took then we have to guess and guessing means possibly guessing wrong.
 
Hi
Thanks very much for the reply, really appreciate this and im reading through the links you've sent me now - just sending in a reply to your post.

I think for me this is another place for confusion, there are lots of different ways to create a website and as a beginner it is confusing and I can appreciate I haven't explained which option I picked and so this makes it quite difficult to answer - apologies!


I believe I have created a web application not a website, i added in the web config file myself because my project didnt start with one. However the web application I created originally (which is what this thread is trying to address) looks different to the screenshot you've sent. On creating a new project I am given two options (although I am aware there are other options if I click on the older version tab but I stayed away from those):

newWebApp.jpg

So if I select the first one so Core Web Application and then choose Web Application as the template, I see this:
coreWebApp.jpg

As you can see there is no references node and there is also no web.config - also if I right click on the project node I still cant see anything in the
Reference Manager dialogue, its blank. If I choose the Asp.net web application (.net framework) which is the second option I have when creating a new project (as seen in the first screenshot) and then pick web forms as the template. I can then see the references node and it also comes with a web.config file already created so maybe this is where I am going wrong? I have been working with the asp.net core web application. I am happy to switch though as I haven't really got that far.

cheers
Dan


 
In an ASP.NET Core application, you can target either .NET Framework or .NET Core, with the latter being the default. If you chose the former then you can still right-click the Dependencies node to add a reference in the usual way. If you choose the latter, I think the only way that you can reference an assembly is by adding the appropriate NuGet package, which you can also do from the Dependencies node.

As JohnH says, you should stick to ASP.NET applications targeting the .NET Framework for now, unless you specifically want to read up about .NET Core and ASP.NET Core. The newer options probably won't offer you any advantages at this stage and there is less information available and more gotchas.
 
In an ASP.NET Core application, you can target either .NET Framework or .NET Core
I think you mean for an ASP.Net Web application you have that choice?
 
I think you mean for an ASP.Net Web application you have that choice?

Nope. The ASP.NET Web Application project template can only target the .NET Framework.

ASP.NET Web Application.PNG

After selecting the ASP.NET Core Web Application project, you then have to decide between .NET Core and .NET Framework, with the former being the default.

ASP.NET Core Application.PNG
 
I meant the path for web applications was either .Net fw or core when selecting a project type... I didn't know that you can use .Net framework for a "Core" project also.
 
Back
Top Bottom