473,326 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Service start issue on Computer restart

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
Nov 21 '05 #1
10 1872
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

Nov 21 '05 #2
It is possible that your service is being started before SQL server is.
You can set you service to depend on other services being started
first. You might look into that.

Nov 21 '05 #3
you need to verify that you have a network connection.
If the service is starting before the network is up and running you will get
an execption error.

if you can not figure it out... a hoky way around it is
..
when the service is started.. put a timer that pauses it for 5 mins.
Private tmr As New System.Timers.Timer(5000)

Private tmrstart As DateTime

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

tmrstart = Now

AddHandler tmr.Elapsed, AddressOf tmr_tick

tmr.Start()

End Sub

Private Sub tmr_tick(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)

If (DateDiff(DateInterval.Minute, Now, DateAdd(DateInterval.Minute, 5,
tmrstart), FirstDayOfWeek.Sunday) <= 0) Then

MsgBox("5 mins up")

tmr.Stop()

'you need to put your function call here

End If

End Sub

Like i said it is hoky as hell but it will get you around your problem until
you can figure it out.

It seems really strange that your getting an execption error. It has to be
that your calling something that is not there yet.
If you were calling database and it has not been started yet you would be
getting an unable to connect so that is not the issues.

It seems to be happening before you get to that issue

Post you service startup code. That might help

Chris

"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:Ok*************@TK2MSFTNGP14.phx.gbl...
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


Nov 21 '05 #4
Append to last post... You should error out on cn.open.
Does not matter if sql is started or not that would not cause an execption
unless he is trying to fill a an adapter.

but then you have the problem that you will error out on connection open if
you can not connect.

So i belive that it is happening when he is trying to create the ...... Umm
hard to say
could be oledb object for framework is not built yet.

What I do know.. Is that your trying to use an object that you can not use
yet


"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
It is possible that your service is being started before SQL server is.
You can set you service to depend on other services being started
first. You might look into that.

Nov 21 '05 #5
One more thing to... about the code i posted
Instead of waiting for 5 min. You can try to hit the database. so you can
use the below if you wish too
Private tmr As New System.Timers.Timer(5000)

Private tmrstart As DateTime

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

tmrstart = Now

AddHandler tmr.Elapsed, AddressOf tmr_tick

tmr.Start()

End Sub

Private Sub tmr_tick(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)

''' you can put code here to see if you can create objects 'and not go on
until you have all your object creates
'so as follows
if((obj1 is not nothing) and (obj2 is not nothing))then
tmr.Stop()
call you function
end if
End Sub

that will allow you to check to see if your objects are created before you
go on.. You could also put a timer there to see if objects are not created
in 5 mins to shut down the service and send an alert email to the
administrator of the software.



"amirmira" <am******@discussions.microsoft.com> wrote in message
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

Nov 21 '05 #6
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


Nov 21 '05 #7
Hi Chris,
I don't think that SQL Server service is an issue. The box that I am testing
the service on does not have SQL Server installed. It only has the SQL Server
connectivity tools.

Thanks anyways.

Ajay Mirmira

"Chris Dunaway" wrote:
It is possible that your service is being started before SQL server is.
You can set you service to depend on other services being started
first. You might look into that.

Nov 21 '05 #8
Hi Chris,
Instead of waiting for 5 minutes, can I not list the network service (and by
this I mean the RPC service) as a dependency to my service?

Thanks in advance.

Ajay Mirmira

"Chris Calzaretta" wrote:
One more thing to... about the code i posted
Instead of waiting for 5 min. You can try to hit the database. so you can
use the below if you wish too
Private tmr As New System.Timers.Timer(5000)

Private tmrstart As DateTime

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

tmrstart = Now

AddHandler tmr.Elapsed, AddressOf tmr_tick

tmr.Start()

End Sub

Private Sub tmr_tick(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)

''' you can put code here to see if you can create objects 'and not go on
until you have all your object creates
'so as follows
if((obj1 is not nothing) and (obj2 is not nothing))then
tmr.Stop()
call you function
end if
End Sub

that will allow you to check to see if your objects are created before you
go on.. You could also put a timer there to see if objects are not created
in 5 mins to shut down the service and send an alert email to the
administrator of the software.



"amirmira" <am******@discussions.microsoft.com> wrote in message
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


Nov 21 '05 #9
Carlos,
If this is a bug in my code, should I not see the error all the time? How is
the code execution different when I start the service manually and when the
service is started on a machine reboot.

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


Nov 21 '05 #10
Thanks you all for the support. I finally found the issue. Chris Dunaway was
right after all. The service depends on SQL Server to be started - if the SQL
Server is not started, then the error occurs. By some freak coincidence, SQL
Server was turned off when I was performing the reboot test - and it was
turned on when I started it manually.

I caught this condition when the service failed to start manually. Anyways,
I have added better error handling in my code - now I get the error "SQL
Server does not exist or access is denied". This is a more acceptable error
and allows me to troubleshoot.

Once again, thanks to you all.

Ajay Mirmira
"amirmira" wrote:
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

Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mrinal Kamboj | last post by:
Hi , Doubt may be bit trivial , but i wanted to know , what are the exact benefits derived while we run a background process as an NT service in windows , we can as well achieve the...
14
by: iceman | last post by:
Hello, I have a windows service. I want to restart it after every 24 hour. Is it possible to restart the service programmatically(from the service itself) using the sercvice controller object?...
6
by: Leonardo Curros | last post by:
Hello, I would like to know what's the best way to restart one service. I would like to do it from the service itself. Is this possible? I try it with ServiceController.stop()...
1
by: Sergey Krutous | last post by:
Can you please help me to resolve the following issue: I developed a windows service. At start up (I have overriden OnStart method) it connects to a web service and if the connection fails the...
0
by: Chung Leong | last post by:
Here's a short tutorial on how to the OLE-DB extension to access Windows Indexing Service. Impress your office-mates with a powerful full-text search feature on your intranet. It's easier than you...
4
by: kkt49 | last post by:
# vim: et sw=4 ts=8 sts from wxPython.wx import * import sys, os, time import pywintypes import win32serviceutil import win32service import win32event import win32process
7
by: shai | last post by:
I am working at .net 1.1, writing in c#. I have windows service with a COM object. Every unexpected time The COM object throw an error that make my service get stuck (do not respond). I can catch...
4
by: carson | last post by:
I have written two windows services: - service A does some crunching of local data files and uploads them to a central processing computer via http. - service B monitors a manifest file on a...
0
by: Shalako | last post by:
Today was this second time this happened. It occured at 12:00AM on 7/14 and today 7/18 according to the application event viewer. The event viewer error says: The Apache service named reported...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.