【问题标题】:Asp.net Handling duplicate key exceptionAsp.net 处理重复键异常
【发布时间】:2015-07-09 21:25:29
【问题描述】:

我正在尝试在 detailsview 中处理重复键异常,我在 iteminserted 下的代码是:

if (e.Exception.Message.Contains("duplicate key")
    {
        Response.Write("Student already registered!");
        e.ExceptionHandled = true;
    }

当有重复键时代码运行正常,但在正常情况下(没有重复键)会出现以下错误:

System.NullReferenceException:对象引用未设置为对象的实例

【问题讨论】:

    标签: c# asp.net exception-handling


    【解决方案1】:

    在运行 If 语句之前检查它是否为空。

    if (e.Exception.Message != null){
          if (e.Exception.Message.Contains("duplicate key")
           {
             Response.Write("Student already registered!");
             e.ExceptionHandled = true;
           }   
     }
    

    【讨论】:

      猜你喜欢
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      相关资源
      最近更新 更多