Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vs 2010 reading configuration settings

Coding in VB.net in VS 2010. I have:

Imports System.Configuration and I added a reference to System.Configuration.

When

**MsgBox(ConfigurationManager.AppSettings("sDBName").ToString)**

is executed, it fails, with "Object reference not set to an instance of an object." sDBName is set.

What have I missed?


In response:

Sorry for the delay in getting back to you; other things demanded my attention.

There is no such section in my app.config file. I added sDBName and other settings via the Settings1.settings file; these objects automatically show up in app.cong as follows:

<applicationSettings>
    <QuickRequest.Settings1>
        <setting name="sDBName" serializeAs="String">
            <value>xxx</value>
        </setting>
        <setting name="sInputPath" serializeAs="String">
            <value>c:\yyy\Infile\</value>
        </setting>
     </QuickRequest.Settings1>

like image 519
DeveloperM Avatar asked Dec 03 '25 16:12

DeveloperM


2 Answers

You reference settings in VB slightly differently than you do in C#. The easiest way is to use the settings that are part of the project and then reference it through the My namespace:

MessageBox.Show(My.Settings.sDBName)

(Note, you don't need the .ToString here because sDBName is already a string).

Since you are including a separate Settings file, you should be able to access its values by calling the Default method to get the instance and then your property off of the default:

MessageBox.Show(Settings1.Default.sDBName)
like image 81
Jim Wooley Avatar answered Dec 07 '25 15:12

Jim Wooley


You've said that sDBName is set: Is the sDBName setting included in the config of your main application, or just in the config of the assembly that the code is contained in?

It needs to be in the config of the entrypoint assembly as this is the config loaded when your application starts.

If it is set there, then you should post your config so that we can see the setting to check for problems.


Update in response to comment:

In the app.config, the setting should appear in the appSettings section, for example:

  <appSettings>
    <add key="sDBName" value="devDB"/>
  </appSettings>
like image 31
Jon Egerton Avatar answered Dec 07 '25 17:12

Jon Egerton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!