3 Replies - 3772 Views - Last Post: 01 February 2011 - 11:08 AM Rate Topic: -----

#1 KXtra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-January 11

Using autocomplete custom source with array

Posted 01 February 2011 - 10:30 AM

Hello all,

First post to the site and I'm hoping someone can give me some assistance. I'm pretty much a newbie at c# although I have had a little programming experience.

I'm writing a small program that I will use to store client connection information in a SQL db and allow me to search it to display the information. I have 90% of the program completed. All the SQL queries work great, update, insert, search...well, search is the problem. I can run search queries all day long IF I know the name I'm looking for. This is where the problem occurs. I'm trying to use the autocomplete feature that's built in to VS and it's giving me a bit of a headache. I've been working on it for about 2 weeks now with the help of another developer at my work.

So...

I have created an array call aSearchContext.
//This variable contains the array that holds Dr Last Name and Practice Name for autosuggest
        public string[] aSearchContext = new string[100];


I have a method that populates this array and also saves it to a file (first it deletes the old file in case new values have been added). This works fine. (the temp.txt file referenced in line 11 and 27 is only for diagnostic purposes unless someone knows a way to get it to work as the autocomplete source...)
private void fGetData()
        {
            SqlConnection myConnection = new SqlConnection("user id=sqlremote;" +
                                                   "password=*****;" + "server=****;" +
                                                   "Trusted_Connection=no;" +
                                                   "database=ClientConnection;" +
                                                   "connection timeout=30;");
            SqlDataReader myDataReader = null;
            SqlCommand myCMD = null;
            int i = 0;
            File.Delete(@"c:\temp.txt");
            try
            {
                myConnection.Open();
                string cmdTxt = "SELECT LastName, PracticeName" +
                         " FROM dbo.ConnectionInfo";
                MessageBox.Show(cmdTxt);
                myCMD = new SqlCommand(cmdTxt);
                myCMD.Connection = myConnection;
                myDataReader = myCMD.ExecuteReader();
                while (myDataReader.Read())
                {
                    aSearchContext[i] = myDataReader.GetString(0);
                    aSearchContext[i + 1] = myDataReader.GetString(1);
                    i += 2;
                }
                StreamWriter sw = new StreamWriter(@"c:\temp.txt");
                i = 0;
                foreach (string s in aSearchContext)
                {
                    sw.WriteLine(aSearchContext[i]);
                    i++;
                }
                sw.Close();
            }
            catch (Exception a)
            {
                MessageBox.Show(a.Message);
            }
            finally
            {
                myDataReader.Close();
            }
        }



at the form load I call fGetData to populate the array and then I try to set the autocompletesource to the array.
 private void frmClient_Load(object sender, EventArgs e)
        {

            fGetData();
            try
            {
                tbSearchBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
                tbSearchBox.AutoCompleteCustomSource.AddRange(aSearchContext);
            }
            catch (Exception z)
            {
                MessageBox.Show(z.Message);
            }

            try
            {
                if (bLockSave)
                {
                    btnSave.Enabled = false;
                }
                if (bAddClient)
                {
                    lblFirstName.Visible = true;
                    tbFirstName.Visible = true;
                    lblLastName.Visible = true;
                    tbLastName.Visible = true;
                }
            }
            catch (Exception y)
            {
                MessageBox.Show(y.Message);
            }

        }



Here is what happens, no matter how many try-catch i put into the code, it doesn't throw an error, the program just closes whenever I begin typing into the search box. I have put breaks throughout the code to try and determine where the error occurs, but to no avail.

I've also tried different ways of completing the autocompletecustomsource like below (this is also in the load method but right now it's commented out.

            //int i = 0;
            //var source = new AutoCompleteStringCollection();
            //foreach (string s in aSearchContext)
            //{
            //    source.Add(aSearchContext[i]);
            //    i++;
            //}
            //tbSearchBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //tbSearchBox.AutoCompleteSource = AutoCompleteSource.CustomSource;



If I open the temp.txt file and copy the value directly into the autocompletecustomsource collection and leave out the instructions on the load procedure then the autocomplete works fine, but I don't want to have to hard code the values as the values will change frenquently.

This is a TextBox with the following properties
AutocompleteCustomSource - (Collection)
AutoCompleteMode - Append
AutoCompleteSource - CustomSource

Thanks in advance for any help and if you need clarification on anything let me know...

I will add that I have read PsychoCoders post on creating an autocomplete text box but i'm still so early into my programming life that i have a hard time following it... sry...

Is This A Good Question/Topic? 0
  • +

Replies To: Using autocomplete custom source with array

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Using autocomplete custom source with array

Posted 01 February 2011 - 10:33 AM

Maybe you are complicating things for yourself.

Check this out:
http://csharpdotnetf...ox-using-c.html
Was This Post Helpful? 1
  • +
  • -

#3 KXtra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-January 11

Re: Using autocomplete custom source with array

Posted 01 February 2011 - 11:00 AM

WOW...

Sergio, that was a fast response! I wasn't expecting anything for at least a day or two...

Yeah, I was way over complicating that.

I cleared all of my old information and modified the code based on the link you posted to work for my program...it only took about 10 lines of code...

Thank You!
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Using autocomplete custom source with array

Posted 01 February 2011 - 11:08 AM

No problem, glad to help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1