window service

N

naveen babu

hi,
i have built a window service to send an email alert..

when i start the service..i get the following msg

The ScheduleAlert service on Local Computer started and then stopped.
Some services stop automatically if they have no work to do, for example
the Performance Logs and Alerts service.

when i check my event log i have teh following error msg

Service cannot be started. System.InvalidCastException: Specified cast
is not valid.
at ScheduleAlert.ScheduleAlert.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
state)

any help would be appreciated.

i am getting the email....but the service is stopped
 
A

Alberto Poblacion

naveen babu said:
i have built a window service to send an email alert..

when i start the service..i get the following msg

The ScheduleAlert service on Local Computer started and then stopped.
Some services stop automatically if they have no work to do, for example
the Performance Logs and Alerts service.

when i check my event log i have teh following error msg

Service cannot be started. System.InvalidCastException: Specified cast
is not valid.
at ScheduleAlert.ScheduleAlert.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
state)

any help would be appreciated.

i am getting the email....but the service is stopped

Well, the event log is quite clear. You have a mistake in your OnStart
method: It is performing an invalid cast. Since you get the email, the error
is somewhere in your code below the part that sends the message.
 
C

csharp 22

hi,
tnx for your reply...as i said i am pretty new to windows service
...this is my first one

can you plz guide me where the error is ....this is my onstart event

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
SqlConnection stCon = new SqlConnection("Data
Source=.;Initial Catalog=abcd;User ID=sa;Password = abcd");
SqlDataAdapter da = new SqlDataAdapter("select * from
ActionItems", stCon);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
DateTime dueDate = (DateTime)dr["Action_Duedate"];
DateTime now = DateTime.Now;
if((dueDate.Day == now.Day) && (dueDate.Month ==
now.Month) && (dueDate.Year == now.Year))
{
sendMail(dr);
}
}
ServiceController[] services =
ServiceController.GetServices();
foreach (ServiceController x in services)
{
if (x.DisplayName == "ScheduleAlert")
{
if (x.Status ==
System.ServiceProcess.ServiceControllerStatus.Running)
{
x.Stop();
}
else
{
x.Start();
}
}
}
}


thank you once again
 
A

Alberto Poblacion

csharp 22 said:
can you plz guide me where the error is ....this is my onstart event

I don't see anything in that code that could throw an "Invalid cast"
after executing sendMail(dr). Maybe the error is inside the sendMail
subroutine, which you must have written elsewhere?
You could experiment surrounding that line with a try..catch to find out
if it is indeed throwing an exception. Or you could connect the debugger to
the Service (do an internet search for this, there are plenty of documents
explaining how to do it) and execute your code step by step to find the
problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top