c# - DataBind事件发生后如何捕捉异常?

标签 c# asp.net error-handling sqldatasource

我有下一个代码:

   protected void Page_Load(object sender, EventArgs e)
    {
       // some code-------------------
       sourceDetails.SelectCommand += "<new condition>";
       this.DataBind();
    }
sourceDetails是.aspx页中的SqlDataSource对象,可以查询数据库。
"<new condition>"-条件发生变化。

如果条件不正确,我的页面会出现错误。我想在错误出现之前将其捕获。

最佳答案

这将警告错误:

try
{
    // some code-------------------
    sourceDetails.SelectCommand += "<new condition>";
    this.DataBind();
}
catch (SqlException e)
{
    ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('" + e.Message + "');", true);
}

关于c# - DataBind事件发生后如何捕捉异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20987064/

相关文章:

c# - 添加带有 Identity asp.net core 的 UserClaim

javascript - 在填写联系表单后,如何让用户返回到他们原来的页面?

android.database.sqlite.SQLiteException : near ": syntax error (code 1): , 编译时:android 编程错误

c# - 从 Skydrive 读取

c# - 将字符串 "172406"快速转换为整数 17、24、06

c# - 无法在 T4 模板中使用我的自定义类

asp.net - 当 UpdatePanel 在计时器上触发时 WatermarkExtender 闪烁

c# - 获取 asp :SqlDataSource Connection String dynamically from a method (Without Webconfig)

wpf - 在VB.net和WPF中处理异常

sql-server - SQL Server 2008-捕获由链接服务器上运行的存储proc引发的执行错误