【问题标题】:Unable to cast object of type 'System.Int32' [closed]无法转换类型为“System.Int32”的对象 [关闭]
【发布时间】:2018-04-05 04:41:23
【问题描述】:

代码

SqlCommand cmd = new SqlCommand("select billno from tpdetail", con);
    con.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    AutoCompleteStringCollection MyCollection = new AutoCompleteStringCollection();
    while (reader.Read())
     {
        MyCollection.Add(reader.GetString(0).ToString());
     }
    textBox3.AutoCompleteCustomSource = MyCollection;

我得到以下异常:

无法将“System.Int32”类型的对象转换为“System.String”类型

【问题讨论】:

标签: c# sql-server


【解决方案1】:

那么,tpdetail 表中billno 列的数据类型 是什么?如果是INT,那么您需要将其阅读作为INT - 而不是作为字符串...。

while (reader.Read())
{
    MyCollection.Add(reader.GetInt(0).ToString());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 2020-08-07
    • 1970-01-01
    相关资源
    最近更新 更多