473,795 Members | 3,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1912
Is the NullReferenceEx ception 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******@discu ssions.microsof t.com> escribió en el mensaje
news:D2******** *************** ***********@mic rosoft.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.T imer(5000)

Private tmrstart As DateTime

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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.E lapsedEventArgs )

If (DateDiff(DateI nterval.Minute, Now, DateAdd(DateInt erval.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*****@NOSPAM sogecable.com> wrote in
message news:Ok******** *****@TK2MSFTNG P14.phx.gbl...
Is the NullReferenceEx ception 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******@discu ssions.microsof t.com> escribió en el mensaje
news:D2******** *************** ***********@mic rosoft.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.goo glegroups.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.T imer(5000)

Private tmrstart As DateTime

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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.E lapsedEventArgs )

''' 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******@discu ssions.microsof t.com> wrote in message
news:D2******** *************** ***********@mic rosoft.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("Appli cation", ".", "MyService" )
Try
moCluster = New Cluster("Applic ationName", NodeTimeOut)
moCluster.Conne ct()
Catch ae As ApplicationExce ption
oEventLog.Write Entry(String.Co ncat("Source: ", ae.Source, " Message: ",
ae.Message), EventLogEntryTy pe.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
GetDataFromRegi stry(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("Con nection to Database Failed",
EventLogEntryTy pe.Error)
Throw New ApplicationErro r("Connect", "Connection to
Database Failed")
Else
msConnectionStr ing = oDB.ConnectionS tring
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 ApplicationErro r(String.Concat (sSource, "::",
ex.Source), "Message::" , ex.Message)
End Try

End Sub

The error I see in the event viewer is as follows:
System.Applicat ionException: Source: Attempting to connect to
database::BDLBS VC Message: Object reference not set to an instance of an
object. at MyService.BDLBS VC.OnStart(Stri ng[] args)
at System.ServiceP rocess.ServiceB ase.ServiceQueu edMainCallback( 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.C onnectionString = msConnectionStr ing

'open connection to the database
Try
sqlConnection.O pen()
bReturnValue = True
Catch ex As Exception
bReturnValue = False
End Try

'set the public member to the connection
Connection = CType(sqlConnec tion, 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 NullReferenceEx ception 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******@discu ssions.microsof t.com> escribió en el mensaje
news:D2******** *************** ***********@mic rosoft.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.T imer(5000)

Private tmrstart As DateTime

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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.E lapsedEventArgs )

''' 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******@discu ssions.microsof t.com> wrote in message
news:D2******** *************** ***********@mic rosoft.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 NullReferenceEx ception 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******@discu ssions.microsof t.com> escribió en el mensaje
news:D2******** *************** ***********@mic rosoft.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

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

Similar topics

4
2387
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 functionality using standalone exe , which will keep on running in a loop and do the job till been messaged to stop . Essentially , i want the key advantages of implementing process daemons as an NT Service in windows .
14
25132
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? Or should i chose some other approach? Any ideas? Thanx for all help. - Iceman
6
3808
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() ServiceController.WaitForStatus(ServiceControllerStatus.Stopped) ServiceController.start() but doesn´t works. It seems waitforstatus instruction is the last
1
3701
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 windows service sets ExitCode property to non-zero value (55) and calls Stop method. If this happens when user starts the service in Services snap-in SCM shows error massage and service stops - it's OK. I configured automatic startup and Recovery...
0
4239
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 think. First, download and install the extension (http://sourceforge.net/project/showfiles.php?group_id=171247&package_id=198554). Simply unzip the file and copy the correct version of php_oledb.dll into the PHP extensions folder. Then add the...
4
3182
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
7236
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 this error. I want to restart my windows service every time the COM object throws an error. I use System.ServiceProcess.ServiceController to stop and start my service. But there is one thing I do not understand:
4
21766
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 webserver to see if service A needs to be updated. What service B does if it sees their is an update for service A is to download a new copy of the service A executable, stop service A, replace the executable with the new copy, and start service B...
0
3296
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 the following error:>>> (22505)Access is denied. : Apache.exe: could not open error log file C:/Apache2/logs/error.log. So I try to start the service and I get this pop up box error: "Windows could not start the Apache2 on Local Computer. For more...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.