数据库存储的值是null,不知道为什么程序跳到了else语句,还有弹出异常“对象不能从 DBNull 转换为其他类型。”请问一下是不是不可以这样判断的阿!有哪位仁兄可以帮帮忙
            if (dt.Rows[0]["ysgh"]==null)
            {
                ck.Ysgh = null;
                ck.Yssj = null;
                ck.Ysip = null;
            }
            else
            {
                ck.Ysgh = Convert.ToInt32(dt.Rows[0]["ysgh"]);
                ck.Yssj = dt.Rows[0]["yssj"].ToString();
                ck.Ysip = dt.Rows[0]["ysip"].ToString();
            }

解决方案 »

  1.   

    需要对三个字段的值都做Null判断的哇
      

  2.   


    if (dt.Rows[0]["ysgh"]==DBNull.Value)
                {
                    ck.Ysgh = null;
                    ck.Yssj = null;
                    ck.Ysip = null;
                }
                else
                {
                    ck.Ysgh = Convert.ToInt32(dt.Rows[0]["ysgh"]);
                    ck.Yssj = dt.Rows[0]["yssj"].ToString();
                    ck.Ysip = dt.Rows[0]["ysip"].ToString();
                }
      

  3.   

      ck.Ysgh = Convert.ToInt32(dt.Rows[0]["ysgh"]);
    dt.Rows[0]["ysgh"]  它的值你判断是不为null,万一值是''空呢?null和''是不一样的。。
      

  4.   


    先做非空判断或在sql语句时。。isnull("字段","默认值")
      

  5.   

    2楼正解
    也可以这样if (dt.Rows[0]["ysgh"].ToString()=="")
    {
    ck.Ysgh = null;
    ck.Yssj = null;
    ck.Ysip = null;
    }