?
Solved

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

Posted on 2006-03-20
14
Medium Priority
?
2,237 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
0
Comment
Question by:dba123
  • 10
  • 4
14 Comments
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 16237079
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
 
LVL 1

Author Comment

by:dba123
ID: 16237102
System.Configuration - asp.net 1.1
0
 
LVL 1

Author Comment

by:dba123
ID: 16237107

System.Configuration.ConfigurationManager.AppSettings

Error      2      'ConfigurationManager' is not a member of 'Configuration'.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 16237153
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
 
LVL 1

Author Comment

by:dba123
ID: 16237202
it's not liking ConfigurationManager is my point...
0
 
LVL 1

Author Comment

by:dba123
ID: 16237211
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
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 16237317
Oh, man.

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

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

Sorry for the misunderstanding.
0
 
LVL 1

Author Comment

by:dba123
ID: 16237399
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
 
LVL 1

Author Comment

by:dba123
ID: 16237404
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
 
LVL 1

Author Comment

by:dba123
ID: 16237414
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
 
LVL 25

Accepted Solution

by:
DBAduck - Ben Miller earned 2000 total points
ID: 16237599
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
 
LVL 1

Author Comment

by:dba123
ID: 16237612
0
 
LVL 1

Author Comment

by:dba123
ID: 16237617
Thank God, literally!
0
 
LVL 1

Author Comment

by:dba123
ID: 16237619
this was driving me batty.  I had the namespace imported....ahh but it's so picky in 2.0
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question