首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

无法将类型“object”隐式转换为“string”?解决方案

2012-03-29 
无法将类型“object”隐式转换为“string”??string con ConfigurationSettings.AppSettings[mc]SqlConne

无法将类型“object”隐式转换为“string”??
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是什么类型?

[解决办法]
Label1.Text = dr["班级名"].ToString();
[解决办法]
Label1.Text = dr["班级名"].ToString();
[解决办法]
Label1.Text = dr["班级名"].toString();
[解决办法]
dr["班级名"] 是一个Object类型 而Label1.Text需要赋一个string类型 object不能强制装换成string

热点排行