数据库是Access,表tb的结构是 nid 数字 默认值为0 主键 非自增,name 文本,department 文本;空表。
protected int selectID(string nid,string tablename)
    {
        int id;
        string connectionstring = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        string connection = connectionstring + Server.MapPath("App_Data/Goverment.mdb");
        OleDbConnection conn = new OleDbConnection(connection);
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = conn;
        string strcmd = "select max("+nid+") from "+tablename;
        cmd.CommandText = strcmd;
        try
        {
            conn.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count < 0)
            {
                id = 1;
            }
            else
            {
                id = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + 1;//错误提示:输入字符串的格式不正确
            }
        }
        catch(Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
        }
        return id;
    }
id = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + 1提示错误:输入字符串格式不正确这是什么原因?忘大家帮帮忙看看!

解决方案 »

  1.   

    程序跟一下,ds.Tables[0].Rows[0][0].ToString()看一下这个值的情况
      

  2.   

    你把ds.Tables[0].Rows[0][0].ToString()输出
    看看是什么值啊?
      

  3.   

    我用string str=ds.Tables[0].Rows[0][0].ToString()运行到这里就报错,看不到结果。我用ExecuteScalar返回一个object,结果显示object的值为{}.到底是怎么回事?
    object o = cmd.ExecuteScalar();
                if (Object.Equals(o, null))
                {
                    id = 1;
                }
                else
                {
                    id = (int)o + 1;
                }
      

  4.   

    你在这 DataSet ds = new DataSet(); 
    设个断点 看看ds里面的数据 我估计ds 里面有一行 ds.Tables[0].Rows[0][0].ToString()不能转换为数字 可能是null和空  绝对是