all groups > vb.net controls > october 2006 >
You're in the

vb.net controls

group:

user control


user control Mihai Velicu
10/3/2006 4:52:39 AM
vb.net controls:
I tried to set the type of the database in a control that reside in a
control library.
I'm using Factory class.When I run the code in User control test container I
receive the error:
"Object reference not set to an instance of an object"
I don't know what is happening .If i try the same code but in a regular
project (from a windows form) is running but when i try this code from a
control that reside in a control library is not working.

The code is:

Imports System
Imports System.Data
Imports System.Data.Common
Imports System.Configuration

Public Class NrSystemForAccounts
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)

____________ here at this first line I have the error message : Object
reference not set to an instance of an object

mConnectionString =
ConfigurationManager.ConnectionStrings(DataBaseConnectionName).ConnectionString.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")
endsub

endclass

Can someone tell me what is happening ?How I can correct this code?

Thank you,
Mihai

Re: user control JerryWEC
10/11/2006 10:31:09 AM
While debugging your code look for which line of code is throwing the
exception. Hence, set break points at the beginning of your method and step
through each line of code.

This error/exception normally means you have not set an object to a new
instance of the type. Example: Dim myVar as String = New String()

or

String myVar = New String();

I don't see any New keywords being used make sure that all of your object
references are assigned a new instanance of the object.

JerryM

Re: user control Mihai
10/11/2006 3:19:40 PM
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>

[quoted text, click to view]

AddThis Social Bookmark Button