Solved

NullReferenceException Unhandled when trying to use ConfigurationManager

Posted on 2008-10-01
3
861 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
  • 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Advice in Xamarin 21 80
Gridview selected row 9 47
Creating a route in asp.net webforms 2 28
Database Connections Not being returned to Connection Pool 7 27
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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