Results 1 to 5 of 5

Thread: [RESOLVED] Null Exception Error Using connection string in app.config

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Location
    Uk
    Posts
    157

    Resolved [RESOLVED] Null Exception Error Using connection string in app.config

    I've moved my Database connection string into my app.config - however its causing a null exception error when i use it

    App.Config
    vb Code:
    1. <connectionStrings>
    2.       <add name="MyData" connectionString="Provider=Microsoft.Jet.Oledb.4.0;Data Source=MyData.mdb;Jet OLEDB:Database Password=123456789"
    3.     providerName="System.Data.OleDb" />
    4.     </connectionStrings>

    I am trying to connect to it using
    vb Code:
    1. Dim con6 As New OleDbConnection(ConfigurationManager.ConnectionStrings("MyData").ConnectionString)

    But that crashes with a null exception error in the JIT debugger "object reference not set to an instance of an object"

    However if i move the connection string to a global module class called "Strings"
    vb Code:
    1. 'db connection
    2.     Friend DBConnectstrings = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=MyData.mdb;Jet OLEDB:Database Password=123456789"

    and call it using
    vb Code:
    1. Dim con6 As New OleDbConnection(Strings.DBConnectstrings)

    It works fine

    I've added the reference systems.configuration to my application
    And i've added 'imports system.configuration'

    Any idea what i'm doing wrong?

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Null Exception Error Using connection string in app.config

    Go to your project properties, under the settings table there will be a grid with Name, Type, Scope and value. Make sure your setting is listed as

    Code:
    Name    Type             Scope        Value
    MyData (ConnectionString) Application Provider=Microsoft.Jet.Oledb.4.0;Data Source=MyData.mdb;Jet OLEDB:Database Password=123456789
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Location
    Uk
    Posts
    157

    Re: Object reference not set to an instance of an object

    I checked my settings but still having no luck.
    I've been playing with this on and off for a week and still cant get it to work.
    I've created a new test project and still having the same issues
    I'm receiving the error
    Object reference not set to an instance of an object
    Form1:
    vb Code:
    1. Imports System.Configuration
    2. Imports System.Data.OleDb
    3.  
    4. Public Class Form1
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Try
    7.             Dim Test As String
    8.             Dim con As New OleDbConnection
    9.             Dim str As String = ConfigurationManager.ConnectionStrings("DB").ConnectionString
    10.             con.ConnectionString = str
    11.             Dim sql As String
    12.             sql = "SELECT Field1 from Data WHERE ID = 1"
    13.             Dim adapter As New OleDbDataAdapter(sql, con)
    14.             Dim dt As New DataTable("Data")
    15.             adapter.Fill(dt)
    16.             Test = dt.Rows(0)("Field1").ToString()
    17.             con.Close()
    18.             If Test = 1 Then
    19.                 MessageBox.Show("Success")
    20.             Else
    21.                 MessageBox.Show("Failed")
    22.             End If
    23.         Catch ex As Exception
    24.             MessageBox.Show(ex.Message)
    25.         End Try
    26.     End Sub
    27. End Class

    App Config:
    vb Code:
    1. <?xml version="1.0" encoding="utf-8" ?>
    2. <configuration>
    3.     <configSections>
    4.     </configSections>
    5.     <connectionStrings>
    6.         <add name="WindowsApplication1.My.MySettings.DB" connectionString="Provider=Microsoft.Jet.Oledb.4.0;Data Source=TestDB.mdb" />
    7.     </connectionStrings>
    8.     <system.diagnostics>
    9.         <sources>
    10.             <!-- This section defines the logging configuration for My.Application.Log -->
    11.             <source name="DefaultSource" switchName="DefaultSwitch">
    12.                 <listeners>
    13.                     <add name="FileLog"/>
    14.                     <!-- Uncomment the below section to write to the Application Event Log -->
    15.                     <!--<add name="EventLog"/>-->
    16.                 </listeners>
    17.             </source>
    18.         </sources>
    19.         <switches>
    20.             <add name="DefaultSwitch" value="Information" />
    21.         </switches>
    22.         <sharedListeners>
    23.             <add name="FileLog"
    24.                  type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
    25.                  initializeData="FileLogWriter"/>
    26.             <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
    27.             <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
    28.         </sharedListeners>
    29.     </system.diagnostics>
    30. </configuration>

    I've also attached the new test project in hope someone can see what i'm doing wrong
    Attached Files Attached Files

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Null Exception Error Using connection string in app.config

    In Config file you have

    <add name="WindowsApplication1.My.MySettings.DB" connectionString="Provider=Microsoft.Jet.Oledb.4.0;Data Source=TestDB.mdb" />
    and the name is WindowsApplication1.My.MySettings.DB and you are trying to find a name called "DB" which is causing the issue. Change the WindowsApplication1.My.MySettings.DB to DB in config file
    Please mark you thread resolved using the Thread Tools as shown

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Location
    Uk
    Posts
    157

    Re: [RESOLVED] Null Exception Error Using connection string in app.config

    Thank god for that - changed the name as you suggested and it now works
    Many thanks for your help guys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width