public int RunProcGetCount(string procName,SqlParameter[] param)
        {
            string count;
            SqlCommand cmd = createCommand(procName, param);
            count=cmd.ExecuteScalar().ToString().Trim();
            if (count == "")
           {
               count = "0";
           }
            Close();
            return count;
        }
出现如下错误:
未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 320:            string count;
行 321:            SqlCommand cmd = createCommand(procName, param);
行 322:            count=cmd.ExecuteScalar().ToString().Trim();
行 323:            if (count == "")
行 324:           {
 

解决方案 »

  1.   

    cmd.ExecuteScalar()可能返回null        public int RunProcGetCount(string procName,SqlParameter[] param)
            {
                SqlCommand cmd = createCommand(procName, param);
                object count = cmd.ExecuteScalar();
                if (count == null) count = 0;
                Close();
                return count.ToString();
            } 
      

  2.   

    检查你的sql语句,在具体数据库环境下执行下
    如果没有查找到任何记录,就会出现这种错误
      

  3.   

      SqlCommand cmd = createCommand(procName, param); 
    检查他。
    看看你传递的参数进去了没有。
    如果没有就会出现那种情况。
    再有就是SQL语句出了问题。
      

  4.   

    出现这个问题的原因是cmd.ExecuteScalar().ToString()为null。你在对null进行trim()当然会发生未将对象引用设置到对象的实例
    检查检查为什么为空吧~!