string con = ConfigurationSettings.AppSettings["mc"];
        SqlConnection mycon = new SqlConnection(con);
       
        SqlCommand mycom = new SqlCommand("select * from bjb where  班级号=@classnom ", mycon);        mycom.Parameters.Add("@classnom", SqlDbType.Char);
        string novalue = TextBox1.Text.Trim();
        mycom.Parameters["@classnom"].Value = novalue;
        mycon.Open();
        SqlDataReader dr = mycom.ExecuteReader();
        
        if (dr.Read())
        {
            Label1.Text = dr["班级名"];//无法将类型“object”隐式转换为“string”。存在一个显式转换(是否缺少强制转换?)
           
        }这是怎么回事呀,object是什么类型?

解决方案 »

  1.   

    Label1.Text = dr["班级名"].ToString();
      

  2.   

    Label1.Text = dr["班级名"].ToString();
      

  3.   

    Label1.Text = dr["班级名"].toString();
      

  4.   

    dr["班级名"]  是一个Object类型 而Label1.Text需要赋一个string类型  object不能强制装换成string
      

  5.   

    不行呀,它提示
    无法将方法组“ToString”转换为非委托类型“string”。您是要调用方法吗?
      

  6.   

    哈哈,将一个DataRow 转换为string 当然出错了.
    楼上有很多正解
      

  7.   


    dr["班级名"]这样出来是Object类型。其实就是经过一层装箱的。一件东西在传输时为了安全或者其他因素起见,把它装了箱子,到目的地的时候你要把它拆箱,否则别人不知道这箱子里装的是什么啊。所以要拆箱/转换类型