0

I want to use the textbox search_txt to filter my DataGridView values, such that whatever letter entered in the textbox auto completes and I am getting an error

Here's the method created

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;
        try
        {
            conDB.Open();
            myreader = command.ExecuteReader();

            while (myreader.Read())
            {
               //Error Here!!!!! 
                string sName = myreader.GetString("CCLname")
                coll.Add(sName);
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);

        }
        search_txt.AutoCompleteCustomSource = coll;

    }

Here's code that loads the DataGridView

 private void btnloaddata_Click(object sender, EventArgs e)
    {
        try
        {
            conDB.Open();
            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";
            OleDbDataAdapter da = new OleDbDataAdapter(command);
            DataTable dt = new DataTable();
            da.Fill(dt);

            dataGridView1.DataSource = dt;
           // this.dataGridView1.Sort(this.dataGridView1.Columns["CCLname"], ListSortDirection.Ascending);


        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to Load Data");
        }
        conDB.Close();

    }
5
  • What is the error that you get? Commented Oct 21, 2015 at 13:20
  • The Error is The best Overloaded method match for 'System.Data.Common.DbDataReader.GetString(int)' has some invalid arguments Commented Oct 21, 2015 at 15:17
  • There may be a chance that you are getting a null value and it fails when converting it to a string. Is there a way for you to check that? Commented Oct 21, 2015 at 19:34
  • got a solution that already Commented Oct 21, 2015 at 23:36
  • Then it might be handy to still post that solution, incase future users mght have the same problem. Commented Oct 22, 2015 at 6:45

1 Answer 1

1

here's the solution for the AutoCompleteMethod

  void AutoCompleteTxtBox() 
    {
        txt_Search.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        txt_Search.AutoCompleteSource = AutoCompleteSource.CustomSource;
        AutoCompleteStringCollection coll = new AutoCompleteStringCollection();
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\ELIJAH\vs_Proj\Abaana\Abaana\abaana.mdb";
        conn.Open();
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = conn;
        cmd.CommandText = "select CCSpn_CODE ,CCLname ,CCFname ,CCMname,CCDOB,CCgender,CCSchool,CaClass,CCVillage,CCSiblings,CCGuardian ,CCContact,CCcurrentDt,CCImage from abaanaCC";
        //cmd.CommandText = "select * from abaanaCC";
        OleDbDataReader myReader = cmd.ExecuteReader();
        while (myReader.Read())
        {//error was here
            string lname = myReader["CCLname"].ToString();
            coll.Add(lname);
       }
        txt_Search.AutoCompleteCustomSource = coll;
        conn.Close();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.