C# 如何在数据绑定事件后捕获异常?
我有下一个代码:C# 如何在数据绑定事件后捕获异常?,c#,asp.net,error-handling,sqldatasource,C#,Asp.net,Error Handling,Sqldatasource,我有下一个代码: protected void Page_Load(object sender, EventArgs e) { // some code------------------- sourceDetails.SelectCommand += "<new condition>"; this.DataBind(); } 受保护的无效页面加载(对象发送方,事件参数e) { //一些代码--------------
protected void Page_Load(object sender, EventArgs e)
{
// some code-------------------
sourceDetails.SelectCommand += "<new condition>";
this.DataBind();
}
受保护的无效页面加载(对象发送方,事件参数e)
{
//一些代码-------------------
sourceDetails.SelectCommand+=“”;
这个.DataBind();
}
sourceDetails
是.aspx页面中的SqlDataSource
对象,它具有对数据库的查询
”
-发生变化的条件
如果条件不正确,我的页面上会出现错误。我想在错误出现之前捕获它。这将警告错误:
try
{
// some code-------------------
sourceDetails.SelectCommand += "<new condition>";
this.DataBind();
}
catch (SqlException e)
{
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('" + e.Message + "');", true);
}
试试看
{
//一些代码-------------------
sourceDetails.SelectCommand+=“”;
这个.DataBind();
}
捕获(SQLE异常)
{
RegisterStartupScript(this.GetType(),“Alert”,“Alert(“+e.Message+”);”,true);
}
正如您在中所看到的,有一些特定的。也许您可以使用这些生命周期挂钩(或)来防止错误,或者更好地处理错误
希望我能帮忙 谢谢大家关注我的问题!我找到了下一个解决方案: 1.SqlDataSource已选择事件。我用过它。 2.我在OnSelected事件中编写了以下代码:
protected void sourceDetails_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
//Doing what we need to do
}
}
SqlDataSourceStatusEventArgs
包含处理异常所需的所有数据:exception
和Command
属性。…您的意思是像try..catch
?Simon Whitehead,是的。怎么样-用所有可能的条件测试你的代码,这样你的用户就永远看不到异常了?捕获异常后你想做什么?显示另一页?尝试{}捕获{}没有帮助。首先,我使用OracleClient,其次,它在Page_Load事件中毫无例外地执行。