Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2004, 02:12 PM
Authorized User
 
Join Date: Nov 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Try Catch block failing

I am trying to implement a "Try Catch" block to generate a custom error message for a SQL Server error. I cannot get it to work.

Here is the code:

Sub btnInsert_Click(Sender As Object, E As EventArgs)

  Dim dsProjectSummary As New DataSet()
  Dim strConnection As String = ConfigurationSettings.AppSettings("BURS")
  Dim objConnection As New SqlConnection(strConnection)
  Dim objCommand As New SqlCommand("ap_ProjectSummaryInsert", objConnection)

  Dim strProjectID As String = CType(FindControl("txtProjectID"), TextBox).Text
  Dim strProjectName As String = CType(FindControl("txtProjectName"), TextBox).Text
  Dim strProjectDescription As String = CType(FindControl("txtProjectDescription"), TextBox).Text
  Dim strPhase As String = CType(FindControl("txtPhase"), TextBox).Text
  Dim strEstDeployDate As String = CType(FindControl("txtEstimatedDeploy"), TextBox).Text
  Dim strRecentAccomplishments As String = CType(FindControl("txtRecentAccomplishments"), TextBox).Text
  Dim strFocusItems As String = CType(FindControl("txtFocusItems"), TextBox).Text
  Dim strEstBenefits As String = CType(FindControl("txtEstBenefits"), TextBox).Text
  Dim strISCost As String = CType(FindControl("txtISCost"), TextBox).Text
  Dim strCorporateISLead As String = CType(FindControl("txtCorporateISLead"), TextBox).Text
  Dim strBusinessLead As String = CType(FindControl("txtBusinessLead"), TextBox).Text

  objCommand.CommandType=CommandType.StoredProcedure

  Dim objParameter As New SqlParameter("@ProjectID", SqlDbType.VarChar, 8)
  objCommand.Parameters.Add(objParameter)
  objParameter.Direction=ParameterDirection.Input
  objParameter.Value=strProjectID

  Dim objParameter1 As New SqlParameter("@ProjectName", SqlDbType.VarChar, 100)
  objCommand.Parameters.Add(objParameter1)
  objParameter1.Direction=ParameterDirection.Input
  objParameter1.Value=strProjectName

  Dim objParameter2 As New SqlParameter("@ProjectDescription", SqlDbType.VarChar, 2000)
  objCommand.Parameters.Add(objParameter2)
  objParameter2.Direction=ParameterDirection.Input
  objParameter2.Value=strProjectDescription

  Dim objParameter3 As New SqlParameter("@Phase", SqlDbType.VarChar, 30)
  objCommand.Parameters.Add(objParameter3)
  objParameter3.Direction=ParameterDirection.Input
  objParameter3.Value=strPhase

  Dim objParameter4 As New SqlParameter("@EstimatedDeploy", SqlDbType.DateTime)
  objCommand.Parameters.Add(objParameter4)
  objParameter4.Direction=ParameterDirection.Input
  objParameter4.Value=Convert.ToDateTime(strEstDeplo yDate)

  Dim objParameter5 As New SqlParameter("@RecentAccomplishments", SqlDbType.VarChar, 2000)
  objCommand.Parameters.Add(objParameter5)
  objParameter5.Direction=ParameterDirection.Input
  objParameter5.Value=strRecentAccomplishments

  Dim objParameter6 As New SqlParameter("@FocusItems", SqlDbType.VarChar, 2000)
  objCommand.Parameters.Add(objParameter6)
  objParameter6.Direction=ParameterDirection.Input
  objParameter6.Value=strFocusItems

  Dim objParameter7 As New SqlParameter("@EstBenefits", SqlDbType.VarChar, 200)
  objCommand.Parameters.Add(objParameter7)
  objParameter7.Direction=ParameterDirection.Input
  objParameter7.Value=strEstBenefits

  Dim objParameter8 As New SqlParameter("@ISCost", SqlDbType.VarChar, 15)
  objCommand.Parameters.Add(objParameter8)
  objParameter8.Direction=ParameterDirection.Input
  objParameter8.Value=strISCost

  Dim objParameter9 As New SqlParameter("@CorporateISLead", SqlDbType.VarChar, 50)
  objCommand.Parameters.Add(objParameter9)
  objParameter9.Direction=ParameterDirection.Input
  objParameter9.Value=strCorporateISLead

  Dim objParameter10 As New SqlParameter("@BusinessLead", SqlDbType.VarChar, 50)
  objCommand.Parameters.Add(objParameter10)
  objParameter10.Direction=ParameterDirection.Input
  objParameter10.Value=strBusinessLead

Try
  objConnection.Open()
    objCommand.ExecuteNonQuery()
    DataBind()
  objConnection.Close()
Catch SQLEx As SQLException
  If SQLEx.Number = 2627 then
    <script language='javascript'>alert('The ProjectID you entered is already used in the database. Please try a different Project ID.');</script>
  Else
    <script language='javascript'>alert('A SQL Server Error Occurred.')</script>
  End If
Finally

End Try

End Sub


When I try to load the web page, I get the following error message:

BC30441: 'Catch' must end with a matching 'End Try'.

Why can't the compiler see the End Try in my code?
 
Old September 24th, 2004, 02:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

It doesn't understand:
1) <script language='javascript'>alert('The ProjectID you entered is already used in the database. Please try a different Project ID.');</script>
2) <script language='javascript'>alert('A SQL Server Error Occurred.')</script>

Try this:
Catch SQLEx As SQLException
  Response.Write(SQLEx.Message)
Catch ex as Exception
  Response.Write("general exception")
Finally

 
Old September 26th, 2004, 04:10 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You have placed javascript in regular code. The compiler doesn't like it.

You can wrap your javascript tags inside a call to the method that will drop it in the page's HTML:

RegisterStartupScript("<script...>...</script>")
 
Old October 5th, 2004, 08:21 AM
Authorized User
 
Join Date: Nov 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by planoie
 You have placed javascript in regular code. The compiler doesn't like it.

You can wrap your javascript tags inside a call to the method that will drop it in the page's HTML:

RegisterStartupScript("<script...>...</script>")
Thanks for the suggestion. Sorry I couldn't respond sooner, I was out of the office all last week.

I inserted the following lines of code after the code for Parameter10:

<code>
Dim strScript1 As String
Dim strScript2 As String

strScript1 = "<script language='javascript'>" & _
           "alert('The ProjectID you entered is already used in the database. Please try a different Project ID.');" & _
           "& chr(60) & "</script>"

strScript2 = "<script language='javascript'>" & _
           "alert('A SQL Server Error Occurred.');" & _
           "& chr(60) & "/script>"

Page.RegisterStartupScript("strScript1", strScript1)
Page.RegisterStartupScript("strScript2", strScript2)
</code>

Now I have a different error:

Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed.

Source Error:

Line 293: #End ExternalSource
Line 294:
Line 295: Public Sub New()
Line 296: MyBase.New
Line 297: Dim dependencies As System.Collections.ArrayList

Source File: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Tempo rary ASP.NET Files\burs\06db06a6\5c2ffd47\a43u_k4r.0.vb Line: 295

Any suggestions on this error?
 
Old October 5th, 2004, 04:22 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Looks like it's telling you that the code can't be there. Where is that code snippit living? It's in a method I assume, but that almost seems to be the problem. Strange...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Failing DAO Tests dgalehouse BOOK: Beginning Spring Framework 2 ISBN: 978-0-470-10161-2 13 September 28th, 2009 07:13 PM
DlookUp Macro failing AidanEnos Access 1 November 23rd, 2007 12:51 AM
Access violation continues after catch (...) block Avihay Visual C++ 1 June 9th, 2006 02:03 AM
DTS Package failing ros1188 SQL Server DTS 1 August 17th, 2005 08:27 AM
Q. My sessions are failing... what's wrong?? richard.york PHP FAQs 0 August 4th, 2004 07:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.