创建自动完成文本框

时间:2015-10-21 14:39:33

标签: c# winforms

这里创建的方法,我收到一个错误,我似乎无法理解它是如何发生请帮助!! 错误最佳重载方法匹配' System.Data.Common.DbDataReader.GetString(int)'有一些无效的论点

void AutoCompleteText()
    {
        search_txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        search_txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
        AutoCompleteStringCollection coll = new AutoCompleteStringCollection();

        OleDbCommand command = new OleDbCommand();
        command.Connection = conDB;
        command.CommandText = "select CCSpn_CODE,CCLname,CCFname,CCMname,CCDOB,CCgender,CCSchool,CaClass,CCVillage,CCSiblings,CCGuardian,CCContact,CCcurrentDt,CCImage from abaanaCC";
       // OleDbDataReader myreader;
        conDB.Open();


        OleDbDataReader myreader = command.ExecuteReader()


            while (myreader.Read())
            {
                //tring sName = myreader["CCLname"].ToString();
                string sName = myreader.GetString(1);
                //string sName = Convert.ToString(myreader.("CCLname"));
                coll.Add(sName);
            }

            search_txt.AutoCompleteCustomSource = coll;
            myreader.Close();


        conDB.Close();

    }

3 个答案:

答案 0 :(得分:1)

GetString方法接受int作为参数。您需要将指定列的索引发送到GetString,如下所示:

string sName = reader.GetString(1);

1表示第二列根据需要进行更改。

或者你可以试试这个:

string sName = reader["CCLname"].ToString();

要解决此错误:

  

行/列没有数据

您应该使用using阻止,因此您的代码应为:

string sqlCmD = "select CCSpn_CODE,CCLname,CCFname,CCMname,CCDOB,CCgender,CCSchool,CaClass,CCVillage,CCSiblings,CCGuardian,CCContact,CCcurrentDt,CCImage from abaanaCC";

using (OleDbConnection conDB = new OleDbConnection(address))
{
     OleDbCommand command = new OleDbCommand(sqlCmD, connection);

     conDB.Open();

     using(OleDbDataReader myreader = command.ExecuteReader())
     {
        if (myreader.Read())
        {
            string sName = myreader.GetString(1);
            ...
        }
     }
}

答案 1 :(得分:1)

string sName = reader.GetString(1);

应返回结果集中第二列中的值(在您的情况下为CCLname列)。

答案 2 :(得分:0)

也许这个

string sName = Convert.ToString(myreader.["CCLname"]);