468,511 Members | 1,546 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,511 developers. It's quick & easy.

class library

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
Oct 10 '06 #1
8 2149
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

"Mihai Velicu" <bv*****@telus.netschreef in bericht
news:ihCWg.6769$P7.2840@edtnps89...
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

Oct 10 '06 #2
So this line work perfectly if from a form I call the class that contain
this line
mConnectionString =
ConfigurationManager.ConnectionStrings(DataBaseCon nectionName).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
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:Om*************@TK2MSFTNGP03.phx.gbl...
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

"Mihai Velicu" <bv*****@telus.netschreef in bericht
news:ihCWg.6769$P7.2840@edtnps89...
>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


Oct 10 '06 #3
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

"Mihai Velicu" <bv*****@telus.netschreef in bericht
news:uaFWg.4579$P7.1012@edtnps90...
So this line work perfectly if from a form I call the class that contain
this line
mConnectionString =
ConfigurationManager.ConnectionStrings(DataBaseCon nectionName).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
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:Om*************@TK2MSFTNGP03.phx.gbl...
>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

"Mihai Velicu" <bv*****@telus.netschreef in bericht
news:ihCWg.6769$P7.2840@edtnps89...
>>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



Oct 10 '06 #4

Mihai Velicu wrote:
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,

--
Tom Shelton

Oct 10 '06 #5
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(DataBaseCon nectionName).ConnectionStr
ing.ToString()
mFactoryName =
ConfigurationManager.ConnectionStrings(DataBaseCon nectionName).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.FileLogTraceLi stener,
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>

"Mihai Velicu" <bv*****@telus.netwrote in message
news:ihCWg.6769$P7.2840@edtnps89...
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


Oct 11 '06 #6
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(DataBaseCon nectionName).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(DataBaseCon nectionName)

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).

"Mihai" wrote:
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(DataBaseCon nectionName).ConnectionStr
ing.ToString()
mFactoryName =
ConfigurationManager.ConnectionStrings(DataBaseCon nectionName).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.FileLogTraceLi stener,
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>

"Mihai Velicu" <bv*****@telus.netwrote in message
news:ihCWg.6769$P7.2840@edtnps89...
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


Oct 12 '06 #7
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

"rmacias" <rm*****@newsgroup.nospamwrote in message
news:17**********************************@microsof t.com...
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(DataBaseCon nectionName).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(DataBaseCon nectionName)

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).

"Mihai" wrote:
>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(DataBaseCo nnectionName).ConnectionStr
ing.ToString()
mFactoryName =
ConfigurationManager.ConnectionStrings(DataBaseCo nnectionName).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.FileLogTraceL istener,
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>

"Mihai Velicu" <bv*****@telus.netwrote in message
news:ihCWg.6769$P7.2840@edtnps89...
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




Oct 12 '06 #8
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!!

"Mihai Velicu" wrote:
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

"rmacias" <rm*****@newsgroup.nospamwrote in message
news:17**********************************@microsof t.com...
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(DataBaseCon nectionName).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(DataBaseCon nectionName)

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).

"Mihai" wrote:
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(DataBaseCon nectionName).ConnectionStr
ing.ToString()
mFactoryName =
ConfigurationManager.ConnectionStrings(DataBaseCon nectionName).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.FileLogTraceLi stener,
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>

"Mihai Velicu" <bv*****@telus.netwrote in message
news:ihCWg.6769$P7.2840@edtnps89...
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




Oct 12 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by syd | last post: by
4 posts views Thread by Brian Shannon | last post: by
5 posts views Thread by Rainer Queck | last post: by
reply views Thread by drawing in aspnet | last post: by
4 posts views Thread by Steve Baer | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.