[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:
<connectionStrings>
<add name="MyData" connectionString="Provider=Microsoft.Jet.Oledb.4.0;Data Source=MyData.mdb;Jet OLEDB:Database Password=123456789"
providerName="System.Data.OleDb" />
</connectionStrings>
I am trying to connect to it using
vb Code:
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:
'db connection
Friend DBConnectstrings = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=MyData.mdb;Jet OLEDB:Database Password=123456789"
and call it using
vb Code:
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? :confused:
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
1 Attachment(s)
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
Quote:
Object reference not set to an instance of an object
Form1:
vb Code:
Imports System.Configuration
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim Test As String
Dim con As New OleDbConnection
Dim str As String = ConfigurationManager.ConnectionStrings("DB").ConnectionString
con.ConnectionString = str
Dim sql As String
sql = "SELECT Field1 from Data WHERE ID = 1"
Dim adapter As New OleDbDataAdapter(sql, con)
Dim dt As New DataTable("Data")
adapter.Fill(dt)
Test = dt.Rows(0)("Field1").ToString()
con.Close()
If Test = 1 Then
MessageBox.Show("Success")
Else
MessageBox.Show("Failed")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
App Config:
vb Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="WindowsApplication1.My.MySettings.DB" connectionString="Provider=Microsoft.Jet.Oledb.4.0;Data Source=TestDB.mdb" />
</connectionStrings>
<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>
</configuration>
I've also attached the new test project in hope someone can see what i'm doing wrong :blush:
Re: Null Exception Error Using connection string in app.config
In Config file you have
Quote:
<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
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 :)