-2

CODE

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;

I get the following exception:

Unable to cast object of type 'System.Int32' to type 'System.String'

user9405863
  • 1,506
  • 1
  • 11
  • 16
  • Not enough information here... probably MyCollection expects an int instead of a string – johnny 5 Apr 05 '18 at 05:02
  • duplicate its already has answer here:https://stackoverflow.com/questions/19582256/unable-to-cast-object-of-type-system-int32-to-type-system-string-in-dataread – user9405863 Apr 05 '18 at 05:24

1 Answers1

3

Well, what is the datatype of your billno column in the tpdetail table?? If it's an INT, then you need to read it as an INT - not as a string ....

while (reader.Read())
{
    MyCollection.Add(reader.GetInt(0).ToString());
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459