try and finally question

G

Guest

I'd like to put the clean up houskeeping items in a finally block at the end
of sub. Specificly i want to close my db connection, however, in the
connection seems to be out of scope in the finally block. Is there a way
aound this?

Code:
Try


Dim conCal As SqlConnection
Dim cmdCal As SqlCommand
Dim dtrCal As SqlDataReader
Dim strDte, endDte As Date
' set the start date to the first date in the month and then set
it to the first date on the calendar grid
strDte = DateAdd("d", -cDte.Day + 1, cDte.Date)

strDte = DateAdd("d", (-strDte.DayOfWeek), strDte.Date)
endDte = DateAdd("d", 35, strDte)
conCal = New SqlConnection("user id=thepronetworke;data
source=""65.36.160.153"";persist security info=True;ini" & _
"tial catalog=ProNetWrkr;password=vf26nu87")
conCal.Open()
cmdCal = New SqlCommand("select calId, calDaate, calShortDesc,
calDesc, CalHoliday from cal where " & _
"calDate >= @strDte and calDate <= @endDte", conCal)
cmdCal.Parameters.Add("@strDte", strDte)
cmdCal.Parameters.Add("@endDte", endDte)
dtrCal = cmdCal.ExecuteReader()
Response.Write(strDte)

Response.Write(endDte)
While dtrCal.Read()
Response.Write(dtrCal("calDate") & " <br>")
End While
dtrCal.Close()

Catch ex As Exception
Dim msg As String = String.Format("msg: {0} <br>Stack Trace:
{1}", _
ex.Message, ex.StackTrace)
lblMsg.Text = ex.GetType.ToString + "<br>" + msg
Finally
conCal.Close()
conCal.Dispose()
End Try
I have a book that saysthis can be done, but this will not compile.
thanks
kes
 
S

Steve C. Orr [MVP, MCSD]

Move your connection declaration (Dim conCal...) BEFORE the try statement.
That should do the trick.
 
G

Guest

great!! I knew it had to be simple !!!
thanks you
kes
--
I figure if you can''t ask a question honestly, even if it suggests
blasphemy and disturbs the sensibilities of the grand masters of programming
you should quite and join a cult. kes
 

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

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top