Hi Folks,

I am designing a windows service which will query a database and send out automated emails on a daily basis. So far the service can send out automated emails, however when I tried to query the database it failed, despite the fact I used the same code that I had used within an .aspx page of my web application. I am usure whether i can use features such as a dataset within a windows service.

Any hints or tips would be greatly appreciated

Regards

Grant

Working Automated Emails Code

VB Code:
  1. Option Explicit On
  2. Option Strict On
  3.  
  4. Imports System.ServiceProcess
  5. Imports System.Xml
  6. Imports System.Text
  7. Imports System.IO
  8. Imports System.Diagnostics
  9. Imports System.Data
  10. Imports System.Web.Mail
  11. Imports Microsoft.VisualBasic.ControlChars
  12. Imports System.Timers
  13. Imports System.Data.SqlClient
  14.  
  15.  
  16.  
  17. Public Class Service1
  18.     Inherits System.ServiceProcess.ServiceBase
  19.  
  20. #Region " Component Designer generated code "
  21.  
  22.     Public Sub New()
  23.         MyBase.New()
  24.  
  25.         ' This call is required by the Component Designer.
  26.         InitializeComponent()
  27.  
  28.         ' Add any initialization after the InitializeComponent() call
  29.  
  30.     End Sub
  31.  
  32.     'UserService overrides dispose to clean up the component list.
  33.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  34.         If disposing Then
  35.             If Not (components Is Nothing) Then
  36.                 components.Dispose()
  37.             End If
  38.         End If
  39.         MyBase.Dispose(disposing)
  40.     End Sub
  41.  
  42.     ' The main entry point for the process
  43.     <MTAThread()> _
  44.     Shared Sub Main()
  45.         Dim ServicesToRun() As System.ServiceProcess.ServiceBase
  46.  
  47.         ' More than one NT Service may run within the same process. To add
  48.         ' another service to this process, change the following line to
  49.         ' create a second service object. For example,
  50.         '
  51.         '   ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
  52.         '
  53.         ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}
  54.  
  55.         System.ServiceProcess.ServiceBase.Run(ServicesToRun)
  56.     End Sub
  57.  
  58.     'Required by the Component Designer
  59.     Private components As System.ComponentModel.IContainer
  60.  
  61.     ' NOTE: The following procedure is required by the Component Designer
  62.     ' It can be modified using the Component Designer.  
  63.     ' Do not modify it using the code editor.
  64.     Friend WithEvents Timer1 As System.Timers.Timer
  65.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  66.         Me.Timer1 = New System.Timers.Timer
  67.         CType(Me.Timer1, System.ComponentModel.ISupportInitialize).BeginInit()
  68.         '
  69.         'Timer1
  70.         '
  71.         Me.Timer1.Enabled = True
  72.         Me.Timer1.Interval = 10000
  73.         '
  74.         'Service1
  75.         '
  76.         Me.ServiceName = "Service1"
  77.         CType(Me.Timer1, System.ComponentModel.ISupportInitialize).EndInit()
  78.  
  79.     End Sub
  80.  
  81. #End Region
  82.  
  83.     Protected Overrides Sub OnStart(ByVal args() As String)
  84.         ' Add code here to start your service. This method should set things
  85.         ' in motion so your service can do its work.
  86.         Timer1.Enabled = True
  87.  
  88.  
  89.  
  90.  
  91.     End Sub
  92.  
  93.     Protected Overrides Sub OnStop()
  94.         ' Add code here to perform any tear-down necessary to stop your service.
  95.         Timer1.Enabled = False
  96.     End Sub
  97.  
  98.     Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
  99.  
  100.         Working()
  101.  
  102.     End Sub
  103.  
  104.     Private Sub Working()
  105.  
  106.         ' Local variables
  107.         Dim sFrom As String = String.Empty
  108.         Dim sTo As String = String.Empty
  109.         Dim sCc As String = String.Empty
  110.         Dim sBcc As String = String.Empty
  111.         Dim sSubject As String = String.Empty
  112.         Dim sBody As String = String.Empty
  113.         Dim nSleep As Integer = 15
  114.         Dim nPause As Integer = 1000
  115.  
  116.         'Create a var. named rightNow and set it to the current date/time
  117.         Dim rightNow As DateTime = DateTime.Now
  118.         Dim s As String 'create a string
  119.  
  120.         s = rightNow.ToString("dd/MM/yy")
  121.  
  122.  
  123.         'Database
  124.         Dim testvariable As String = String.Empty
  125.  
  126.         ' Enter the infinite loop
  127.         Try
  128.             ' First, sleep for a while
  129.             'mWorker.Sleep(nSleep * 60 * 1000)
  130.             'mWorker.Sleep(2000)
  131.             ' Woke up
  132.             'LogInfo("Service woke up")
  133.             Dim i As Integer = 0
  134.             ' Prepare e-mail fields
  135.             sFrom = "[email protected]"
  136.             sTo = "[email protected]"
  137.                        Dim oMailMsg As MailMessage = New MailMessage
  138.             oMailMsg.From = sFrom
  139.             oMailMsg.To = sTo
  140.             oMailMsg.Cc = sCc
  141.             ' Call a stored procedure to process the current item
  142.             ' The success message
  143.             oMailMsg.Subject = sSubject + "Emergency Response Rota"
  144.             oMailMsg.BodyFormat = MailFormat.Html
  145.  
  146.             oMailMsg.Body = sBody + "<HTML><BODY>Dear All,<BR><BR>The Rota for the week beginning the " + s + " is as follows: <br>" + testvariable + "</BODY></HTML>"
  147.             SmtpMail.SmtpServer = "mailhost.wfc.domain.net"
  148.  
  149.  
  150.             ' Send the message
  151.             If Not (oMailMsg.To = String.Empty) Then
  152.                 SmtpMail.Send(oMailMsg)
  153.             End If
  154.             ' Pause to avoid hogging the CPU
  155.             'mWorker.Sleep(nPause)
  156.         Catch obug As Exception
  157.             'LogEvent(obug.Message)
  158.         Finally
  159.  
  160.         End Try
  161.  
  162.     End Sub
  163.  
  164. End Class