In my main method, I had a line of code -
WebRequest reqObj = WebRequest.Create(CompleteDPath + FileName);
I added an import - System.Net.WebRequest
. This causes an error:
The type or namespace name 'YourProject' could not be found (are you missing a using directive or an assembly reference?).
The error goes away when I change the import to System.Net
Why does this happen ?
Actually, I am using this code inside a C# script task of SSIS, an ETL tool. I mention that in case you cannot replicate the problem in plain visual studio and want to declare SSIS as a source of the problem.
using Directive (C# Reference)
The using directive has two uses:
- To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace:
using System.Text;
- To create an alias for a namespace or a type. This is called a using alias directive.
using Project = PC.MyCompany.Project;
You can't import a type, you have to import the entire namespace, and that's why using System.New.WebRequest
does no work, and your code does not compile.
You could create an alias to WebRequest
, to prevent entire namespace to be imported, but I personally don't like that solution:
using WebRequest = System.Net.WebRequest;
And yes, the error could be a little more descriptive, but that's the problem here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With