Hi every one I am getting the following error: "The name 'ConfigurationManager' does not exist in the current context"
// namespaces
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
namespace Database1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string GetConnectionString(string strConnection)
{
//variable to hold our connection string for returning it
string strReturn = "";
//check to see if the user provided a connection string name
//this is for if your application has more than one connection string
if (!string.IsNullOrEmpty(strConnection)) //a connection string name was
{
//get the connection string by the name provided
strReturn = ConfigurationManager.ConnectionStrings[strConnection].ConnectionString;
}
else //no connection string name was provided
{
//get the default connection string
strReturn = ConfigurationManager.ConnectionStrings["YourConnectionName"].ConnectionString;
}
//return the connection string to the calling method
return strReturn;
}
}
}
Have you added a reference to System.Configuration.dll?
You have to add reference to System.Configuration.dll
When you right click and click on the Add reference button
Click .Net tab in the open popup
There you should be able to locate the System.Configuration.dll file
This is the path:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Configuration.dll
also you can add reference to the :System:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Configuration.Install.dll
(hope it helps :))
Which version of .NET Framework and Visual Studio are you using?
ConfigurationManager is only available in .NET 2 and above. If you are using .NET Framework 1.x and Visual Studio 2002/2003, you won't be able to use this class at all.
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx
Click "Other versions" link in the top of this page and you will see all versions it supports.
I ran into same issue using .Net Core, but using .NET Running Install-Package System.Configuration.ConfigurationManager in Package manager solved it for me.
Related
I'm following a guide to write output data from Visual Studio into a google spreadsheet. I'm using a NUnit project type for test-automation purposes.
At the end of the guide there is a code block that I pasted inside my project:
using OpenQA.Selenium.Support.UI;
using System;
using NUnit.Framework;
using OpenQA.Selenium;
using System.Collections;
using System.Collections.Generic;
using Google.Apis.Sheets.v4;
using Google.Apis.Auth.OAuth2;
using System.IO;
using Google.Apis.Services;
using Newtonsoft.Json;
using WikipediaTests.Foundation_Class;
namespace AutomationProjects
{
[TestFixture]
public class TestClass : TestFoundation
{
public class SpreadSheetConnector
{
//Codeblock from guide pasted here!
}
[Test]
public void test1()
{
//Test case 1. Do XYZ...
}
}
}
In the code block included in the guide there is a section that reads the JSON credential file:
private void ConnectToGoogle()
{
GoogleCredential credential;
using (var stream = new FileStream(Path.Combine(HttpRuntime.BinDirectory, "Export Project-03e8aa07234e.json"),
FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream).CreateScoped(_scopes);
}
//...
But I get an error for the 'HttpRuntime' saying: Error CS0103 The name 'HttpRuntime' does not exist in the current context
There is no suggestion from VS to add a new 'using' reference so I'm assuming that is not the problem.
So what could be the problem? To whole codeblock from the: guide
Short answer - yes.
Long answer - I believe you need to add the System.Web dll here. C# projects do not add all dependencies by default -- rather, they provide you with a list of potential references, and let the user pick and choose on an as-needed basis.
Under your project, find the Dependencies section. Right click, and click Add Reference. Under Assemblies, find System.Web and check the box next to it, then click OK.
Once you add that, then you will need to add using System.Web to the top of your file.
This guide may help too: https://learn.microsoft.com/en-us/visualstudio/ide/how-to-add-or-remove-references-by-using-the-reference-manager?view=vs-2019
It can be seen and compiled inside the same file it is in inside the same project, the file is called default.aspx.cs.
However when I try to include the namespace in another file of the same project using the using DBConnStrings; statement -> I keep getting the compiler error "The type or namespace name 'DBConnStrings' could not be found".
The Code in the file which compiles called default.aspx.cs is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using DBConnStrings;
namespace DBConnStrings
{
public class GlobalStrings
{
public static string carSalesDBConnString =
ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString();
}
}
public void BindManu()
{
SqlConnection conn = new
SqlConnection(GlobalStrings.carSalesDBConnString); // Connect to Carsales database
conn.Open();
// .....
}
The other files can not see this namespace although they are in the same project.
How do I make them see it?
First of all, I'd suggest to put the method BindManu into the class, because I can't imagine this would work like that.
But to your problem: You have to specify the whole namespace. That means, if the file with the DBConnStrings namespace is in the folder test, you have to use using test.DBConnStrings to import the namespace. You should name the namespace like that as well to avoid confusion (namespace test.DBConnStrings).
But you actually don't have to specify the whole path, just the path within the project. That means, if your class is in C:\Users\Foo\Documents\Visual Studio 2017\Projects\BlaProject\DirectoryA\DirectoryB\MyClass with the project located in C:\Users\Foo\Documents\Visual Studio 2017\Projects\BlaProject, your namespace would be BlaProject.DirectoryA.DirectoryB. If the file your using isn't in your project folder, then you have to add a reference and use the path within the other project as above (but with another project name, obviously). If you want to add references, open the Solution Explorer, right click onto References, select add Reference, and select the reference to the project.
If you don't want to struggle with all that you can let vs do it for you. Simply type in the class you want from the other namespace, it will be marked as an error, click onto the lightbulb and select something like add using reference.
Furthermore, if you want to add a class to your project, don't do it with the explorer - simply right click onto the folder from your project you want to add the class to in your Solution Explorer, select Add, then New Item. In the popup select class and type in your name for the class. That's it!
I am a beginner in c# programming. I am gettting error The name 'File' does not exist in the current context.
Problem must be in the line var v = File.ReadLines("dictionary.txt");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
var v = File.ReadLines("dictionary.txt");
Just add this using statement to the top of you file
using System.IO;
The compiler only recognises classes from namespaces that you have in the current context. You add namespace to the context using using statements. You can also use a fully qualified type name such as System.IO.File to refer to the class.
If you are using Visual Studio you can also place the cursor on the problematic symbol (File) and press Shift + Alt + F10
For anyone using Visual Studio 2015 this error will still occur even if System.IO is referenced. The problem is that by default a Visual Studio 2015 project will target both the dnx451 and dnxcore50 frameworks, and the System.IO assembly doesn't seem to be available for dnxcore50.
If you look in the project.json file you'll see a "frameworks" section. The quick fix is to comment out the "dnxcore50" entry so that you're only targeting dnx451:
"dnxcore50": {
"dependencies": {
"System.Console": "4.0.0-beta-22816",
"System.Collections": "4.0.10-beta-22816",
"System.Linq": "4.0.0-beta-22816",
"System.Threading": "4.0.10-beta-22816",
"Microsoft.CSharp": "4.0.0-beta-22816"
}
}
Add using System.IO; to your usings section.
File class is located in System.IO namespace.
Alternatively (if it is the only place in your code where you're using some type from System.IO) you can use fully qualified name of File like:
var v = System.IO.File.ReadLines("dictionary.txt");
But in the case when you need to access objects from some namespace multiple times in your code, it is better to inculde that namespace in usings.
You need to include System.IO add using System.IO next to other usings.
Add a using statement:
using System.IO
I'm working .Net Core in vs2017 and I have a similar problem.
To solve this problem you should change the target framework and install System.IO.FileSystem.
Using the following methods:
Right click the project and select properties.
in Application Tab and Target Framework section change to .NetStandard1.6
Then, To install System.IO.FileSystem, run the following command in the Package Manager Console
Install-Package System.IO.FileSystem -Version 4.3.0
1.In here first we need to create the object like this.
OpenFileDialog ofd = new OpenFileDialog();
2.Then you need to add this top of your code
using System.IO;
3.Finally you can change your code like this
OpenFileDialog.Title -----> ofd.Title
OpenFileDialog.Filter -----> ofd.Filter
I'm trying to get SQLite running in Visual Studio 2012 for C#.
However after going through a set of tutorials I still get the error DllNotFoundException for the SQLite.Interop.dll.
This is the full error I receive:
Unable to load DLL 'SQLite.Interop.dll': The specified path is
invalid. (Exception from HRESULT: 0x800700A1)
I have created a reference for the System.Data.SQLite.dll. Now I found that I have to add the SQLite.Interop.dll file into my project folder but I still get this error.
Oh and BTW, this is my code, if anyone is interested:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
namespace SQLiteWinFormCS
{
public partial class Form1 : Form
{
private SQLiteConnection _sqlCon;
private SQLiteCommand _sqlCmd;
private SQLiteDataAdapter _db;
private DataSet _ds = new DataSet();
private DataTable _dt = new DataTable();
private string _dbPath = String.Empty;
public Form1()
{
InitializeComponent();
}
private void uiOpenDB_Click(object sender, EventArgs e)
{
Console(String.Format("You clicked {0}.", ((Button)sender).Name));
this._dbPath = uiDatabaseFilepath.Text;
Console("Filepath to DB = " + this._dbPath);
Console("Attempting to open DB connection...");
this._sqlCon = new SQLiteConnection(String.Format("Data Source={0};", #"\\Some-PC\ISC\Databases\testdbs\test.db3")); // << ERROR
Console("DB connection succesfull!");
}
private void Console(string text)
{
uiConsoleOutput.AppendText(text);
uiConsoleOutput.ScrollToCaret();
}
}
}
Can anyone help me get this thing working?
Thanks in advance.
Copy the SQLite.Interop.dll file into your debug folder.
For example "Projects\sqlite test18\sqlite test18\bin\Debug" place it here.
And dont add the Interop as a reference.
Add only these references
System.Data.SQLite
System.Data.SQLite.EF6
System.Data.SQLite.EF6
This solved my problem.
And I was using Sqlite x86 under x64 OS.
Dohh...
I was probably using a wrong System.Data.SQLite.dll.
For anyone who is interested you can find the new .dll file I downloaded here:
http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
I needed the .dll for the 4.5 Framework (x86); I clicked the first download link.
Also I am using just the System.Data.SQLite.dll, no other files!
Hope this answer helps someone else. :-)
I'm having difficulty getting my settings out of App.config. The research I've done so far says that I should be able to use ConfigurationManager.AppSettings (which is part of System.Configuration).
However, I can't get it to compile. And of course it doesn't like the older method (ConfigurationSettings.AppSettings) either as its obsolete.
What have I missed here?
Is it my project's Target Framework (currently set to ".Net Framework 4 Client Profile
")?
And example of my code follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace myNameSpace
{
class Program
{
private void LoadAppConfig()
{
string mySetting;
//This won't compile
mySetting =
System.Configuration.ConfigurationManager
.AppSettings["mySettingName"];
//This compiles but of course is obsolete, and I get that warning.
mySetting =
System.Configuration.ConfigurationSettings
.AppSettings["mySettingName"];
}
static void Main(string[] args)
{
// stuff happens
}
}
}
You need to add a reference to System.Configuration