Solved

How do I rewrite the following AppSettings vs2003 statement in vs2005?

Posted on 2006-10-25
13
577 Views
Last Modified: 2012-08-14
Hi,
How do I rewrite the following AppSettings vs2003 statement in vs2005?

Dim StrConn As String = ConfigurationSettings.AppSettings.Get("CN1Str")

Thanks
Mc
0
Comment
Question by:mcain_bba
  • 3
  • 3
  • 2
  • +2
13 Comments
 
LVL 7

Expert Comment

by:Vaxman2
ID: 17808589
Add a reference to system.configuration to the project
Add an Imports System.Configuration line
Change the line to:  Dim StrConn As String = ConfigurationManager.AppSettings.Get("CN1Str")


Imports System.Configuration

Module Module1
      Sub Main()
            Dim StrConn As String = ConfigurationManager.AppSettings.Get("CN1Str")
      End Sub
End Module
0
 
LVL 13

Accepted Solution

by:
newyuppie earned 500 total points
ID: 17809020
in vb 2005 you can use the MY namespace which is pretty useful

Dim StrConn As String = My.Settings.CN1Str
0
 
LVL 28

Expert Comment

by:strickdd
ID: 17811055
An easier way to reference the appsettings is:

ConfigurationSettings.AppSettings("CN1Str")

You don't have to use the Get method because it is an indexed array in the AppSettings. You can reference by index or key name.
0
 

Author Comment

by:mcain_bba
ID: 17811166
I have tried all three ways.  They each have value, but I must be missing something big, because each gives a different error message.  See below.  I do not think this makes a differance, but this is a vs2003 app that was coverted to vs2005.

Imports System.Data.OracleClient
Imports System.Configuration

Module ModFldDef
    Sub Main()
        Dim StrConn As String = ConfigurationManager.AppSettings.Get("CN1Str")
        'There is a blue line under ConfigurationManager and saids "ConfigurationManager is not declared"
    End Sub

    Dim StrConn As String = My.Settings.CN1Str
    'There is a blue line under My.Settings and saids "Settings is not a member of My"

ConfigurationSettings.AppSettings("CN1Str")
    'There is a blue line udner ConfigurationSettings and saids "Declaration expected"
End Module

Thanks
Mc
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17811758
i was the one who posted
Dim StrConn As String = My.Settings.CN1Str

this certainly is the easiest way to access setting in 2005.

the class My.Settings will be auto generated (much like a tableadapter class) whenever you create new settings in your project. i assume you already went to Project Properties/Settings and created the setting "CN1Str" that you intend to use in your app. if not, create it, save and compile, and you should get the My.Settings namespace.

NY
0
 
LVL 28

Expert Comment

by:strickdd
ID: 17812380
I thought you would know you have to set this value to a variable, but I guess I should have posted that. so try this:

Dim StrConn As String = System.ConfigurationSettings.AppSettings("CN1Str")

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mcain_bba
ID: 17812911
Newyuppie,
I do not think My.Settings will work because the data is dynamic.  The value of the string needs to change without recompiling.  I am using App.Config the change the data source to test or production.  


<appSettings>      
<add key="CN1Str" value="user id=ASNUSER;data source=FIRMTEST;password=xxxxx" />
</appSettings>


strickdd,
ConfigurationSettings.AppSettings("CN1Str") Works, but is obsolete in 2005

Thanks
Mc
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17813154
you can use my code to set and get the setting, unless you have set it to Application Scope in which case it will always be ReadOnly. Set it to UserScope and try it. did you try this or just saying?
0
 
LVL 7

Expert Comment

by:Vaxman2
ID: 17813317
The trick to get the code:

Sub Main()
        Dim StrConn As String = ConfigurationManager.AppSettings.Get("CN1Str")
        'There is a blue line under ConfigurationManager and saids "ConfigurationManager is not declared"
    End Sub

to work is to specifically add a reference to system.configuration to your references. You can have an import for system.configuration without it, but it can't resolve the call until it has the DLL reference.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 17813461
I'm sorry, I copied the wrong thing try:

Dim StrConn As String = System.ConfigurationManager.AppSettings("CN1Str")
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17854195
Hi mcain_bba ,

You need to get used to using my.settings if your using 2005 as what newyuppie explained earlier.

So using the following line would do the job, providing you have the property setting in the config file.
And you CAN update this in your application or when deployed via the config file.
dim m StrConn As String = My.Settings.CN1Str
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17994540
I think newyuppie should get the points.
Although all where right (ish), newyuppie's was the precise answer for this question.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Duplicate a row 2 46
Expando 4 46
Can Amazon AppStream 2.0 be used for VB.NET and VB6 applications? 2 75
Duplicating a tab page content to another in TabControl control 11 40
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now