locked
Need Help with Auto-Complete; textBox1 Mapped to and SQL Server DB

    Frage

  • Dear C# experts!  I am trying to run the auto-complete code from here:

    http://www.csharpaspnetarticles.com/2009/01/winforms-autocomplete-textbox-using-c.html

     

    I modified it a bit to work with my SLQ Server DB.  Here’s what I have now:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.Data.SqlClient;

    using System.Configuration;

    using System.Collections.Specialized;

     

     

    namespace WindowsFormsApplication3

    {

        publicpartialclassForm1: Form

        {

     

            //public string strConnection = ConfigurationManager.AppSettings["ConnString"];

            //AutoCompleteStringCollection namesCollection =

            //new AutoCompleteStringCollection();

     

     

            publicForm1()

            {

                InitializeComponent();

            }

     

                    publicpartialclassfrmAuto: Form

            {

                publicstringstrConnection =

                ConfigurationManager.AppSettings["ConnString"];

                AutoCompleteStringCollectionnamesCollection =

                newAutoCompleteStringCollection();

     

                privatevoidForm1_Load(objectsender, EventArgse)

                {

                    SqlDataReaderdReader;

                    SqlConnectionconn = newSqlConnection("Server='Excel-PC';Database='Northwind';Trusted_Connection=True");

                    conn.ConnectionString = strConnection;

                    SqlCommandcmd = newSqlCommand();

                    cmd.Connection = conn;

                    cmd.CommandType = CommandType.Text;

                    cmd.CommandText =

                    "Select distinct [CompanyName] from [Customers]"+

                    " order by [CompanyName] asc";

                    conn.Open();

                    dReader = cmd.ExecuteReader();

                    if(dReader.HasRows == true)

                    {

                        while(dReader.Read())

                            namesCollection.Add(dReader["CompanyName"].ToString());

                    }

                    else

                    {

                        MessageBox.Show("Data not found");

                    }

                    dReader.Close();

     

                    textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;

                    textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

                    textBox1.AutoCompleteCustomSource = namesCollection;

     

                }

                privatevoidbtnCancel_Click(objectsender, EventArgse)

                {

                    Application.Exit();

                }

                privatevoidbtnOk_Click(objectsender, EventArgse)

                {

                    MessageBox.Show("Hope you like this example");

                }

     

            }

        }

     

    }

     

     

     

    I’m getting the following errors (which don’t make sense to me):

    Error    1          The name 'ConfigurationManager' does not exist in the current context

     

    I have this:

    using System.Configuration;

     

    Though it would take care of the error, but nope.

     

    Also, 3 red squiggle lines under textBox1:

    Error    2          Cannot access a non-static member of outer type 'WindowsFormsApplication3.Form1' via nested type 'WindowsFormsApplication3.Form1.frmAuto'

     

    I have a ‘textBox1’ and the case sensitivity matches, so I’m not sure what’s causing this error.

     

    Can someone please help me get this working?

     

    Thanks everyone!!

     

     

     

    Montag, 5. März 2012 04:50

Antworten

Alle Antworten

  • Does anyone know how to do this???  I know a little C#, but obviously not as much as I thought!!
    Dienstag, 6. März 2012 04:13
  • I figure it out!!  Solution here!!

    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/353316f5-dd3a-4515-a262-22b9de49a1d4

    Hope that helps others!!

    Dienstag, 6. März 2012 04:58
  • Hi ryguy72,

    I'm glad to hear that you got it working. If you have any difficulty in future programming, we welcome you to post here again.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    Mittwoch, 7. März 2012 01:52