【发布时间】:2013-11-04 02:58:33
【问题描述】:
我试图将数据库中的数据添加到组合框。
try
{
SqlCeCommand com = new SqlCeCommand("select * from Category_Master", con);
SqlCeDataReader dr = com.ExecuteReader();
while(dr.Read()){
string name = dr.GetString(1);
cmbProductCategory.Items.Add(name);
}
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
我得到以下异常:
无法将“System.Int32”类型的对象转换为“System.String”类型
我在这里错过了什么?
【问题讨论】:
-
你遇到了什么异常??尝试使用
Convert.ToString(value)而不是value.ToString() -
无法将“System.Int32”类型的对象转换为“System.String”类型。
-
我改成了dr.GetString(1).ToString(),但是错误还是一样。
-
显然,数据读取器的索引为 1 的列有一个值,其类型是
System.Int32,而不是System.String。 -
为什么只选择所有列,而您只阅读其中的一个?只需选择您想要的列 - 这将减少出错的可能性。
标签: c# sqldatareader