class library

M

Mihai Velicu

Hi !

I created a class library and in it I have a class that call
"ConfigurationManager" to find out what type of database I work and the
connection string.When I call this class from a form, everithing is running
well, no errors.When I call this class from a custom control I receive the
message "Object reference not set to an instance of an object"
Does anyoane know what is happening ?

Thanks,
Mihai
 
C

Cor Ligthert [MVP]

Mihai,

You have somewhere not instanced an object, try to find that in your code.
For us that is impossible, this newsgroup has not the possibility to look
through that video camera on your head.

Cor
 
M

Mihai Velicu

So this line work perfectly if from a form I call the class that contain
this line
mConnectionString =
ConfigurationManager.ConnectionStrings(DataBaseConnectionName).ConnectionString.ToString()

In a custom control that reside in a control library if i call the class
that reside in a library class and that has somewhere in a method this line,
is not working.Gave me that error message.

So don't be rude with comments like that one bellow.I tried a couple of
hours to see what is happening - debugging and serching in MSDN but
unfortunately with no results.That why I ask help on this newsgroup.

Regards,
Mihai
 
C

Cor Ligthert [MVP]

Mihai,

Don't you find your own question rude, asking something and giving not any
information. Is your configuration manager a shared class or has it to be
instanced, nothing that you show.

There should be a reason why it is not working, but even with this simple
code string we cannot see it, you suppose we start a kind of guessing
context. There is not a kind of wonder that protects it to be working in
this situation.

However as most people than reply, we don't have to anwer your questions, we
just can ignore it.

Cor
 
T

Tom Shelton

Mihai said:
Hi !

I created a class library and in it I have a class that call
"ConfigurationManager" to find out what type of database I work and the
connection string.When I call this class from a form, everithing is running
well, no errors.When I call this class from a custom control I receive the
message "Object reference not set to an instance of an object"
Does anyoane know what is happening ?

Thanks,
Mihai

Mihai,

Can you give show us your app.config (obviously, change any sensitive
data :)... And maybe the method where you are calling this.

Thanks,
 
M

Mihai

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>
 
G

Guest

Hi Mihai,

If you're trying to access data from an app.config file from a Class Library
(*.dll), the name of the config file must be the name of the callling
assembly.

For example say I have a Windows Forms EXE app called "MyProgram.exe" which
references a class library called "MyLibrary.dll". Inside my library, I make
the call:

mConnectionString =
ConfigurationManager.ConnectionStrings(DataBaseConnectionName).ConnectionStr
ing.ToString()

Now, if the config file is not name "MyProgram.exe.config" (it should NOT be
named "MyLibrary.dll.config"), or if the connection string does not exist in
the file, a NULL (Nothing) value will be returned at this portion of the
statement:

ConfigurationManager.ConnectionStrings(DataBaseConnectionName)

Now since this part of the statement is returning Nothing, calling the
".ConnectionString.ToString()" part will throw the exception you are seeing.
I would recommend doing something like this:

Dim connStrings As ConnectionStringSettingsCollection
connStrings = ConfigurationManager.ConnectionStrings

Dim conStr As ConnectionStringSettings
conStr = connStrs(DataBaseConnectionName)

If conStr Is Nothing then
Throw New Exception("App.config or connection string not found")
end If

If the exception gets thrown through the if statement check, then there's a
good change the DLL can not find the app.config file. Make sure your
connection strings are defined in the App.config file of the Calling Assembly
(of the .exe project).
 
M

Mihai Velicu

Thank you very much for you answer.I disturb you once again.Please tell me
in what book I can find what you told me because belive me or not a read a
lot and it seems for nothing.The usualy books don't have any reference to
something like that.

Thank you very much again,
Maihai
 
G

Guest

Honestly, a person can not learn to be a good troubleshooter and a good
programmer by reading books. Actually coding, debugging, making mistakes,
and learning from those mistakes is what makes a good coder. Experience
will always be the best teacher. It just takes some time. I hope this is
something you have learned and will remember the next time something like
this comes up.

Good luck!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

strange error message 3
references 1
Implements IDisposable 5
Change cursor in a Class Library 4
Simple class library (dll) example? 3
Inheritance and COM 1
How to properly "name" a class library? 1
dll path 7

Top