C# 异常消息显示在文本框中,该文本框在函数中处理,应更改为简单错误消息。

C# 异常消息显示在文本框中,该文本框在函数中处理,应更改为简单错误消息。,c#,asp.net,C#,Asp.net,我正在用asp.net和c开发一个web应用程序,下面指定了使用的代码 public string fnDisplayManualRecords1(string patid) { string cmdString = "select top 1 (patientid) from Patient_Data where PatientID like '"+patid+"%' order by PatientID desc"; con = new SqlConnection(str);

我正在用asp.net和c开发一个web应用程序,下面指定了使用的代码

public string fnDisplayManualRecords1(string patid)
{
    string cmdString = "select top 1 (patientid) from Patient_Data where PatientID like '"+patid+"%' order by PatientID desc";
    con = new SqlConnection(str);

    try
    {
        con.Open();
        cmd = new SqlCommand(cmdString, con);
        cmd.Parameters.AddWithValue("@patid", patid);
        string result = cmd.ExecuteScalar().ToString();
        return result;
    }
    catch (Exception ex)
    {

        log.Debug("Error: Inside catch block of fnCreateManualRecords");
        log.Error("Error msg:" + ex);

        log.Error("Stack trace:" + ex.StackTrace);
        //transaction.Rollback();
        return ex.ToString();
    }

    finally
    {
        con.Close();
    }


}
这是一个被调用的函数,如下所示,对于正确的数据,它可以正常工作,但是当它捕获异常时,它会在如下所示的文本框中显示异常消息 System.NullReferenceException:对象引用未设置为对象的实例。在myConnection.fDisplayManualRecords1String部分,d:\shryas\PMS\u CMR\PMS\u production\u manual\u update\App\u Code\Connection.cs:第2157行

现在,我只需要将异常消息更改为“未找到数据”,而不是上面显示的实际异常消息。

产生NullReferenceException的原因是您应该检查
try
{
    // Do stuff
}
catch (Exception ex) 
{
    // Log error

    // Return friendly string
    return "No Data Found";
}
返回值为空。不要忘记关闭SqlConnection 作为SqlCommand:使用构造IMHO是最佳选择

public string fnDisplayManualRecords1(string patid) {
  string cmdString = "select top 1 (patientid) from Patient_Data where PatientID like '"+patid+"%' order by PatientID desc";

  using (new SqlConnection(str)) {
    try {
      con.Open();

      using (cmd = new SqlCommand(cmdString, con)) {
        cmd.Parameters.AddWithValue("@patid", patid);

        Object data = cmd.ExecuteScalar();

        // Check if returned value is null: special case  
        if (Object.ReferenceEquals(null, data))
          return "No data found"; // <- Your message here

        return data.ToString(); 
      }
    }
    catch (DataException ex) { // <- Ordinary data (SQL server) errors
      log.Debug("Error: Inside catch block of fnCreateManualRecords");
      log.Error("Error msg:" + ex);
      log.Error("Stack trace:" + ex.StackTrace);

      return ex.ToString();
    }
  }
}

对自定义消息进行例外处理并返回该消息:

try
{
    con.Open();
    cmd = new SqlCommand(cmdString, con);
    cmd.Parameters.AddWithValue("@patid", patid);
    string result = cmd.ExecuteScalar().ToString();
    return result;
}
catch (Exception ex)
{

    log.Debug("Error: Inside catch block of fnCreateManualRecords");
    log.Error("Error msg:" + ex);

    log.Error("Stack trace:" + ex.StackTrace);
    //transaction.Rollback();
     //Custom Exception
     Exception sqlException= new Exception("No Data Found");
     return sqlException;
    //return ex.ToString();
}

finally
{
    con.Close();
}

在这样简单的情况下,尽量不要抛出异常。在数据库查询中找不到结果是一个常见的问题,可以通过if语句进行检查,而不会产生将其转换为值的开销。
try
{
    con.Open();
    cmd = new SqlCommand(cmdString, con);
    cmd.Parameters.AddWithValue("@patid", patid);
    string result = cmd.ExecuteScalar().ToString();
    return result;
}
catch (Exception ex)
{

    log.Debug("Error: Inside catch block of fnCreateManualRecords");
    log.Error("Error msg:" + ex);

    log.Error("Stack trace:" + ex.StackTrace);
    //transaction.Rollback();
     //Custom Exception
     Exception sqlException= new Exception("No Data Found");
     return sqlException;
    //return ex.ToString();
}

finally
{
    con.Close();
}