2013-10-21 85 views
1

我使用catch塊與SqlException,但我想顯示自定義消息,而不是異常本身。 那麼如何過濾出這些信息。就像說我想趕上Unique Key約束違規。有一些我可以用來確定錯誤的錯誤代碼。如何在C#中處理sql異常?

protected void Button1_Click(object sender, EventArgs e) 
     { 
      try 
      { 

       objuser.username = txt_email.Text; 
       objuser.email = txt_username.Text; 
       objuser.password = txt_password.Text; 
       int i = BusinessUser.BusinessRegisterUser(objuser); 
       if (i > 0) 
       { 
        Session["user_authenticate"] = "Verified"; 
        Session["user_email"] = objuser.email; // sql exception handle for uniqe key 
        Response.Redirect("user_registration.aspx"); 
       } 
      } 
      catch (SqlException ex) 
      { 
       // Handle Exception here if e-mail already exists 
      } 
      catch (Exception ex) 
      { 

      } 

     } 

回答