Thanks Carlos. I am posting my start up code.
On the service Startup, I have this code:
Protected Overrides Sub OnStart(ByVal args() As String)
Dim oEventLog As New EventLog("Application", ".", "MyService")
Try
moCluster = New Cluster("ApplicationName", NodeTimeOut)
moCluster.Connect()
Catch ae As ApplicationException
oEventLog.WriteEntry(String.Concat("Source: ", ae.Source, " Message: ",
ae.Message), EventLogEntryType.Error)
End Try
End Sub
The Cluster object's connect method has the following code:
Public Sub Connect()
Dim sDatabase As String
Dim sServer As String
Dim sUser As String
Dim sPassword As String
Dim bTrusted As Boolean
Dim oDB As sqlDB
Dim sSource As String
Try
sSource = "Attempting to connect to registry"
'connect to registry and get the information
GetDataFromRegistry(sServer, sDatabase, sUser, sPassword,
bTrusted)
sSource = "Attempting to create database object"
'create the db class
If bTrusted Then
oDB = New sqlDB(sServer, sDatabase)
Else
oDB = New sqlDB(sServer, sDatabase, sUser, sPassword)
End If
sSource = "Attempting to connect to database"
'attempt to connect to the database
If Not oDB.Open Then
WriteEvent("Connection to Database Failed",
EventLogEntryType.Error)
Throw New ApplicationError("Connect", "Connection to
Database Failed")
Else
msConnectionString = oDB.ConnectionString
End If
'close database connection
sSource = "Attempting to close database connection"
oDB.Close()
oDB = Nothing
Catch ex As Exception
'close connection if it is open
If Not (oDB Is Nothing) Then
If oDB.State <> ConnectionState.Closed Then
oDB.Close()
End If
oDB = Nothing
End If
'error - raise back to caller
Throw New ApplicationError(String.Concat(sSource, "::",
ex.Source), "Message::", ex.Message)
End Try
End Sub
The error I see in the event viewer is as follows:
System.ApplicationException: Source: Attempting to connect to
database::BDLBSVC Message: Object reference not set to an instance of an
object. at MyService.BDLBSVC.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMai nCallback(Object
state)
Since I do not see the "Attempting to close database connection" in the
error message, I beleive that the error occurs when an attempt to open the
connection is being made. The sqlDB is custom class that wraps common
database functions.
The NEW method of the sqlDB class creates the connection string and the Open
method opens a connection to the database. For the sake of completeness, I
will also post the code for the sqlDB's open method.
Public Overrides Function Open() As Boolean
Dim sqlConnection As New SqlConnection
Dim bReturnValue As Boolean = False
'set connection string
sqlConnection.ConnectionString = msConnectionString
'open connection to the database
Try
sqlConnection.Open()
bReturnValue = True
Catch ex As Exception
bReturnValue = False
End Try
'set the public member to the connection
Connection = CType(sqlConnection, Object)
'return true or false depending on the success of the connection
Return bReturnValue
End Function
Any insight into my problem is appreciated. I have also noticed that I do
not have this issue on the Windows XP box (my development environment) - the
service starts automatically on a reboot - but the problem occurs on a
Windows 2000 server and Windows 2003 (both development servers) machine.
Again, I am thankful for your help.
Regards,
Ajay Mirmira
"Carlos J. Quintero [.NET MVP]" wrote:
Is the NullReferenceException thrown in your code (a bug that you must fix)
or inside the .NET Framework?
--
Carlos J. Quintero
MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
"amirmira" <am******@discussions.microsoft.com> escribió en el mensaje
news:D2**********************************@microsof t.com...I have a Windows Service developed in VB.NET that attempts to connect to a
database as soon as the service starts. I have no problem when I start the
service manually - but when I restart the computer that hosts the service,
I
get a Null Reference Exception when I try to connect to the database (I
have
set the service to start automatically). Any ideas why this is happening?
Thanks in advance.
Ajay Mirmira