在我的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連接中獲取錯誤,以便不接管整個頁面,而是顯示在我的網頁上的標籤上?他們被觸發之前
我有它數據庫類代碼,以便它可以在其他頁面共享 – Kari
不,我的意思是將try catch移動到您的頁面,將db代碼保留在原來的位置,例外c然後在您的頁面中提出,您可以訪問該錯誤並將其分配給您的標籤。 –
我不知道在哪裏放置的try/catch在我的網頁,因爲所有它是一個ObjectDataSource指向更新/刪除/等的方法是這樣的: – Kari