开发者

How to check if an oledbcommand succeeded?

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


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
       ...


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜