2009-12-31 4 views
0

Im travaillant sur une base de données d'accès, et je veux savoir comment puis-je corriger le code ci-dessous pour que je puisse afficher les informations correctes à l'utilisateur. Le problème est, je veux afficher un message d'erreur si le OleDbCommand n'a pas réussi.Comment vérifier si un OleDbCommand réussi?

 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 

Répondre

0

Cela a commencé comme SqlException et a été changé pour Oledb pour cette question. Désolé de ne pas avoir testé.

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

Pensez à ce que les exceptions que vous souhaitez piéger et les attraper explicitement


Try 

Catch se As SpecialException 
    MsgBox("Error1") 

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

End Try 
Questions connexes