views:

25

answers:

2

Im working on an access database, and I want to know how can I fix the code below so that I could display the correct information to the user. The problem is, I want to display an error message if the oledbcommand did not succeed.

        Try

        cn = New OleDbConnection(" Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\ro.mdb")
        'provider to be used when working with access database
        cn.Open()
        cmd = New OleDbCommand("select * from [Table1]", cn)

        cmd.CommandText = "insert into [Table1]([LNAME], [FNAME], [MNAME], [POS], [UNAME], [PASS]) values('" + lstname + "','" + frsname + "','" + midname + "','" + post + "','" + usrname + "','" + pword + "')"


        cmd.ExecuteNonQuery()

    Catch
        MsgBox("Either one of the text box is empty or the IDNUMBER entered is already on the database", MsgBoxStyle.Critical)


    End Try
A: 

Figure out what exceptions you'd like to trap and catch them explicitly


Try

Catch se As SpecialException
  MsgBox("Error1")

Catch e As Exception    ' Catch all other exceptions.
  MsgBox("General Error")

End Try
Trent
A: 

This started as a SqlException and was changed to Oledb for this question. Sorry for not testing.

 Try    
      ... 
    Catch dbException as OledbException
         Dim myErrors as OledbErrorCollection = dbException.Errors
         Dim i as Integer
           For i = 0 to myErrors.Count - 1
               MessageBox.Show("Index #" & i & ControlChars.Cr & _
                   "Error: " & myErrors(i).ToString() & ControlChars.Cr)
           Next i 
    Finially
       ...
Jeff O