How we can handel this error coming in windows service System.ComponentModel.Win32Exception: Error

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Public Class R4
Private ProcessDesc As String = "Receiving Report"
Private elEvents As New EventLog
Private messtext As StringBuilder = New StringBuilder
Private action As String
Private logon As String
Private logonname As String
Private datein As String
Private timein As String
Private printer As String
Private email As String
Private customer As String
Private customerdesc As String
Private store As String
Private building As String
Private buildingdesc As String
Private company As String
Private whse As String
Private rcvtype As String
Private POnumber As String
Private pack As String
Private wr As String
Private conn As New SqlConnection
Public pathlog As String = App_Path() + "Log"
Public srandom As String

Public datasource As ReportDataSource
Public subdatasource As ReportDataSource
Public ParamList As New Generic.List(Of ReportParameter)
Private StreamsOfPrint As IList(Of Stream)
Private IndexCurrentPagePrint As Integer
Public ReportViewer1 As New ReportViewer
Public RndValue As Integer
Private sbTrace As StringBuilder

Public Sub Process_Action(ByVal State As Object)
Dim result As Char = "0"
elEvents.WriteEntry("Starting " & Me.building & "-" & Me.logon & "-" & Me.action & " Process..." + Now.ToLongTimeString(), EventLogEntryType.Information)
result = Set_Start()

If result = "0" Then Exit Sub
result = Print_Report()

If result = "0" Then Exit Sub

Set_End()

elEvents.WriteEntry("Ending " & Me.building & "-" & Me.logon & "-" & Me.action & " Process..." + Now.ToLongTimeString(), EventLogEntryType.Information)
End Sub

Public Function Print_Report() As Char
Dim mySqlCmd As New SqlCommand
Dim mySQLDA As New SqlDataAdapter
Dim myDS As New DataSet
Dim sql As String = ""
Dim namedataset As String = "DSRptReceiving_DSRptReceiving"
sbTrace = New StringBuilder()
Try
sbTrace.AppendLine("Start of R4.Print_Report()")
Print_Report = "0"
sbTrace.AppendLine("Set Print_Report = 0")

If conn.State = ConnectionState.Open Then conn.Close()
conn.ConnectionString = ConnStr
sbTrace.AppendLine("Connection String: " & ConnStr)
conn.Open()

GetParameters(conn, Me.customer)

sql = "sp_ac_rptreceiving " + Trim(Me.customer) + "," + Trim(Me.store) + "," + Trim(Me.building) + ","
sql += Trim(Me.rcvtype) + "," + Trim(Me.POnumber) + "," + Trim(Me.pack) + "," + Trim(Me.wr) + ""
mySqlCmd.CommandTimeout = 800
sbTrace.AppendLine("Connection Timeout: " & mySqlCmd.CommandTimeout)
mySqlCmd.Connection = Me.conn
mySqlCmd.CommandText = sql
sbTrace.AppendLine("Stored Procedure: " & sql.ToString)
mySQLDA.SelectCommand = mySqlCmd
mySQLDA.Fill(myDS, namedataset)

If myDS.Tables.Count = 0 Then
sbTrace.AppendLine("Table count in Dataset = 0 ")
messtext.Append("User Requesting Action: " & Me.logon & vbCrLf)
messtext.Append("Building: " & Me.building & " RcvType: " & Me.rcvtype & vbCrLf)
messtext.Append("PO Number: " & Me.POnumber & vbCrLf)
messtext.Append("Pack Slip: " & Me.pack & vbCrLf)
messtext.Append("WR: " & Me.wr & vbCrLf)
messtext.Append("Message: There is no data for this report" + vbCrLf)
messtext.Append("Date: " + Now.ToLongDateString() + vbCrLf)
messtext.Append("Time: " + Now.ToLongTimeString() + vbCrLf)
messtext.Append(vbCrLf & vbCrLf & "Message sent by WDSQEP Actions Services")
elEvents.WriteEntry(messtext.ToString(), EventLogEntryType.Warning)
sbTrace.AppendLine(messtext.ToString())
SendEmail(messtext.ToString(), Me.action + " - " + ProcessDesc + " (No data)", MailPriority.Normal, "01", Me.email, "")
Else
check row count in dataset table
If myDS.Tables(0).Rows.Count > 0 Then
Dim pathfilereport = App_Path() + "ReportsRptReceiving.rdlc"

sbTrace.AppendLine("Report path: " & pathfilereport)

ReportViewer1.Visible = False
ReportViewer1.LocalReport.ReportPath = pathfilereport
ParamList.Add(New ReportParameter("NameCustomer", Me.customerdesc))
ParamList.Add(New ReportParameter("NameStore", Me.buildingdesc))
ParamList.Add(New ReportParameter("NameUser", Me.logonname))
ParamList.Add(New ReportParameter("SubTitle0", "Receiving Report"))
ParamList.Add(New ReportParameter("SubTitle1", Me.customerdesc))
ParamList.Add(New ReportParameter("PO", Trim(Me.POnumber)))
ParamList.Add(New ReportParameter("BPO", "*" & Trim(Me.POnumber) & "*"))
ParamList.Add(New ReportParameter("PackSlip", Trim(Me.pack)))
ParamList.Add(New ReportParameter("BPS", "*" & Trim(Me.pack) & "*"))
ParamList.Add(New ReportParameter("WR", Trim(Me.wr)))
ParamList.Add(New ReportParameter("BWR", "*" & Trim(Me.wr) & "*"))
ParamList.Add(New ReportParameter("Vendor", Trim(myDS.Tables(0).Rows(0).Item("vendorid").ToString)))
ParamList.Add(New ReportParameter("lblvendor", Trim(myDS.Tables(0).Rows(0).Item("lblvendor").ToString)))
ParamList.Add(New ReportParameter("Company", Trim(Me.company)))
ParamList.Add(New ReportParameter("Whse", Trim(Me.whse)))
ParamList.Add(New ReportParameter("RcvType", Trim(Me.rcvtype)))
ParamList.Add(New ReportParameter("Printqty", Trim(myDS.Tables(0).Rows(0).Item("printqty").ToString)))
ReportViewer1.LocalReport.SetParameters(ParamList)
ReportViewer1.LocalReport.DataSources.Clear()
datasource = New ReportDataSource(namedataset, myDS.Tables(namedataset))
ReportViewer1.LocalReport.DataSources.Add(datasource)
ReportViewer1.LocalReport.Refresh()
ReportViewer1.PrintDialog()
srandom = GetRandomNumber().ToString
Dim pathfileexport As String = pathlog + "Recv" + "_" + Me.logon + "_" + srandom + "_" + Me.POnumber + "_" + Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & Now.Hour.ToString & Now.Minute.ToString & Now.Second.ToString & Now.Millisecond.ToString & ".pdf"

sbTrace.AppendLine("PDF Complete File path :" + pathfileexport)

sbTrace.AppendLine("Start of R4.Print_Report().Export(PDF)")
Export("PDF", pathfileexport)
sbTrace.AppendLine("End of R4.Print_Report().Export(PDF)")

sbTrace.AppendLine("Start of R4.Print_Report().Print()")
Print()
sbTrace.AppendLine("End of R4.Print_Report().Print()")
Else
sbTrace.AppendLine("No record found in dataset")
End If
SendEmail("WDSQEP Actions Services Logon: " & Me.logon & " Action: " & Me.action & " Sending Report", ProcessDesc, MailPriority.Normal, "01", Me.email, pathfileexport)
End If
Print_Report = "1"
sbTrace.AppendLine("Set Print_Report = 1")

sbTrace.AppendLine("End of R4.Print_Report()")

Catch e1 As Exception

changes done to write the error in log
LoggingTracing.WriteError(e1.ToString())

messtext.Remove(0, messtext.Length)
messtext.Append("User requesting " + Me.building & "-" & Me.logon + " action : " + Me.action + vbCrLf)
messtext.Append("Message: Error during execution of " & sql.ToString & vbCrLf)
messtext.Append(e1.ToString + vbCrLf)
messtext.Append("Date: " + Now.ToLongDateString() + vbCrLf)
messtext.Append("Time: " + Now.ToLongTimeString() + vbCrLf)
messtext.Append(vbCrLf & vbCrLf & "Message sent by WDSQEP Actions Services")
elEvents.WriteEntry(messtext.ToString(), EventLogEntryType.Error)
SendEmail(messtext.ToString(), Me.action + " - " + ProcessDesc + " (Error)", MailPriority.High, "01", Me.email, "")
Finally
changes done to trace the error in log
LoggingTracing.WriteTrace(sbTrace.ToString())

If Me.conn.State = ConnectionState.Open Then Me.conn.Close()

changes done to dispose all the ojbect
If myDS Is Nothing Then
myDS.Dispose()
End If
If mySQLDA IsNot Nothing Then
mySQLDA.Dispose()
End If
If mySqlCmd IsNot Nothing Then
mySqlCmd.Dispose()
End If
If sbTrace IsNot Nothing Then
sbTrace.Length = 0
End If
If messtext IsNot Nothing Then
messtext.Length = 0
End If
End Try
End Function

Private Function Set_Start() As Char
Dim sql As New StringBuilder
Dim mySqlCmd As New SqlCommand
Dim result As Char
sbTrace = New StringBuilder()
Try
sbTrace.AppendLine("Start of R4.Set_Start()")

Set_Start = "0"

sbTrace.AppendLine("Set_Start = 0")

If conn.State = ConnectionState.Open Then conn.Close()
conn.ConnectionString = ConnStr
conn.Open()

sql.Append("sp_act_start_action " & Me.customer & "," & Me.action & "," + Me.logon & "")
sql.Append("," + Me.datein + "")
sql.Append("," + Me.timein + "")
mySqlCmd.Connection = conn
mySqlCmd.CommandText = sql.ToString

sbTrace.AppendLine("Stored Procedure: " & sql.ToString)

result = mySqlCmd.ExecuteScalar()
If result = "1" Then
Set_Start = "1"

sbTrace.AppendLine("Set_Start = 1")
End If

sbTrace.AppendLine("End Of R4.Set_Start()")
Catch e1 As Exception
changes done to write the error in log
LoggingTracing.WriteError(e1.ToString())

messtext.Remove(0, messtext.Length)
messtext.Append("User requesting " + Me.building & "-" & Me.logon + " action : " + Me.action + vbCrLf)
messtext.Append("Message: Error during execution of " & sql.ToString & vbCrLf)
messtext.Append(e1.ToString + vbCrLf)
messtext.Append("Date: " + Now.ToLongDateString() + vbCrLf)
messtext.Append("Time: " + Now.ToLongTimeString() + vbCrLf)
messtext.Append(vbCrLf & vbCrLf & "Message sent by WDSQEP Actions Services")
elEvents.WriteEntry(messtext.ToString(), EventLogEntryType.Error)
SendEmail(messtext.ToString(), Me.action + " - " + ProcessDesc + " (Error)", MailPriority.High, "01", Me.email, "")
Finally
changes done to trace the error in log
LoggingTracing.WriteTrace(sbTrace.ToString())

If Me.conn.State = ConnectionState.Open Then Me.conn.Close()

changes done to dispose all the ojbect
If Not mySqlCmd Is Nothing Then
mySqlCmd.Dispose()
End If
If Not sql Is Nothing Then
sql.Length = 0
End If
If Not sbTrace Is Nothing Then
sbTrace.Length = 0
End If
End Try
End Function

Private Function Set_End() As Char
Dim sql As New StringBuilder
Dim mySqlCmd As New SqlCommand
Dim result As Char
sbTrace = New StringBuilder()
Try
sbTrace.AppendLine("Start of R4.Set_End()")
Set_End = "0"
sbTrace.AppendLine("Set_End = 0")

If conn.State = ConnectionState.Open Then conn.Close()
conn.ConnectionString = ConnStr
conn.Open()

sql.Append("sp_act_end_action " & Me.customer & "," & Me.action & "," + Me.logon & "")
sql.Append("," + Me.datein + "")
sql.Append("," + Me.timein + "")
mySqlCmd.Connection = conn
mySqlCmd.CommandText = sql.ToString
sbTrace.AppendLine("Stored Procedure: " & sql.ToString)
result = mySqlCmd.ExecuteScalar()
If result = "1" Then
Set_End = "1"
sbTrace.AppendLine("Set_End = 1")
End If
sbTrace.AppendLine("End Of R4.Set_End()")
Catch e1 As Exception
changes done to write the error in log
LoggingTracing.WriteError(e1.ToString())

messtext.Remove(0, messtext.Length)
messtext.Append("User requesting " + Me.building & "-" & Me.logon + " action : " + Me.action + vbCrLf)
messtext.Append("Message: Error during execution of " & sql.ToString & vbCrLf)
messtext.Append(e1.ToString + vbCrLf)
messtext.Append("Date: " + Now.ToLongDateString() + vbCrLf)
messtext.Append("Time: " + Now.ToLongTimeString() + vbCrLf)
messtext.Append(vbCrLf & vbCrLf & "Message sent by WDSQEP Actions Services")
elEvents.WriteEntry(messtext.ToString(), EventLogEntryType.Error)
SendEmail(messtext.ToString(), Me.action + " - " + ProcessDesc + " (Error)", MailPriority.High, "01", Me.email, "")
Finally
changes done to trace the error in log
LoggingTracing.WriteTrace(sbTrace.ToString())
If Me.conn.State = ConnectionState.Open Then Me.conn.Close()
changes done to dispose all the ojbect
If Not mySqlCmd Is Nothing Then
mySqlCmd.Dispose()
End If
If Not sql Is Nothing Then
sql.Length = 0
End If
If Not sbTrace Is Nothing Then
sbTrace.Length = 0
End If
End Try
End Function

Public Sub New(ByVal sourcelog As String, ByVal v_action As String, ByVal v_logon As String, ByVal v_logonname As String, ByVal v_datein As String, ByVal v_timein As String, ByVal v_printer As String, ByVal v_email As String, ByVal v_customer As String, ByVal v_customerdesc As String, ByVal v_store As String, ByVal v_storedesc As String, ByVal v_building As String, ByVal v_buildingdesc As String, ByVal v_company As String, ByVal v_whse As String, ByVal v_whsedesc As String, ByVal v_rcvtype As String, ByVal v_ponumber As String, ByVal v_pack As String, ByVal v_wr As String)
Me.elEvents.Source = sourcelog
Me.action = Trim(v_action)
Me.logon = Trim(v_logon)
Me.logonname = Trim(v_logonname)
Me.datein = Trim(v_datein)
Me.timein = Trim(v_timein)
Me.printer = v_printer
Me.email = Trim(v_email)
Me.customer = Trim(v_customer)
Me.customerdesc = Trim(v_customerdesc)
Me.store = Trim(v_store)
Me.storedesc = Trim(v_storedesc)
Me.whsedesc = Trim(v_whsedesc)
Me.building = Trim(v_building)
Me.buildingdesc = Trim(v_buildingdesc)
Me.company = Trim(v_company)
Me.whse = Trim(v_whse)
Me.rcvtype = Trim(v_rcvtype)
Me.POnumber = Trim(v_ponumber)
Me.pack = Trim(v_pack)
Me.wr = Trim(v_wr)
End Sub

Public Sub Print()
Dim deviceInfo As String = _
"<DeviceInfo>" + _
" <OutputFormat>EMF</OutputFormat>" + _
" <PageWidth>8.5in</PageWidth>" + _
" <PageHeight>11in</PageHeight>" + _
" <MarginTop>0.0in</MarginTop>" + _
" <MarginLeft>0.0in</MarginLeft>" + _
" <MarginRight>0.0in</MarginRight>" + _
" <MarginBottom>0.0in</MarginBottom>" + _
"</DeviceInfo>"
Dim stream As Stream
Dim warnings() As Warning = Nothing
Dim printDoc As New PrintDocument()
sbTrace = New StringBuilder()
sbTrace.AppendLine("Start of R4.Print()")
Try
StreamsOfPrint = New List(Of Stream)()
ReportViewer1.LocalReport.Render("Image", deviceInfo, AddressOf CreateStream, warnings)

For Each stream In StreamsOfPrint
stream.Position = 0
Next

If StreamsOfPrint Is Nothing Or StreamsOfPrint.Count = 0 Then
Exit Sub
End If

printDoc.PrinterSettings.PrinterName = Trim(Me.printer)
trace the printer setting
sbTrace.AppendLine("Active Printer : " + printDoc.PrinterSettings.PrinterName)

If Not printDoc.PrinterSettings.IsValid Then
sbTrace.AppendLine("Printer Not Valid. ")
messtext.Append("User Requesting Action: " & Me.logon + " action : " + Me.action + vbCrLf)
messtext.Append("Building: " & Me.building & " RcvType: " & Me.rcvtype & vbCrLf)
messtext.Append("PO Number: " & Me.POnumber & vbCrLf)
messtext.Append("Pack Slip: " & Me.pack & vbCrLf)
messtext.Append("WR: " & Me.wr & vbCrLf)
messtext.Append("Message: The Printer is Invalid" + vbCrLf)
messtext.Append("Date: " + Now.ToLongDateString() + vbCrLf)
messtext.Append("Time: " + Now.ToLongTimeString() + vbCrLf)
messtext.Append(vbCrLf & vbCrLf & "Message sent by WDSQEP Actions Services")
elEvents.WriteEntry(messtext.ToString(), EventLogEntryType.Information)
sbTrace.AppendLine(messtext.ToString())
SendEmail(messtext.ToString(), Me.action + " - " + ProcessDesc + " (Printer Invalid)", MailPriority.Normal, "01", Me.email, "")
Exit Sub
End If

IndexCurrentPagePrint = 0
AddHandler printDoc.PrintPage, AddressOf PrintPage
printDoc.Print()

messtext.Append("User Requesting Action: " & Me.logon + " action : " + Me.action + vbCrLf)
messtext.Append("Building: " & Me.building & " RcvType: " & Me.rcvtype & vbCrLf)
messtext.Append("PO Number: " & Me.POnumber & vbCrLf)
messtext.Append("Pack Slip: " & Me.pack & vbCrLf)
messtext.Append("WR: " & Me.wr & vbCrLf)
messtext.Append("Message: Printing was Sent to " + Me.printer + vbCrLf)
messtext.Append("Date: " + Now.ToLongDateString() + vbCrLf)
messtext.Append("Time: " + Now.ToLongTimeString() + vbCrLf)
messtext.Append(vbCrLf & vbCrLf & "Message sent by WDSQEP Actions Services")
trace the printing description
sbTrace.AppendLine(messtext.ToString())
elEvents.WriteEntry(messtext.ToString(), EventLogEntryType.Information)
sbTrace.AppendLine("End of R4.Print()")
Finally
changes done to trace the error in log
LoggingTracing.WriteTrace(sbTrace.ToString())
changes done to dispose all the ojbect
If printDoc IsNot Nothing Then
printDoc.Dispose()
End If
If Not (StreamsOfPrint Is Nothing) Then
Dim stream_print As Stream
For Each stream_print In StreamsOfPrint
stream_print.Close()
Next
StreamsOfPrint = Nothing
End If
End Try
End Sub

Private Sub PrintPage(ByVal sender As Object, _
ByVal ev As PrintPageEventArgs)
Dim pageImage As New Metafile(StreamsOfPrint(IndexCurrentPagePrint))
ev.Graphics.DrawImage(pageImage, ev.PageBounds)

IndexCurrentPagePrint += 1
ev.HasMorePages = (IndexCurrentPagePrint < StreamsOfPrint.Count)
End Sub

Private Function CreateStream(ByVal name As String, ByVal fileNameExtension As String, _
ByVal encoding As Encoding, ByVal mimeType As String, ByVal willSeek As Boolean) As Stream
Dim stream As Stream
stream = New FileStream(pathlog + name + "_" + Me.logon + "_" + srandom + "_" + Me.POnumber + "_" + Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & Now.Hour.ToString & Now.Minute.ToString & Now.Second.ToString & Now.Millisecond.ToString & CType(RndValue, String) & "." & fileNameExtension, FileMode.Create)
StreamsOfPrint.Add(stream)
Return stream
End Function

Public Sub Export(ByVal Type As String, ByVal pathfileexport As String)
Dim warnings As Warning() = Nothing
Dim streamids As String() = Nothing
Dim mimeType As String = Nothing
Dim encoding As String = Nothing
Dim extension As String = Nothing
Dim bytes As Byte()
Dim deviceInfo As String
Dim fs As FileStream = Nothing
Try
If Type = "EXCEL" Then
deviceInfo = _
"<DeviceInfo>" + _
" <OutputFormat>EXCEL</OutputFormat>" + _
" <PageWidth>8.5in</PageWidth>" + _
" <PageHeight>11in</PageHeight>" + _
" <MarginTop>0.0in</MarginTop>" + _
" <MarginLeft>0.0in</MarginLeft>" + _
" <MarginRight>0.0in</MarginRight>" + _
" <MarginBottom>0.0in</MarginBottom>" + _
"</DeviceInfo>"

bytes = ReportViewer1.LocalReport.Render("Excel", Nothing, mimeType, encoding, extension, streamids, warnings)
fs = New FileStream(pathfileexport, FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
fs.Close()
Else
If Type = "PDF" Then
deviceInfo = _
"<DeviceInfo>" + _
" <OutputFormat>PDF</OutputFormat>" + _
" <PageWidth>8.5in</PageWidth>" + _
" <PageHeight>11in</PageHeight>" + _
" <MarginTop>0.0in</MarginTop>" + _
" <MarginLeft>0.0in</MarginLeft>" + _
" <MarginRight>0.0in</MarginRight>" + _
" <MarginBottom>0.0in</MarginBottom>" + _
"</DeviceInfo>"
bytes = ReportViewer1.LocalReport.Render("PDF", deviceInfo, mimeType, encoding, extension, streamids, warnings)
fs = New FileStream(pathfileexport, FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
fs.Close()
End If
End If
Finally
changes done to dispose all the ojbect
If fs IsNot Nothing Then
fs.Dispose()
End If
End Try

End Sub
End Class

View the full article
 
Back
Top