Solved

NullReferenceException Unhandled when trying to use ConfigurationManager

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

21 Experts available now in Live!

Get 1:1 Help Now