Skip to main content

Autocomplete TextBox in Windows Form Application using SQL Server

Autocomplete TextBox in Windows Form Application using SQL Server.

In this article we Lear how to create autocompelete textbox on the basis of sql server data.

Step 1:

First open visual Studio ---->File---->New---->Project----->Windows From Application---->Ok
Drag and Drop label and TextBox.





Step 2:
Now Double Click on Windows Form and write the following Code :

        SqlConnection con = new SqlConnection(@"Data Source=IS9;Initial Catalog=practice;User ID=sa;Password=abc");
        public void loadData()
        {
            con.Open();
            AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
            string querry = @"Select distinct [name] from [bio]";
            SqlCommand cmd = new SqlCommand(querry, con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows == true)
            {
                while (dr.Read())
                    namesCollection.Add(dr["name"].ToString());

            }

            dr.Close();
            con.Close();

            textBox1.AutoCompleteMode = AutoCompleteMode.Append;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox1.AutoCompleteCustomSource = namesCollection;
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            loadData();
        }
    }

}



Output:
Press F5



Comments

Popular posts from this blog

How to Pass Data from one Form to other Form in windows Form Application C#

Introduction: Hi Guys! In this tutorial we learn how to pass data from one form to other form in windows form application using c#. Let’s Follow the Step and create windows form application. Step1: Visual studio- à File- à New- à Project à windows Form Application- à Ok Step2: Click on ToolBox- à Drag and drop Label and TextBox. I creat a Form with 3 Labels and TextBox as shown in fig. Step 3: I have Name,Fname,Address Label on Form .so I take a three global variable .Write this code on the Form1.cs         public static string SetValueForText1 = "" ;         public static string SetValueForText2 = "" ;         public static string SetValueForText3 = "" ; Step4: Project- à Add Windows Form- à Click on Add Step 5: After Creating Form Double Click on Submit button on windows Form1 and Write the Cod...