Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Try catch

Status
Not open for further replies.

Jacksparrow87

Technical User
Jun 29, 2008
93
GB
Hi people,

I was hoping someone could help me out please, I never really used Try and Catch methods but I've just started to get used to them and they are really helpful.

I was hoping someone could offer me some help please, my application is simple as when the 'Save' button gets clicked the information gets saved in to the database and after that Outlook opens ready to send an email (with the same information that gets saved into the database.)

So far I have the following:

Code:
    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        Try
           'Coding to save data into the database
            MessageBox.Show("Booking Successful!", "CNS Support", MessageBoxButtons.OK, MessageBoxIcon.Information)
           
        Catch e1 As Exception
            MessageBox.Show("Sorry, Error Updating!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        Try
           'Coding to open up outlook
           
        Catch e1 As Exception
            MessageBox.Show("Sorry, Email cannot be sent at this time!", "Sending Email Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

Now the problem with the above coding is that if there is an Error updating to the database then the second try method should not trigger off (so outlook should not open) How would I do this? Can I put the 2nd try method in with the first one so if the first one is successfull then the second one can run?

Please help me, thanks.
 
Yes you can nest Try/Catch blocks. Something like:

Code:
Try
  'Do Something
  Try
    'Do Something else
  Catch e2 As Exception
  End Try
Catch e1 As Exception
End Try

Although in your particular case, you might not need to do so, because you can also catch different types of Exceptions in the same Try/Catch block. Also be sure to use Finally in your Try/Catch blocks where appropriate (an example would be closing your database connection).
 
You could nest your Try/Catch blocks but personally, I'd either set a flag after your first exception and condition the second Try/Catch on that flag or you could just Return after the MessageBox in the first exception.
 
Sorry Im a little bit confused, how would I code the try and catch?

Code:
       Try
            Dim conn As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "\CNS.mdb")
            Dim sql As String = String.Empty

            If _ID = 0 Then
                sql = "insert into Log(lognumber,logno,notenumber,dol,dol2,compid,company,callersname,communication,telephone,email,product,prodcall,cuscall,tspent,problem,logged,solution,chacode,status) values('" & txtlogno.Text & "','" & txtlogno.Text & "','" & txtnotes.Text & "','" & dtp.Text & "','" & dtp.Text & "','" & lblcompid.Text & "','" & txtcomp.Text & "','" & cmbcall.Text & "','" & cmbint.Text & "','" & txttel.Text & "','" & txtmail.Text & "','" & cmbprod.Text & "','" & txtprodcall.Text & "','" & txtcuscall.Text & "','" & dtptime.Text & "','" & Replace(txtprob.Text, "'", "''") & "','" & cmblogged.Text & "','" & Replace(txtsolutions.Text, "'", "''") & "','" & cmbcode.Text & "','" & cmbstatus.Text & "')"
            Else
                sql = "UPDATE Log SET LogNumber='" & txtlogno.Text & "', LogNo='" & txtlogno.Text & "', NoteNumber='" & txtnotes.Text & "', Dol='" & dtp.Text & "', Dol2='" & dtp.Text & "', CompID='" & lblcompid.Text & "', Company='" & txtcomp.Text & "', CallersName='" & cmbcall.Text & "', Communication='" & cmbint.Text & "', Telephone='" & txttel.Text & "', Email='" & txtmail.Text & "', Product='" & cmbprod.Text & "', Prodcall='" & txtprodcall.Text & "', Cuscall='" & txtcuscall.Text & "', TSpent='" & dtptime.Text & "', Problem='" & Replace(txtprob.Text, "'", "''") & "', Logged='" & cmblogged.Text & "', Solution='" & Replace(txtsolutions.Text, "'", "''") & "', ChaCode='" & cmbcode.Text & "', Status='" & cmbstatus.Text & "' WHERE ID=" & _ID

            End If
            conn.Open()
            Dim command As New OleDbCommand(sql, conn)
            command.ExecuteNonQuery()
            conn.Close()
            MessageBox.Show("Update Successful!", "CNS Support", MessageBoxButtons.OK, MessageBoxIcon.Information)
           
 Me.Close()
        Catch e1 As Exception
            MessageBox.Show("Sorry, Error Updating!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

'The above saves the data, below code is the sending mail

 Dim sParams As String = ""
        Dim emailto As String = "Internalsupport@thesolution.co.uk" & ";" & txtmail.Text
        Dim subject As String = "SUPPORT" & ":   " & lbllogcall.Text & ":   " & txtcomp.Text & ":   " & Label1.Text & " " & txtlogno.Text
        Dim body As String = lbllogcall.Text
        body = body & "%0A"
        body = body & "%0A" & Label1.Text & " " & txtlogno.Text
        body = body & "%0A"
        body = body & "%0A" & Label4.Text & " " & dtp.Text
        body = body & "%0A"
        body = body & "%0A" & Label5.Text & " " & txtcomp.Text
        body = body & "%0A"
        body = body & "%0A" & Label6.Text & " " & cmbcall.Text
        body = body & "%0A"
        body = body & "%0A" & Label15.Text & " " & txtcuscall.Text
        body = body & "%0A"
        body = body & "%0A" & Label19.Text & " " & txtprob.Text
        body = body & "%0A"
        body = body & "%0A" & Label21.Text & " " & txtsolutions.Text
        body = body & "%0A"
        body = body & "%0A" & Label20.Text & " " & lblloggedname.Text
        body = body & "%0A"
        body = body & "%0A" & Label22.Text & " " & cmbstatus.Text
        body = body & "%0A"
                       sParams = "mailto:" & emailto & "?subject=" & subject & "&body=" & body
        System.Diagnostics.Process.Start(sParams)

Any one help me with the actual try and cath funtion please?
 
You could use a function like
Code:
Function InsertData() As Boolean
'here you will insert data

Then in your Try Catch
Code:
If IndsertData=True then
'send mail

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
I would do it like this:

Code:
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "\CNS.mdb")

Dim sql As String = String.Empty

            If _ID = 0 Then
                sql = "insert into Log(lognumber,logno,notenumber,dol,dol2,compid,company,callersname,communication,telephone,email,product,prodcall,cuscall,tspent,problem,logged,solution,chacode,status) values('" & txtlogno.Text & "','" & txtlogno.Text & "','" & txtnotes.Text & "','" & dtp.Text & "','" & dtp.Text & "','" & lblcompid.Text & "','" & txtcomp.Text & "','" & cmbcall.Text & "','" & cmbint.Text & "','" & txttel.Text & "','" & txtmail.Text & "','" & cmbprod.Text & "','" & txtprodcall.Text & "','" & txtcuscall.Text & "','" & dtptime.Text & "','" & Replace(txtprob.Text, "'", "''") & "','" & cmblogged.Text & "','" & Replace(txtsolutions.Text, "'", "''") & "','" & cmbcode.Text & "','" & cmbstatus.Text & "')"
            Else
                sql = "UPDATE Log SET LogNumber='" & txtlogno.Text & "', LogNo='" & txtlogno.Text & "', NoteNumber='" & txtnotes.Text & "', Dol='" & dtp.Text & "', Dol2='" & dtp.Text & "', CompID='" & lblcompid.Text & "', Company='" & txtcomp.Text & "', CallersName='" & cmbcall.Text & "', Communication='" & cmbint.Text & "', Telephone='" & txttel.Text & "', Email='" & txtmail.Text & "', Product='" & cmbprod.Text & "', Prodcall='" & txtprodcall.Text & "', Cuscall='" & txtcuscall.Text & "', TSpent='" & dtptime.Text & "', Problem='" & Replace(txtprob.Text, "'", "''") & "', Logged='" & cmblogged.Text & "', Solution='" & Replace(txtsolutions.Text, "'", "''") & "', ChaCode='" & cmbcode.Text & "', Status='" & cmbstatus.Text & "' WHERE ID=" & _ID

            End If
       Try

            
            conn.Open()
            Dim command As New OleDbCommand(sql, conn)
            command.ExecuteNonQuery()
            MessageBox.Show("Update Successful!", "CNS Support", MessageBoxButtons.OK, MessageBoxIcon.Information)
           

        Catch e1 As Exception
            MessageBox.Show("Sorry, Error Updating!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
        End Try

'The above saves the data, below code is the sending mail

 Dim sParams As String = ""
        Dim emailto As String = "Internalsupport@thesolution.co.uk" & ";" & txtmail.Text
        Dim subject As String = "SUPPORT" & ":   " & lbllogcall.Text & ":   " & txtcomp.Text & ":   " & Label1.Text & " " & txtlogno.Text
        Dim body As String = lbllogcall.Text
        body = body & "%0A"
        body = body & "%0A" & Label1.Text & " " & txtlogno.Text
        body = body & "%0A"
        body = body & "%0A" & Label4.Text & " " & dtp.Text
        body = body & "%0A"
        body = body & "%0A" & Label5.Text & " " & txtcomp.Text
        body = body & "%0A"
        body = body & "%0A" & Label6.Text & " " & cmbcall.Text
        body = body & "%0A"
        body = body & "%0A" & Label15.Text & " " & txtcuscall.Text
        body = body & "%0A"
        body = body & "%0A" & Label19.Text & " " & txtprob.Text
        body = body & "%0A"
        body = body & "%0A" & Label21.Text & " " & txtsolutions.Text
        body = body & "%0A"
        body = body & "%0A" & Label20.Text & " " & lblloggedname.Text
        body = body & "%0A"
        body = body & "%0A" & Label22.Text & " " & cmbstatus.Text
        body = body & "%0A"
                       sParams = "mailto:" & emailto & "?subject=" & subject & "&body=" & body
        Try
               System.Diagnostics.Process.Start(sParams)
        Catch e2 As Exception
              'Some Exception Code here
        End Try

Also I would stay away from the string concatenation in your SQL strings and user Parameters if you can.
 
Thanks Zameer, any chance you could implement that with my coding please? as im a little bit confused to where the coding should go.

Thanks bro.
 
Zameer please ignore me previous post as RiverGuy has helped me out, thanks buddy.
 
Riverguy there is a problem with that coding, if there is a problem updating outlook opens, what should happen is if there is a problem updating outlook should not open unless the update is successfull.

Thanks
 
Code:
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim writeWasSuccessful As Boolean = False

        Try
            ' Database Insert or Update code

            writeWasSuccessful = True

        Catch ex As Exception
            MessageBox.Show("Sorry, Error Updating!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
        End Try

        If (writeWasSuccessful) Then

            Try
                ' Send email
            Catch ex As Exception
                ' Error sending email
            End Try

        End If

    End Sub
 
Whoops, either use a flag as Dave has suggested or nest it inside the first Try/Catch.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top