What's Wrong With My VB.NET Code Of Windows Forms Application?
Posted
by
Krishanu Dey
on Stack Overflow
See other posts from Stack Overflow
or by Krishanu Dey
Published on 2012-11-18T16:44:49Z
Indexed on
2012/11/18
17:00 UTC
Read the original article
Hit count: 206
I've to forms frmPrint & frmEmail and a dataset(MyDataset) with some DataTable and DataTable Adapters. In frmPrint I've the following Sub
Public Sub StartPrinting()
try
adapterLettersInSchedules.Fill(ds.LettersInSchedules)
adapterLetters.Fill(ds.Letters)
adapterClients.Fill(ds.Clients)
adapterPrintJobs.GetPrintJobsDueToday(ds.PrintJobs, False, DateTime.Today)
For Each prow As MyDataSet.PrintJobsRow In ds.PrintJobs
Dim lisrow As MyDataSet.LettersInSchedulesRow = ds.LettersInSchedules.FindByID(prow.LetterInScheduleID)
If lisrow.Isemail = False Then
Dim clientrow As MyDataSet.ClientsRow = ds.Clients.FindByClientID(prow.ClientID)
Dim letterrow As MyDataSet.LettersRow = ds.Letters.FindByID(lisrow.LetterID)
'prow.
'lisrow.is
Label1.SuspendLayout()
Label1.Refresh()
Label1.Text = "Printing letter"
txt.Rtf = letterrow.LetterContents
txt.Rtf = txt.Rtf.Replace("<%Firstname%>", clientrow.FirstName)
txt.Rtf = txt.Rtf.Replace("<%Lastname%>", clientrow.LastName)
txt.Rtf = txt.Rtf.Replace("<%Title%>", clientrow.Title)
txt.Rtf = txt.Rtf.Replace("<%Street%>", clientrow.Street)
txt.Rtf = txt.Rtf.Replace("<%City%>", clientrow.City)
txt.Rtf = txt.Rtf.Replace("<%State%>", clientrow.State)
txt.Rtf = txt.Rtf.Replace("<%Zip%>", clientrow.Zip)
txt.Rtf = txt.Rtf.Replace("<%PhoneH%>", clientrow.PhoneH)
txt.Rtf = txt.Rtf.Replace("<%PhoneW%>", clientrow.PhoneW)
txt.Rtf = txt.Rtf.Replace("<%Date%>", DateTime.Today.ToShortDateString)
Try
PDoc.PrinterSettings = printDlg.PrinterSettings
PDoc.Print()
prow.Printed = True
adapterPrintJobs.Update(prow)
Catch ex As Exception
End Try
End If
Next prow
ds.PrintJobs.Clear()
Catch ex As Exception
MessageBox.Show(ex.Message, "Print", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
And in frmEmail i've the Following Sub
Public Sub SendEmails()
try
adapterLettersInSchedules.Fill(ds.LettersInSchedules)
adapterLetters.Fill(ds.Letters)
adapterClients.Fill(ds.Clients)
adapterEmailJobs.GetEmailJobsDueToday(ds.EmailJobs, False, Today)
Dim ls_string As String
For Each prow As MyDataSet.EmailJobsRow In ds.EmailJobs
Dim lisrow As MyDataSet.LettersInSchedulesRow = ds.LettersInSchedules.FindByID(prow.LetterInScheduleID)
If lisrow.Isemail = True Then
Dim clientrow As MyDataSet.ClientsRow = ds.Clients.FindByClientID(prow.ClientID)
Dim letterrow As MyDataSet.LettersRow = ds.Letters.FindByID(lisrow.LetterID)
txt.Rtf = letterrow.LetterContents
ls_string = RTF2HTML(txt.Rtf)
ls_string = Mid(ls_string, 1, Len(ls_string) - 176)
If ls_string = "" Then Throw New Exception("Rtf To HTML Conversion Failed")
Label1.SuspendLayout()
Label1.Refresh()
Label1.Text = "Sending Email"
If SendEmail(clientrow.Email, ls_string, letterrow.EmailSubject) Then
Try
prow.Emailed = True
adapterEmailJobs.Update(prow)
Catch ex As Exception
End Try
Else
prow.Emailed = False
adapterEmailJobs.Update(prow)
End If
End If
Next prow
Catch ex As Exception
MessageBox.Show(ex.Message, "Email", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
I'm running this two subs using two different Threads.
Public th As New Thread(New ThreadStart(AddressOf StartFirstPrint))
Public th4 As New Thread(New ThreadStart(AddressOf sendFirstEmail))
Here is the code of StartFirstPrint
and sendFirstEmail
Public Sub StartFirstPrint()
Do While thCont
Try
Dim frm As New frmPrint()
'frm.MdiParent = Me
frm.StartPrinting()
Catch ex As Exception
End Try
Loop
End Sub
Public Sub sendFirstEmail()
Do While thCont
Try
Dim frmSNDEmail As New frmEmail
frmSNDEmail.SendEmails()
Catch ex As Exception
End Try
Loop
End Sub
the thCont is a public boolean variable that specifies when to shop those threads.
Most Of the time this works very well. But some times it gives errors Like the following image
I don't know why is this occurring. Please help me.
© Stack Overflow or respective owner