Solved

NullReferenceException Unhandled when trying to use ConfigurationManager

Posted on 2008-10-01
3
875 Views
Last Modified: 2012-05-05
I'm trying to write a windows application that can retrieve a persistant setting I want to store in a setting file.  I believe I want to use configurationmanager.  When I run it I'm receiving the following

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="retrieveProperties"
  StackTrace:
       at retrieveProperties.DALText.GetProperties() in C:\TroyTemp\retrieveProperties\retrieveProperties\DALText.vb:line 20
       at retrieveProperties.Module1.Main() in C:\TroyTemp\retrieveProperties\retrieveProperties\Module1.vb:line 7
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()


This is my first time and I'm likely doing something very simple wrong.
Imports System.Configuration
Public Class DALText
    Public Function GetProperties() As ArrayList
        Dim sSourceFile As String
        sSourceFile = System.Configuration.ConfigurationManager.AppSettings("SourceFile").ToString
 
    End Function
End Class
 
     ''Here is the contents of my app.config file
 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="retrieveProperties.PropertiesSettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <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>
    <userSettings>
        <retrieveProperties.PropertiesSettings>
            <setting name="SourceFile" serializeAs="String">
                <value>"C:\TroyTemp\RawProperties.txt"</value>
            </setting>
        </retrieveProperties.PropertiesSettings>
    </userSettings>
</configuration>

Open in new window

0
Comment
Question by:bminetwork2277
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22621628
check this out:
 

--XML file
<userSettings>
    <WindowsApplication3.My.MySettings>
      
      <setting name="SourceFile" serializeAs="String">
        <value>"C:\TroyTemp\RawProperties.txt"</value>
      </setting>
    </WindowsApplication3.My.MySettings>
  </userSettings>
 
-- vb.net code
 
 Dim sett As My.MySettings
        sett = WindowsApplication3.My.Settings
        Dim sSourceFile As String
        sSourceFile = sett("SourceFile")

Open in new window

0
 
LVL 1

Author Comment

by:bminetwork2277
ID: 22647193
I am now getting
System.Configuration.SettingsPropertyNotFoundException was unhandled
  Message="The settings property 'SourceFile' was not found."
My Code is as follows

        Dim sett As My.MySettings
        sett = retrieveProperties.My.Settings
        Dim sSourceFile As String
        sSourceFile = sett("SourceFile")

My XML is as follows

      <userSettings>
            <retrieveProperties.My.MySettings>
                  <setting name="SourceFile" serializeAs="String">
                        <value>"C:\TroyTemp\RawProperties.txt"</value>
                  </setting>
            </retrieveProperties.My.MySettings>
      </userSettings>
0
 
LVL 13

Accepted Solution

by:
SameerJagdale earned 500 total points
ID: 22648802
have you tried my code..it works for me.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

617 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