Solved

NullReferenceException Unhandled when trying to use ConfigurationManager

Posted on 2008-10-01
3
845 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
Comment Utility
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
Comment Utility
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
Comment Utility
have you tried my code..it works for me.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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 …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

15 Experts available now in Live!

Get 1:1 Help Now