ConfigurationManager.ConnectionStrings[...] can't be used even after including "using System.Configuration" - c#

I'm coding a project in C# in ASP.net MVC. I have the connection string in the Web.config file. I can't share it due to security reasons. In my code, I have the line: string CS2 = ConfigurationManager.ConnectionStrings["..."].ConnectionString; But I get an error and a red line under "ConfigurationManager" that says it does not exist in the current context.
I've checked other pages that say I need to include the reference by clicking on the project > Add > Reference > and then select the correct one. But I don't see Configuration listed there. And after browsing and searching, I still don't find it. I'm not quite sure what I can do besides try learning the Entity Framework which I'm not really a fan of doing.
EDIT: solution ended up being finding the library in NuGet, installing it, and adding an app.config file with the connectionString in it.

What version of .NET are you using? For .NET Core projects, reference the Nuget package linked below. For a full .NET project you should be able to include the System.Configuration project from references. And make sure using System.Configuration; is in place.
https://www.nuget.org/packages/System.Configuration.ConfigurationManager/

Related

Could not load information for project X after update codegeneration 6.0.9

I'm developing a .NET Core project. Yesterday, Web.CodeGeneration was updated automatically. After the update, I get an error when I try to add new view to my project:
"Scaffolding Failed"
"Could not load information for project X"
I tried to remove and re-install all nuget packages again, and I checked package versions and all of them are 6.0.9 so, same version.
I tried to create a new project to test "add view", but when I install Entity Framework Core packages with web.codegeneration in the test, project, I again get the same error.
How to fix it?
Unload all your class libraries/other projects except for your web application then try re-adding any scaffolded items. This is the current workaround that works on my end, at least until this bug gets fixed.
Update:
Bug is patched with the Microsoft.VisualStudio.Web.CodeGeneration.Design 6.0.10
EDIT - Fixed in 6.0.10, if you are experiencing this issue make sure you update the packages.
The issue has been reported and is a bug in the scaffolding code.
The suggested workaround is to scaffold in a new solution/project with the same name(s) and then copy the files over:
Make new project with same solution and project name and add DB context and
other necessary thing than scaffold then the new added files add in the
main project you are making, now you are good to go.
Unloading the linked projects as suggested by others works, provided you don't have your models in separate class projects.
Otherwise we will have to wait
Remove all project references in the project you are working on, then you won't get error. After adding Areas etc. you can again add project references
If you create references to other projects, the error returns. Importing the libraries works, but if you reference them it gives an error
This isn't actually an answer, but I'm too new to leave a comment. I just wanted to pass along some information that I found useful.
I was having an issue scaffolding my MVC Controller, with views, when my Library was unloaded, even though I kept the reference. This bug is also being discussed on GitHub, and Deepak Joy Jose uploaded this video showing the workaround: Scaffolding Workaround It's for identity scaffolding issues, but the same logic applies to controllers. It's a long workaround, but it did work for my issue.

Hangfire + ASP.NET MVC

This is my first time posting here.
I am currently writing an ASP.NET MVC application (using .NET 4.5.2 and MVC 5). I want to integrate Hangfire into my project. I have set up a side project which worked. But when I tried to integrate it into my main project, several errors occurred. They are:
The .dll files had to be strong-named (I fixed this issue by using ildasm/ilasm to strong name them. I replace the original files that came with the nuget package.)
After strong-naming and replacing the files, I ran into another error. When I tried to build and run the solution, a 'GlobalConfiguration' does not exists in the current context, and also a are you missing an assembly reference error is thrown.
I have tried various ways to solve this, but to no avail. I would really like some advice on this ! Thanks !
GlobalConfiguration is a class in the Hangfire namespace. You should import the namespace so that you can use it (and the extension methods it provides)
using Hangfire;
So now code like this will work:
GlobalConfiguration.Configuration.UseSqlServerStorage("MyConnectionString");

Reference Missing After Nuget Package Update

I want to start off by saying I've been really hesitant to ask this question because it seems like it should be such a simple thing, however, I've scoured the internet and all I can find is the basic solution to something similar to my issue.
I have a nuget package that is v 1.30 and I'm trying to update it to 1.41. I've used the NuGet UI and I've also used the Package Manager Console to do this multiple times with small variations and I continue to get the same result. After I do something like "Update-Package PackageName" my package updates correctly. NuGet deletes the old reference and the old files and the 1.41 version is referenced by all the projects in the solution using that package. When I look at the Code, there are no syntax errors, Visual Studio seems to think the reference is just fine and I can even ctrl+B (re#er) to 'go to declaration.' However, when I build, every place I have 'using PackageName' in code throws an error that says
"The type or namespace name 'Common' does not exist in the namespace 'PackageName' (are you missing an assembly reference?)"
I have tried cleaning my solution. Then building. I've tried resetting through Git, cleaning the solution, updating then building. I've tried updating, cleaning, deleting the reference, building (will get the errors obviously), then adding the reference back in manually and building. Every time I try I get the same error. It's only happening for my Core project, but all my other projects depend on the Core, so they won't build anyway. I'm assuming this issue would still present itself if they were able to build. I've looked at the properties of the reference, it is referencing the v 1.41 file and it is set to Copy Local = True (I read somewhere someone suggested looking at that).
I'm open to suggetions for things to try. I don't have any code changes that I'll lose of I do a Git Reset, so I can try whatever.
Thanks Internet.
After trying several different things, we figured out it was a version mismatch with ASP.NET. Someone had pulled in a newer package that required updating to ASP.NET 4.5 and the solution I was working in was still ASP.NET 4.0. After updating to 4.5 the problem with the references was resolved. But we ran into some new Automapper issues. One thing after another. However, if you're banging your head against your desk in frustration because of this problem, check the framework version of your projects and the package you're updating.

When compiling solution, previously working assembly reference / using no longer works [duplicate]

My C# WinForms solution has two projects.
A DLL which is the main project I'm working on, and an executable WinForms I call "Sandbox" so that I can compile/run/debug the DLL easily in one go.
I'm working in .Net 4.0 for both projects.
Everything was working fine until I added some seemingly innocent code, and a reference to System.Web in the DLL.
Now my Sandbox project can't see the namespace of the DLL project. I didn't change anything which I believe should have affected this.
If I delete the project reference to the DLL from the Sandbox references and re-add it, then the red underlines all disappear and the colour coding comes back for all my classes etc; but as as soon as I try to build the solution, the whole thing falls apart again.
When I right-click the DLL project in the Sandbox's references and view in object browser, I can see the namespace and all the stuff in there.
I have a feeling this might be some sort of bug?
Is this some sort of VS2010 bug? I had this same issue a few months ago and I could only fix it at the time by making a whole new project and re-importing my files. This time, however, I have a bajillion files and will only do that as a last resort!
Edit:
After panickedly going through and undoing all my changes, trying to find what caused the problems, it seems to be this line:
string url = "http://maps.google.com?q=" + HttpUtility.UrlEncode(address);
If I comment out this line, then I get no namespace errors and the project builds fine. I can't see anything wrong with this line though.
I'm ready to declare this a bug in VS2010, this has bitten way too many programmers already. The fix is easy: Project + Properties, Application tab, change Target Framework to ".NET Framework 4" instead of the Client Profile that is selected by default.
System.Web is not included in the client profile. Having this option in the first place is quite silly, the client profile is only 15% smaller than the full version of .NET 4.0. Having it selected by default is even sillier. But I digress.
UPDATE: mercifully this all got fixed in VS2012. Which no longer makes the client profile the default for a new project. And the client profile got retired completely in .NET 4.5, good riddance.
Check to make sure that both projects are using the non-client profile for their target framework (go to each project's properties to do this).
One possibility is that the target .NET Framework version of the class library is higher than that of the project.
I faced this problem, and I solved it by closing visual studio, reopening visual studio, cleaning and rebuilding the solution. This worked for me. On some other posts, I have read the replies and most of users solved the problem by following this way.
Try building only the project with the Sandbox dll first independently.
Then point your executable project to the required dll and ensure copy local is set to true. in reference settings.
Tthen build the executable project.
Changing the target framework from the ".NET Framweork 4 Client Profile" to ".NET Framework 4" worked for me with a similar problem. I agree that the client profile doesn't seem to have much of an advantage to using it. I seem to get nailed with weird errors that I hunt for until I remember that Visual Studio defaults to the client profile. I guess the moral of the story when getting an error is: if "Rebuild Solution" doesn't work, check the Target framework...
If you tried already doing the Framework change, and still not worked, I hope this works for you (as it did for me): Simply add the necessary references from within your projects. Very obvious but I was doing it wrong until I found what was the issue.
I just had this issue and it turned out to be I had multiple namespaces being used that had the same object name (i.e. business objects had the same names as mvc models);
Fully qualifying the names fixed the issue for me.

the type or namespace name could not be found (Reference present)

After I build a few of my references, which are present in the project and accessible through intellisense before the build is performed.
Unfortunately none of my projects are set to target Client Profile, as answered in this thread.
The type or namespace name could not be found
This seems to be the most common cause, does anyone know what else other than this can cause the same issue?
Edit:
The Types that aren't found are all from the referenced DLLs. I have the correct using directives for these and they are present in the resource folder, however they become underlined red after a build (I did try cleaning to).
My initial thought was the framework (the project is on v3.5), but I checked all my resources working or otherwise all where on v2 bar a few (working ones) on v4 which I removed temporarily to make sure we're causing issues.
Unable to access the project from my current location but will post any requested specifics as soon as I can.
edit:
Resolved the issue by deleting all of the custom class libraries... found and rebuilt each of their projects then added the new DLLs back into the project. Stored libraries weren't very organised, so I probably have some confliction between old/new versions.
Have you read the warning in the error list? You might need to install nuget package used by the referenced projects
for me the error warning was
All projects referencing "project file" must install nuget package Microsoft.Bcl.Build.
Are the projects being built correctly ?
Sometimes, you need to manually generate them (right click on them, generate), before being able to run a full build.
I dont exactly know why, but this might be the way to go for you.
~A few simple things to try before getting into more complicated water...
Try clicking:
Build->Clean Solution
Build->Build Solution
If this doesn't work, check your using statements are all there and correct, check your references all still exist.
I had a similar issue today, and I thought I would mention it here in case it helps someone else out. In my case, I have a VB.net Class Library which is referenced by a C# class library. The solution would build fine, but in the IDE, as soon as I started editing a file in the C# project, I would get errors about not finding a reference to the VB.Net library. It turns out that the VB.Net class library had a reference to System.web (lowercase w). I'm not sure how that came about -- but the solution was to delete that reference and replace it with a reference to System.Web (uppercase W).
Make sure all the projects in your solution have the same "Target framework" value

Categories