• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

ConfigurationSettings.AppSettings returns empty - can anyone help? (Urgent)

Hi,

this is kind of an urgent requirement. Sorry if this is vague but this is all the information i have available as the other person is at a remote location and has only given me these details. He is trying to retieve some appsettings in the config file in VB.NET and is getting an error with these symptoms:

symptoms: ConfigurationSettings.AppSettings returns empty

This all the info i have so is anyone able to provide a either a soltuion (well done if you can) or point me in the right direction.

Thanks you any help
0
cbridle
Asked:
cbridle
3 Solutions
 
adatheladCommented:
The web.config file should contain a section like this:

<appSettings>
   <add key="ConfigKey1" value="SomeValue"/>
</appSettings>

I'm presuming you already have a list of keys defined as per my example above. If not, then this is your problem - you need to add them.
If you do have these, then you need to specifically request the setting you want:
e.g.
Dim strConfigValue as string = ConfigurationSettings.AppSettings("ConfigKey1")  -- VB.NET

I'm not 100% but the key you are looking for may be case-sensitive (i.e. "configKey1" might return blank)
0
 
iboutchkineCommented:
check this
http://support.microsoft.com/directory/article.asp?ID=kb;en-us;Q313405&SD=MSDN

If you are using Visual Studio .NET then the best way to create your config file is to add an Application
Configuration item to your Project. ie. Right click your project, go to ADD>Add New Item..> Application
Configuration File..
This will create a App.Config file along side your files in the project.


Create Console Application That Reads Content of Configuration File
-----------------------------------------------------------------
You can store application settings within the configuration file that is associated with the application.
Configuration files are saved in XML format. The System.Configuration and
the System.Collections.Specialized namespaces in the Microsoft .NET Framework include the necessary
classes to retrieve information from a .NET application configuration file during run time.



In Solution Explorer, click Show All Files .
Right-click Bin , and then click Include in Project .
Right-click the Bin folder, point to Add , and then click Add New Item .
In the Add New Item dialog box, select the Application Configuration File template, click Open , and then
add the App.config file to the project.

NOTE : Application configuration files must reside in the same folder as the executable file that they are
associated with. Because Visual Studio .NET places executable files in the Bin folder, you must also place
the configuration file in the Bin folder.


You must name the application configuration file according to the following format:
ApplicationName . ApplicationType .config
In Solution Explorer, right-click App.config , and then click Rename . Rename the App.config file
ConfigurationFile.exe.config . The associated application can now locate this .config file.


Configuration file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
      <appSettings>
            <add key="Key0" value="0" />
            <add key="Key1" value="1" />
            <add key="Key2" value="2" />
      </appSettings>
</configuration>


App (ConfigurationFile)

Imports System.Configuration
Imports System.Collections.Specialized

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Dim sAttr As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        sAttr = ConfigurationSettings.AppSettings("Key0")
        MsgBox("The value of Key0: " & sAttr)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim sAll As NameValueCollection
        Dim msg As String

        sAll = ConfigurationSettings.AppSettings()

        Dim s As String
        For Each s In sAll.AllKeys

            msg &= "Key: " & s & " Value: " & sAll(s) & vbCrLf
        Next
        MsgBox(msg)


    End Sub
End Class
0
 
YZlatCommented:
also make sure key and value are all lower-case and not "Key" or "Value"
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now