We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

'System.Configuration' is a namespace and cannot be used as an expression

Medium Priority
2,757 Views
Last Modified: 2013-11-19
I'm trying to form this right for my .NET 2.0 app

 Dim Conn As SqlConnection = New SqlConnection(System.Configuration!System.Configuration.ConfigurationManager.AppSettings("MYConn"))

Error      3      'System.Configuration' is a namespace and cannot be used as an expression
Comment
Watch Question

DBAduck - Ben MillerSQL Server Architect
CERTIFIED EXPERT

Commented:
Shouldn't it be:

Dim Conn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("MYConn"))

I am not sure where you got the "System.Configuration!" in your statement, but that is what it is complaining about.

Ben Miller

Author

Commented:
System.Configuration - asp.net 1.1

Author

Commented:

System.Configuration.ConfigurationManager.AppSettings

Error      2      'ConfigurationManager' is not a member of 'Configuration'.
DBAduck - Ben MillerSQL Server Architect
CERTIFIED EXPERT

Commented:
Oh, my point was that you had it in there 2 times.

To reference AppSettings you could either put an Imports statement in the top of the code for System.Configuration or reference it with full namespace like I put in my other comment.

The post originally in the question had System.Configuration!System.Configuration which is a duplicate and the ! indicates an Expression not a namespace.

HTH.
Ben Miller

Author

Commented:
it's not liking ConfigurationManager is my point...

Author

Commented:
I have this System.Configuration.ConfigurationManager.AppSettings

but it is not liking ConfigurationManager as in my error states.  This is a VB.NET console application. I've placed the connection string in app.config
DBAduck - Ben MillerSQL Server Architect
CERTIFIED EXPERT

Commented:
Oh, man.

Sorry.  If it is 1.1 then it should look like this:

System.Configuration.ConfigurationSettings.AppSettings("MyConn")

Sorry for the misunderstanding.

Author

Commented:
this is ASP.NET 2.0.  I'm trying to do what I normally do in my ASP.NET apps which is form my connection string and reference it from my .config file.  THIS however is a VB.NET console app in which I"m trying to do the same thing.  I have created an App.config file, and in it put the same type of connection string as I have done in my ASP.NET apps...unfortunately it doesn't seem to work.

Let me restate the entire issue.

vironment:
Using VS 2005
Coding in .NET 2.0
Problem:
If I'm creating let's say a VB.NET console application, how do I tie in the connection string properly right down to syntax for .NET 2.0 if I have to work with portions in my Database such as writing files to a specific directory using System.IO and doing a lookup vie a lookup function I am creating to look up values in my SQL Table?  With ASP.NET you define your connection string in web.config but what about a console app?
I tried to add a new app.config to my console app.  In it have this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                 initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>

  <connectionStrings>
    <add name="MYConn"
         connectionString="Data Source=myserver;Initial Catalog=mydb; integrated security=SSPI;persist security info=False; Trusted_Connection=Yes"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
 
</configuration>
In my console.vb code, in one of my functions I have this:
Dim objConn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("MYConn"))
I get the error in my .vb code: Error 2 'ConfigurationManager' is not a member of 'Configuration'.

Author

Commented:
Environment:
-----------------
Using VS 2005
Coding in .NET 2.0

Problem:
----------
If I'm creating let's say a VB.NET console application, how do I tie in the connection string properly right down to syntax for .NET 2.0 if I have to work with portions in my Database such as writing files to a specific directory using System.IO and doing a lookup vie a lookup function I am creating to look up values in my SQL Table?  With ASP.NET you define your connection string in web.config but what about a console app?
I tried to add a new app.config to my console app.  In it have this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                 initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>

  <connectionStrings>
    <add name="MYConn"
         connectionString="Data Source=myserver;Initial Catalog=mydb; integrated security=SSPI;persist security info=False; Trusted_Connection=Yes"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
 
</configuration>


In my console.vb code, in one of my functions I have this:
Dim objConn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("MYConn"))
I get the error in my .vb code: Error 2 'ConfigurationManager' is not a member of 'Configuration'.

Author

Commented:
I'm going to start using my template when posting, I think it makes the issue more clear....tell me if the template helps.
SQL Server Architect
CERTIFIED EXPERT
Commented:
OK, if you are using 2.0 and VB.NET and a Console app.

Here goes.

the first thing is that you need to set a Reference to System.Configuration because you do not get this one for free, especially in a Console App.

So right click on the project in VS and choose Add Reference.  Select System.Configuraiton and it will add the reference to your project.
Then put a statement at the top of your Module for the Console App:

Imports System.Configuration

Now you can use the following line of code to get your connection string.

Dim objConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("MyConn").ConnectionString)

Ben Miller

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:

Author

Commented:
Thank God, literally!

Author

Commented:
this was driving me batty.  I had the namespace imported....ahh but it's so picky in 2.0
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.