Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Try Catch Finally and variable declaration

Try Catch Finally and variable declaration

Scheduled Pinned Locked Moved Visual Basic
helpquestionworkspace
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zyxil
    wrote on last edited by
    #1

    I have the following simple code, yet in the finally block the compilier says that "'con' is not declared." What's up with this? (LogEvent is just an event log wrapper function which works like a champ.) Try Dim con As New SqlConnection(Configuration.Settings("ConnectionString")) Dim cmd As New SqlCommand("INSERT INTO Messages (MessageType, Message) VALUES('" & _ MessageType & "','" & Message & "')", con) cmd.ExecuteNonQuery() Catch e As Exception LogEvent(e.ToString(), EventLogEntryType.Error) Finally con.Close() '<- compiler error here: "con" is not declared. End Try -John

    Z 1 Reply Last reply
    0
    • Z Zyxil

      I have the following simple code, yet in the finally block the compilier says that "'con' is not declared." What's up with this? (LogEvent is just an event log wrapper function which works like a champ.) Try Dim con As New SqlConnection(Configuration.Settings("ConnectionString")) Dim cmd As New SqlCommand("INSERT INTO Messages (MessageType, Message) VALUES('" & _ MessageType & "','" & Message & "')", con) cmd.ExecuteNonQuery() Catch e As Exception LogEvent(e.ToString(), EventLogEntryType.Error) Finally con.Close() '<- compiler error here: "con" is not declared. End Try -John

      Z Offline
      Z Offline
      Zyxil
      wrote on last edited by
      #2

      con needs to have been declared outside of the try block, as thus: Dim con As SqlConnection Dim cmd As SqlCommand Try con = New SqlConnection(Configuration.Settings("ConnectionString")) cmd = New SqlCommand("INSERT INTO Messages (MessageType, Message) VALUES('" & _ MessageType & "','" & Message & "')", con) cmd.ExecuteNonQuery() Catch e As Exception LogEvent(e.ToString(), EventLogEntryType.Error) Finally con.Close() End Try -John

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups