提示错误:未将对象引用设置到对象的实例。
错误行为最后一行  i =(int) command.ExecuteScalar();请大家指点下,不知道哪里错了。数据库为access,Car表中car_num为自增加的一个属性,肯定是int类型的。代码:            string Constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\db\db1.mdb;User ID=admin";
            OleDbConnection oc = new OleDbConnection(Constr);       //初始化连接
            oc.Open();            string sql = string.Format("SELECT car_num FROM [Car] where car_licence='{0}' and car_outtime = null", outcar_lic_box.Text.Trim());//查找车牌号为outcar_lic_box.Text.Trim(),并且cat_outtime为空的车辆。
            OleDbCommand command = new OleDbCommand(sql, oc);
            i =(int) command.ExecuteScalar();

解决方案 »

  1.   

    没有数据 ,返回的ExecuteScalar是null,把null转成int报错
      

  2.   

    car_outtime = nullSQL里的NULL判断是is null,不是= null
      

  3.   

    先把你的 SQL 语句在 Access 中执行一下,看看结果,再检查代码。
    这种情况一般都是因为没有符合条件的值而返回 null 造成的。============================================
    string sql = string.Format("SELECT car_num FROM [Car] where car_licence='{0}' and car_outtime = null", outcar_lic_box.Text.Trim());//查找车牌号为outcar_lic_box.Text.Trim(),并且cat_outtime为空的车辆。
    ============================================这句你换成字符串链接形式的 SQL 语句试试,把 '{0]' 去掉看看,可能是这里的问题。