try catch problem - help pls

thejeraldo

Well-known member
Joined
Jun 9, 2009
Messages
70
Location
Philippines
Programming Experience
1-3
guys please help me on my error handling.. im kinda confused with this try catch thing in vb.net. yes i can catch exceptions but i wanna be able to catch exceptions specifically. like when a user entered an employee id that already exists. i want to do this so the users can understand the error and not in jargon so they will be able to give me feedback/reports if there is anybug.

when i do multiple exceptions i get an warning code like these : catch block never reach.... on the DuplicateNameException.

thanks a lot.

Code:
[FONT="Courier New"][SIZE="2"]Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            cmd = New OleDb.OleDbCommand
            cmd.CommandType = CommandType.Text
            strsave = "UPDATE tblemp SET "
            strsave = strsave + "EmpID = '" + txEmpID.Text + "', "
            strsave = strsave + "FirstName = '" + txFName.Text + "', "
            strsave = strsave + "MiddleName = '" + txMName.Text + "', "
            strsave = strsave + "LastName = '" + txLName.Text + "' "
            strsave = strsave + "WHERE EmpID = '" + emp + "'"
            cmd.CommandText = strsave

            cmd.Connection = con
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "HRIS Error")
        Catch ex As DuplicateNameException
            MsgBox("Inform the user that the employee id she entered is not unique. or something like that")

        End Try
    End Sub[/SIZE][/FONT]
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Code:
CREATE PROCEDURE NewEmployee
(
   @EmpID int,
   @FirstName varchar(50),
   etc...
)
AS

IF EXISTS(SELECT EmpID FROM tblemp WHERE EmpID = @EmpID)

 -- update the existing record
 UPDATE tblemp 
    SET
    EmpID = @EmpID, FirstName = @FirstName etc... 
 WHERE EmpID = @EmpID

ELSE

 -- insert new record
 INSERT INTO tblemp (EmpID, FirstName, etc...) 
 VALUES (@EmpID, @FirstName, etc...)
In addition you may want to return the results in ReturnValue param:

Code:
commmand.Parameters.Add("@ReturnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue
If you set the return to 1 it means that record exists otherwise it does not.

P.S> of course you can always use an inline query to check if an EmpID exists.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,412
Location
Norway
Programming Experience
10+
thejeraldo said:
catch block never reach.... on the DuplicateNameException.
Code:
       Catch ex As Exception
            ...
       Catch ex As DuplicateNameException
help said:
supply Catch statements aimed at specific types of errors, going from the most specific to the least specific.
How to: Test Code with a Try…Catch Block in Visual Basic
Since all exceptions inherits the Exception class, the Exception type is the least specific type. This is the correct order:
Code:
       Catch ex As DuplicateNameException

       Catch ex As Exception
 
Top Bottom