PC Review


Reply
Thread Tools Rate Thread

Catch Try Finally Weirdness

 
 
Cameron Frasnelly
Guest
Posts: n/a
 
      27th Aug 2003
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


 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      27th Aug 2003
"Cameron Frasnelly" <(E-Mail Removed)> schrieb
> 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


--
Armin

 
Reply With Quote
 
CJ Taylor
Guest
Posts: n/a
 
      27th Aug 2003
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

"Cameron Frasnelly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Chris Dunaway
Guest
Posts: n/a
 
      27th Aug 2003
"Cameron Frasnelly" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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
 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      27th Aug 2003
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

> Try


sqlDR = cmdObj.ExecuteReader()

> Finally


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

> End Try


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

"Cameron Frasnelly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Try...Catch...Finally not firing finally? David Lozzi Microsoft ASP .NET 12 11th May 2007 01:41 AM
Try Catch Else Finally cj Microsoft VB .NET 32 18th Mar 2006 02:51 AM
try/catch/finally Alexander Mueller Microsoft C# .NET 17 22nd Mar 2005 10:03 AM
try catch finally Tilfried Weissenberger Microsoft C# .NET 6 30th Nov 2004 02:06 PM
try, catch, finally- whats the point of finally? Alan Paulk Microsoft C# .NET 7 11th May 2004 02:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:02 PM.