Try/Catch/Finally Question

I am trying the Try/Catch/Finally and I am getting the "myconn" is not declared. I didn't get this error until I put in the Try/Catch/Finally, any reason why?

Try

Dim webnew As Boolean = True
Dim WebID As Integer
Dim contactID As Integer
Dim reportID As Integer
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
"c:\accmon\database\accmon.mdb" & ";"

'********************************************
'Load Website table
Dim MySQL As String = "Insert into websites (agencyID, url, service, addressIP, webnew)" & _
  "Values (@DOTAgency, @URL, @ServiceType, @IPAddress, webnew)"
Dim MyConn As New OleDbConnection(strConn)
Dim Cmd As New OleDbCommand(MySQL, MyConn)
With Cmd.Parameters
  .Add(
New OleDbParameter("@DOTAgency", DOTAgency.SelectedItem.Value))
  .Add(
New OleDbParameter("@URL", URL.Text))
  .Add(
New OleDbParameter("@ServiceType", ServiceType.SelectedItem.Value))
  .Add(
New OleDbParameter("@IPAddress", IPAddress.Text))
  .Add(
New OleDbParameter("@webnew", webnew))
End With
MyConn.Open()
Cmd.ExecuteNonQuery()

'Get the new WebID number
MySQL = ""
MySQL = "SELECT @@Identity FROM websites"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
WebID =
CType(Cmd.ExecuteScalar(), Integer)

'*******************************************
'Load webContact table
MySQL = "Insert into webcontact (webID, primarycontact, primaryemail, privacycontact, privacyemail," & _
"privacyurl, privacyxml) Values (webID, @PMName, @PMEmail, @PName, @PEmail, @PURL, @XML)"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
With Cmd.Parameters
  .Add(
New OleDbParameter("@webID", WebID))
  .Add(
New OleDbParameter("@PMName", PMName.Text))
  .Add(
New OleDbParameter("@PMEmail", PMEmail.Text))
  .Add(
New OleDbParameter("@PName", PName.Text))
  .Add(
New OleDbParameter("@PEmail", PEmail.Text))
  .Add(
New OleDbParameter("@PURL", PURL.Text))
  .Add(
New OleDbParameter("@XML", XML.SelectedItem.Text))
End With
Cmd.ExecuteNonQuery()

'Get the new webContact number
MySQL = ""
MySQL = "SELECT @@Identity FROM webcontact"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
contactID =
CType(Cmd.ExecuteScalar(), Integer)

'Update the webSites table with the correct webContact ID
MySQL = "UPDATE websites SET contactID = @contactID WHERE webID = @webID"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
With Cmd.Parameters
  .Add(
New OleDbParameter("@contactID", contactID))
  .Add(
New OleDbParameter("@webID", WebID))
End With
Cmd.ExecuteNonQuery()

'*******************************************
'Load Report508 table with the webId
MySQL = "Insert into report508 (webID) Values (webID)"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
With Cmd.Parameters
  .Add(
New OleDbParameter("@webID", WebID))
End With
Cmd.ExecuteNonQuery()

'Get the new report508ID number
MySQL = ""
MySQL = "SELECT @@Identity FROM webcontact"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
reportID =
CType(Cmd.ExecuteScalar(), Integer)

'Update the webSites table with the correct webContact ID
MySQL = "UPDATE websites SET reportID = @reportID WHERE webID = @webID"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
With Cmd.Parameters
  .Add(
New OleDbParameter("@reportID", reportID))
  .Add(
New OleDbParameter("@webID", WebID))
End With
Cmd.ExecuteNonQuery()

'**********************************************
'Load Privacy table with the webID
MySQL = "Insert into privacy (webID) Values (webID)"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
With Cmd.Parameters
.Add(
New OleDbParameter("@webID", WebID))
End With
Cmd.ExecuteNonQuery()

Catch ex As Exception
Label3.Text =
"Database cannot be updated. Please try again later."
Finally

'Close Update
MyConn.Close()

End Try
0
gmcghee
8/9/2006 7:44:02 PM
asp.net.access-datasource 4679 articles. 0 followers. Follow

2 Replies
440 Views

Similar Articles

[PageSpeed] 15

Move declarations outside of Try, like so:

Dim webnew As Boolean = True
Dim WebID As Integer
Dim contactID As Integer
Dim reportID As Integer
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
"c:\accmon\database\accmon.mdb" & ";"
'Load Website table
Dim MySQL As String = "Insert into websites (agencyID, url, service, addressIP, webnew)" & _
  "Values (@DOTAgency, @URL, @ServiceType, @IPAddress, webnew)"
Dim MyConn As New OleDbConnection(strConn)
Dim Cmd As New OleDbCommand(MySQL, MyConn)

Try

'********************************************
With Cmd.Parameters
  .Add(
New OleDbParameter("@DOTAgency", DOTAgency.SelectedItem.Value))
  .Add(
New OleDbParameter("@URL", URL.Text))
  .Add(
New OleDbParameter("@ServiceType", ServiceType.SelectedItem.Value))
  .Add(
New OleDbParameter("@IPAddress", IPAddress.Text))
  .Add(
New OleDbParameter("@webnew", webnew))
End With
MyConn.Open()
Cmd.ExecuteNonQuery()

'Get the new WebID number
MySQL = ""
MySQL = "SELECT @@Identity FROM websites"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
WebID =
CType(Cmd.ExecuteScalar(), Integer)

'*******************************************
'Load webContact table
MySQL = "Insert into webcontact (webID, primarycontact, primaryemail, privacycontact, privacyemail," & _
"privacyurl, privacyxml) Values (webID, @PMName, @PMEmail, @PName, @PEmail, @PURL, @XML)"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
With Cmd.Parameters
  .Add(
New OleDbParameter("@webID", WebID))
  .Add(
New OleDbParameter("@PMName", PMName.Text))
  .Add(
New OleDbParameter("@PMEmail", PMEmail.Text))
  .Add(
New OleDbParameter("@PName", PName.Text))
  .Add(
New OleDbParameter("@PEmail", PEmail.Text))
  .Add(
New OleDbParameter("@PURL", PURL.Text))
  .Add(
New OleDbParameter("@XML", XML.SelectedItem.Text))
End With
Cmd.ExecuteNonQuery()

'Get the new webContact number
MySQL = ""
MySQL = "SELECT @@Identity FROM webcontact"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
contactID =
CType(Cmd.ExecuteScalar(), Integer)

'Update the webSites table with the correct webContact ID
MySQL = "UPDATE websites SET contactID = @contactID WHERE webID = @webID"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
With Cmd.Parameters
  .Add(
New OleDbParameter("@contactID", contactID))
  .Add(
New OleDbParameter("@webID", WebID))
End With
Cmd.ExecuteNonQuery()

'*******************************************
'Load Report508 table with the webId
MySQL = "Insert into report508 (webID) Values (webID)"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
With Cmd.Parameters
  .Add(
New OleDbParameter("@webID", WebID))
End With
Cmd.ExecuteNonQuery()

'Get the new report508ID number
MySQL = ""
MySQL = "SELECT @@Identity FROM webcontact"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
reportID =
CType(Cmd.ExecuteScalar(), Integer)

'Update the webSites table with the correct webContact ID
MySQL = "UPDATE websites SET reportID = @reportID WHERE webID = @webID"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
With Cmd.Parameters
  .Add(
New OleDbParameter("@reportID", reportID))
  .Add(
New OleDbParameter("@webID", WebID))
End With
Cmd.ExecuteNonQuery()

'**********************************************
'Load Privacy table with the webID
MySQL = "Insert into privacy (webID) Values (webID)"
Cmd.Parameters.Clear()
Cmd.CommandText = MySQL
With Cmd.Parameters
.Add(
New OleDbParameter("@webID", WebID))
End With
Cmd.ExecuteNonQuery()

Catch ex As Exception
Label3.Text =
"Database cannot be updated. Please try again later."
Finally

'Close Update
MyConn.Close()

End Try
Starting with ASP.NET 2.0? Look at:
Programming Microsoft Web Forms
My Blog
0
douglas
8/9/2006 9:16:51 PM

thanks, solve the issue

g

0
gmcghee
8/10/2006 12:52:17 PM
Reply:

Similar Artilces:

Try Catch Question about Finally
Hi folks, Inside of a function that returns an integer I do a databse call. Se code below. If I do the following and there is an exception does the finally get performed? If there is an exception and the "ParamReturn.Value" is 1 what gets returned. I'm just a little confused on the sequence of events. <CODE>Try   objConn.Open()  objCmd.ExecuteNonQuery() Catch ex As Exception   Return 2 Finally  objConn.Close() End Try Return ParamReturn.Value </CODE>John BelthoffDodge, Duck, Dip, Dive & DodgeIf a man can dodge a wrench, he can dodge a...

Try Catch Finally (Finally omit)
Hi, I am using VS.NET 2005. In previous version of VS.NET, we have Try, Catch & Finally, but why in VS.NET 2005 the Finally omit in VB.NET and it is still using in C#. I am asking in VB.NET as if we type Try and enter then only Try & Catch came but bydefault Finally not came and we required to manually enter it but but in C# when we type Try, the Catch & Finally byself come. Thanks in Advance. Einstein_Lover: Hi, I am using VS.NET 2005. In previous version of VS.NET, we have Try, Catch & Finally, but why in VS.NET 2005 the Finally omit in VB.NET and it is still using...

TRY...CATCH...FINALLY...END TRY
When Error accured. Process will run to the CATCH clause then run Finally Clause. But When Error not accured, Process will run to Finally Clause. So, Error accured or not, Finally Clause certain will to be run it. I don't know, if this, script in Finally Clause or after Finally's script, what's different? Thanks Leo I don't understand the question... but Finally is executed if the exception is thrown or not. "LEO" <leo@mail.nak.com.tw> wrote in message news:FZ6i83HKCHA.1020@forums.sybase.com... > When Error accured. Process will ru...

Debug.Assert, try/catch/finally block strategy question
I'm sick of putting break points in try catch blocks and finding errors that way when I'm debugging I love Debug.Assert - Instead of putting break points in every catch block, I wish there was a way to do something like assert...Get ready to blast your energy levels into high gear with Sedgewick Energy Drink. High powered adaptogenic herbs mixed with potent levels of B-vitamins and...Please disregard the contents of this post. what in the world are you doing debugging like that? Use the command window, and other tools also. There is no better means of doing anything than to ste...

try catch finally / response.redirect / response.end flow control question
Hi Guys, Can any one tell me if the finally block would be executed in the following example (assuming that dbReader.read() returned false because no records were found).   Many Thanks! DanielTry dbCon = New SqlConnection(myconnectionstring) dbCon.Open() dbCom = dbCon.CreateCommand() strSQL + "SELECT * FROM bookings WHERE ID &gt; 9000" dbCom.CommandText = strSQL dbReader = dbCom.ExecuteReader() If dbReader.Read() Then 'init fields Else Response.Redirect("main.aspx") End if Finally dbReader.clo...

TRY...CATCH...FINALLY...END TRY statement
I am getting serious issues with the PB Statement TRY...CATCH...FINALLY...END TRY I am using: TRY ds_buffers[1].settransobject(SQLCA) Rows_Page=ds_buffers[1].retrieve(mdl_sk_itemref) ValColPage=ds_buffers[1].Object.sk_page[1,Rows_Page] CATCH (type of erro) messagebox("","") {FINALLY } END TRY I am getting Sintax Error. Anybody could help me out? this is my use of Try-catch TRY [...] CATCH (ctscomponents_createexception e) [...] FINALLY [...] END TRY It's your CATCH clause. Here's an excerpt from the help file...

Catch, Try Question and Stored Procedure Question
 Hi, i know that catch ex as SQLexception is for catching SQL error right? Then what is "catch ex as exception" for?  and also what is "Finally" used for? Another thing about stored procedure, what is the range of values that i can use for setting my own return values? like if exist (select * from table) return -101 <--- what is the range of numbers i can use these?and what are the other numbers like the positive numbers for? thanks To Under Stand the Try Catch and FInally you must read the following articles http://msdn.microsoft.com/en-us...

What if Finally throws an error in try-catch-finally?
can anyone tell me What if Finally throws an error in try-catch-finally? CheersBinowww.codepal.co.inPlease Mark as Answer if you find the post useful. It'll throw an exception - being in the finally block doesn't exclude your code from throwing exceptions.You can put another try/catch block inside the finally block if you want to trap exceptions there.  Then the exception that is generated in the Finally block will propagate up the call stack until a Catch block is found For example Sub Main()  Try    Foo()  Catch    Console.Wri...

.NET 2005 TRY..CATCH with SQL RAISERROR Not Catching
Is there a reason why the following code does not raise an error in my .NET 2005 application? Basically I have a try..catch in my stored procedure. Then I have a try...catch in my .NET application that I want to display the error message. But, when the stored proc raisses the error, the .net code doesn't raise it's error. The .NET code DOES raise an error if I remove the try..catch from the SQL proc and let it error (no error handling), but not if I catch the error and then use RAISERROR to bubble-up the error to .NET app. (I really need to catch the error in my SQL proc and rollbac...

try catch in try catch is it ok ?
Hi I'm having some trouble with the TTrayIcon, occasionally it can not create the tray icon and then i throws and exception, but it still set its FVisible = true even though it didn't succeed. So I have come up with this solution: try{ TrayIcon->Visible = true; }catch( const EOutOfResources &e ) { try{ TrayIcon->Visible = false; }catch( const EOutOfResources &e ) { ShowTrayIconTimer->Enabled = true; //tries again a little later. } } I'm not sure about the try catch in...

try-catch-finally
Why can't I do: try { A(); } catch(...) { B(); } __finally { C(); } Should I do this instead? try { A(); } catch(...) { B(); } C(); Is there any difference? Will C() be *alway* executed? Hi Tom Tom Duhamel wrote: I think You need an extra try something like this: try { try { A(); } catch(...) { B(); } }__finally { C(); } Kind regards Asger "Tom Duhamel" <tom420.duhamel@gmail.com> wrote in message news:52758@forums.codegear.com... > Why can't I do: Bec...

Try-catch questions
I am experimenting with try-catch (finally). I have two windows, one an inquiry and the other a transaction entry window. Both are very complex, core windows in the app. The nature of the transaction entry window is such that much work can be in an un-saved state. The nature of the inquiry is that it crashes on a more or less regular basis. The inquiry is very complex, with 30-50(?)+ events spread out over 50+ objuects. It is not feasible to surround all the script with try-catch. I would like to see ANY system error generated in the inquiry trapped in a try-catch so that I ...

Try/Catch/Finally
Hi In try/catch/ (finally) block when we're configuring a data base connection what do we need properly to put inside de try block.. objConnection, objDataAdapter, Objcommand.. just the first one..? I see several things around that stuff.. And what about finally block.. only Close and Dispose methods?Thanks a lot for any commentBest regards,Ivan Andrade By and large, you need to protect objects that hold unmanaged resources with a try/catch/finally block.  Connections for sure meet this criteria.  certainly not the adapter, and I do not believe the command hold...

How do to TRY...CATCH...FINALLY
Im trying to use the try catch fianlly to bypass and invalid row range error while accessing a datastore which is empty. Here is my code, try is_exclude_vendor[] = ids_vendorobject.vendor[1, ids_vendor.rowcount()] catch ( dwruntimeerror err) messagebox("Error", err.getmessage()) end try My problem is, now I 2 error messages Frist the powerbuilder error message saying invalid row range and then the error message from the catch block. Isnt the catch section supposed to bypass the default error message ? Am I not doing it correctly ? Thanks, Satish. Uncheck Just In ...

Web resources about - Try/Catch/Finally Question - asp.net.access-datasource

West Lothian question - Wikipedia, the free encyclopedia
He illustrated his point by pointing out the absurdity of a Member of Parliament for West Lothian being able to vote on matters affecting the ...

Melbourne Victory's Kevin Muscat irked by questions of Roy O'Donovan's ban
The famous Muscat stare was in evidence midway through his pre-match chat with journalists after Victory's training session on Thursday.

Question Of The Day: Worst Craigslist nightmare?
Filed under: Etc. , Used Car Buying Buyers that demand a lifetime warranty. Sellers who lie. Flakes who don't show up. What's your worst car-related ...

iPhone 6s demand called into question as Apple reportedly orders cut back in production
Citing sources familiar with Apple's supply chain, Nikkei is reporting that the company is scaling back iPhone 6s and 6s Plus production by as ...

One SAHM's Answer to the Dreaded Question: 'Do You Work?'
We volunteer at school (but not too much). We get involved with sports (but not too much). We show up at every poetry reading, tipi-making event, ...

'Making a Murderer' lawyers answer 2 burning questions viewers have about the Steven Avery case
Steve Avery's defense attorney Dean Strang and former Calumet County prosecutor Ken Kratz appeared on Fox News to address questions about the ...

Question #5 for 2016: Will the Fed raise rates in 2016, and if so, by how much?
Earlier I posted some questions for next year: Ten Economic Questions for 2016 . I'll try to add some thoughts, and maybe some predictions for ...

Donald Trump: Birther questions about Ted Cruz could be a problem
Republican front-runner suggests citizenship questions about Cruz, who was born in Canada, could hurt GOP

Podtoid records today, ask us questions!
Here's a little peek behind the curtain: there's a good chance you don't listen to Podtoids on Wednesday, unless that's the soonest day post ...

Five Questions To Ask Before You Accept A Job Offer
Here are five essential questions to ask before you accept a job offer because Liz Ryan doesn't want you to end up in the wrong job!

Resources last updated: 1/7/2016 5:36:13 PM