Catch Try Finally Weirdness

C

Cameron Frasnelly

The second to last lin of code in the below sub is causing VS2003 to tell me
that "Name 'SqlDR' is not declared" when obviously it is. It's also used
just fine in the main code... any ideas????

*****CODE BELOW****

Public Sub loadWOFromKEY()

Try

'Setup the connection

Dim connObj As SqlConnection = New
SqlConnection(OpsDB_Module.ConnectionString())

'Setup the command to execute

Dim cmdObj As SqlCommand = New SqlCommand("spWorkOrderFull_byKEY", connObj)

cmdObj.CommandType = CommandType.StoredProcedure

'----add parameter to stored procedure

Dim myParm As SqlParameter = cmdObj.Parameters.Add("@WorkOrderKEY",
SqlDbType.Int)

myParm.Value = 3333

'Open the connection

connObj.Open()

'Grab the data into a reader object

Dim sqlDR As SqlDataReader = cmdObj.ExecuteReader()

If sqlDR.HasRows = False Then

Me._ProblemTitle = "No Rows"

End If

'----LOTS O WORK - Get the column values

Dim tmpString As String = sqlDR.GetName(0) 'Testing

sqlDR.Read()

Me._WorkOrderKEY = sqlDR.GetInt16(0)

Me._Problem = sqlDR.GetString(sqlDR.GetOrdinal("Problem"))

Me._Priority = sqlDR.GetInt16(sqlDR.GetOrdinal("Priority"))

Me._ReceiveNOW = sqlDR.GetDateTime(sqlDR.GetOrdinal("ReceiveNOW"))

sqlDR.Close()

connObj.Close()

Catch ex As Exception

Me._Problem = ex.Message.ToString

Finally

sqlDR.Close()

End Try
 
A

Armin Zingler

Cameron Frasnelly said:
The second to last lin of code in the below sub is causing VS2003 to
tell me that "Name 'SqlDR' is not declared" when obviously it is.
It's also used just fine in the main code... any ideas????

It's not declared in the finally block, only in the try block. So, it's
scope is block level.

Declare it at procedure level:

dim sqlDR as SqlDatareader

Try

finally

end try
 
C

CJ Taylor

Don't declare your variables in a TRY

declare them outside the try statement and instnatiate within the try

i.e.

Dim sqlCommand as SqlCommand

try

sqlCommand = new SQLCOmmand()

catch

end try
 
C

Chris Dunaway

The second to last lin of code in the below sub is causing VS2003 to
tell me that "Name 'SqlDR' is not declared" when obviously it is.
It's also used just fine in the main code... any ideas????

Are you referring to the line in the Finally clause? If so, then no, it is
not declared. You declared the variable inside the Try clause. Since
Vb.Net now has block level scoping, the variable is not visible in the
finally clause. You will need to move the Dim statement outside the Try
for it to be visible throughout the sub.

Chris
 
J

Jay B. Harlow [MVP - Outlook]

Cameron,
The second to last lin of code in the below sub is causing VS2003 to tell me
that "Name 'SqlDR' is not declared" when obviously it is. It's also used
just fine in the main code... any ideas????
No sqwlDR is not defined (in the current scope).

The Try Block (of the Try/Finally statement) introduces a new scope,
variables defined with in the Try block itself are local to that block. Any
Catch clause blocks & the Finally block are different scopes from the Try
block.

You are trying to use the sqlDR variable in the Finally block.

What I normally do is define the variables outside the try/finally
statement. Then in the Finally block, if its not Nothing dispose of it.
Public Sub loadWOFromKEY()

Dim sqlDR As SqlDataReader

sqlDR = cmdObj.ExecuteReader()

If Not sqlDR Is Nothing Then
sqlDR.Close()
End If

Of course if you use the ExecuteReader with the CommandBehavior of Close
connection then the Finally is not needed.

Dim sqlDR As SqlDataReader =
cmdObj.ExecuteReader(CommandBehavior.CloseConnection)

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

SqlDataReader Error 2
SqlDataReader Problem 7

Top