2017-02-24 37 views
0

在我的C#.NET Web應用程序中我有一個GridView綁定到ObjectDataSource。該ObjectDataSource使用我的App_Code文件夾中的.cs文件中的select,update,insert,delete命令方法。同樣在ObjectDataSource控件我已經OnDeleted,OnUpdated,等...設置爲指向我的代碼隱藏功能,試圖更新,並顯示錯誤消息的標籤,以便用戶知道數據庫命令沒有工作:C#異常處理無法在ObjectDataSource上工作

protected void odsLinks_DBAction(object sender, ObjectDataSourceStatusEventArgs e) 
    {  
     lbLinksErrorBox.Text = ""; 
     if (e.Exception != null) 
     { 
      lbLinksErrorBox.Text = "There was a problem.<br>" + e.Exception.InnerException.Message.ToString(); 
      lbLinksErrorBox.ForeColor = System.Drawing.Color.Red; 
      //e.ExceptionHandled = true; 
     } 
    } 

哪裏我實際上做我的數據庫操作,我已經建立了他們周圍的try/catch塊,如:

try 
{ 
    //SQL work happens here 
} 
catch (Exception e) 
{ 
    //It sends me an email here 
} 

的問題是,當我在我的SQL語句中的錯誤,和它的土地在catch塊中,錯誤會佔據屏幕。我寧願將錯誤顯示在我的標籤lbLinksErrorBox中作爲紅色文本,以便用戶意識到問題,但可以繼續工作。

它似乎並不像onDeleted錯誤處理的要求不斷被達成。錯誤在try/catch塊結束,並且永遠不會在代碼隱藏中使用onDeleted odsLinks_DBAction函數。

這是我的ObjectDataSource控件看起來像我的.aspx頁面中:

<asp:ObjectDataSource ID="odsLinks" runat="server" SelectMethod="GetLinks" TypeName="TaskDataAccess" DeleteMethod="DeleteLink" InsertMethod="AddLink" UpdateMethod="UpdateLink" OnDeleted="odsLinks_DBAction" OnInserted="odsLinks_DBAction" OnUpdated="odsLinks_DBAction"> 
     <DeleteParameters> 
      <asp:Parameter Name="link_ID" Type="String" /> 
     </DeleteParameters> 
     <InsertParameters> 
      <asp:ControlParameter ControlID="hfTreeID" Name="tree_ID" PropertyName="Value" Type="String" /> 
      <asp:ControlParameter ControlID="hfTaskID" Name="task_ID" PropertyName="Value" Type="String" /> 
      <asp:Parameter Name="link_Title" Type="String" /> 
      <asp:Parameter Name="link_URL" Type="String" /> 
     </InsertParameters> 
     <SelectParameters> 
      <asp:ControlParameter ControlID="hfTreeID" Name="tree_ID" PropertyName="Value" Type="String" /> 
      <asp:ControlParameter ControlID="hfTaskID" Name="task_ID" PropertyName="Value" Type="String" /> 
     </SelectParameters> 
     <UpdateParameters> 
      <asp:Parameter Name="link_ID" Type="String" /> 
      <asp:Parameter Name="link_Title" Type="String" /> 
      <asp:Parameter Name="link_URL" Type="String" /> 
     </UpdateParameters> 
    </asp:ObjectDataSource> 

這裏是我的App_Code文件cs文件的數據庫連接是如何建立一個例子:

public static void DeleteLink(string link_ID) 
{ 
    GetUserDateTime gudt = new GetUserDateTime(); 
    try 
    { 
     string dsn = ConfigurationManager.ConnectionStrings["myDatabaseHere"].ConnectionString; 
     SqlConnection con = new SqlConnection(dsn); 
     SqlCommand cmd = new SqlCommand("UPDATE [TreeLinks] SET Link_Deleted = 'YES' where Link_ID = @Link_ID", con); 
     cmd.Parameters.AddWithValue("@Link_ID", link_ID); 

     con.Open(); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
    } 
    catch (Exception e) 
    { 
     string body = "Error on TaskDataAccess.DeleteLink() <br>" + gudt.user + "<br>" + e.ToString(); 
     SendEmail.sendEmail("Error on Task page", body); 
    } 
} 

想法?我如何從我的SQL連接中獲取錯誤,以便不接管整個頁面,而是顯示在我的網頁上的標籤上?他們被觸發之前

回答

0

你的事件將永遠不會觸發的,會引發異常。

從咬合內設置有異常的信息標籤。

好了,它是一個非常同時,因爲我用網絡的形式,但如果IIRC你的消耗臭氧層物質,你需要添加一個名爲當appcode事件結束回調方法,例如: OnUpdating =「MyObjectDataSource_Updated」 ......

的MyObjectDataSource_Updated方法應該是在你的頁面不是共享CS文件,因此它可以訪問你的頁面元素(標籤等)

+0

我有它數據庫類代碼,以便它可以在其他頁面共享 – Kari

+0

不,我的意思是將try catch移動到您的頁面,將db代碼保留在原來的位置,例外c然後在您的頁面中提出,您可以訪問該錯誤並將其分配給您的標籤。 –

+0

我不知道在哪裏放置的try/catch在我的網頁,因爲所有它是一個ObjectDataSource指向更新/刪除/等的方法是這樣的: – Kari

0

添加事件處理程序對於OnUpdated,OnDeleted等到你的objectdatasource定義,這些應該指向你的代碼中的方法,以便它們可以訪問eventargs中的異常和你想要顯示異常信息的標籤元素。