I'm using Factory model to determine at runtime what kind of database I'm
using and to get the connection string from app.config.
I'm using the "ConfigurationManager " static class provided by .Net
framework 2.0.
I marked in the code where the error is. I repeat again, from a form
everithing is running properly only from a user control that reside in a
control library is the problem. It seems I have to declare somehow the
static method in the code .
because the ConfigurationManager is a static method I cannot use New....
If you want to help me
Create a control library and in it, a control and put this code and try to
execute and you can see the results I mean the error message.You have to add
reference to System.Configuration.
Bellow is the code and App.Config.
Thank you very much !
Mihai
The code is :
Imports System
Imports System.Data
Imports System.Data.Common
Imports System.Configuration
Public Class NrSystemForAccounts
'Private gts As GenericDb.GenericDbWrapper.GenericTableServices
Private dt As DataTable
Private mConnectionString As String
Private mFactoryName As String
Private mFactory As DbProviderFactory
Private mFactoryConnection As DbConnection
Private mFactoryCommand As DbCommand
Private mDataAdapter As DbDataAdapter
Private Sub SetFactory(ByVal DataBaseConnectionName As String)
!!!!!!!! The error is in the line bellow :"Object reference not set to an
instance of an object"
mConnectionString =
ConfigurationManager.ConnectionStrings(DataBaseConnectionName).ConnectionStr
ing.ToString()
mFactoryName =
ConfigurationManager.ConnectionStrings(DataBaseConnectionName).ProviderName.
ToString()
mFactory = DbProviderFactories.GetFactory(mFactoryName)
End Sub
Private Sub NrSystemForAccounts_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
Me.SetFactory("DatabaseConnection1")
End sub
End class
The App.config code is :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- This section defines the connections and database providers -->
<connectionStrings>
<add name="DatabaseConnection1"
connectionString="Persist Security Info=False;Integrated
Security=SSPI;database=Conta;server=(local);"
providerName="System.Data.SqlClient" />
<add name="DatabaseConnection2"
connectionString="" providerName="Oracle" />
</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>