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

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
LVL 1
dba123Asked:
Who is Participating?
 
DBAduck - Ben MillerPrincipal ConsultantCommented:
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

0
 
DBAduck - Ben MillerPrincipal ConsultantCommented:
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
0
 
dba123Author Commented:
System.Configuration - asp.net 1.1
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
dba123Author Commented:

System.Configuration.ConfigurationManager.AppSettings

Error      2      'ConfigurationManager' is not a member of 'Configuration'.
0
 
DBAduck - Ben MillerPrincipal ConsultantCommented:
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
0
 
dba123Author Commented:
it's not liking ConfigurationManager is my point...
0
 
dba123Author 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
0
 
DBAduck - Ben MillerPrincipal ConsultantCommented:
Oh, man.

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

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

Sorry for the misunderstanding.
0
 
dba123Author 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'.

0
 
dba123Author 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'.

0
 
dba123Author 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.
0
 
dba123Author Commented:
0
 
dba123Author Commented:
Thank God, literally!
0
 
dba123Author Commented:
this was driving me batty.  I had the namespace imported....ahh but it's so picky in 2.0
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.