C# - Simple Auto Complete List With SQLite

In this tutorial we will create a Simple Auto Complete List With SQLite using C#. C# is a general-purpose, object-oriented programming language. C# automatically manages inaccessible object memory using a garbage collector, which eliminates developer concerns and memory leaks. C# is faster than dynamically typed languages because things are more clearly defined. It contains several classes that support any C# platforms, like game development. It has a friendly environment for all new developers. So let's do the coding...

Getting Started

First you will have to download & install the Visual Studio. Visual Studios is an open source development feel free to create any application that you want. Here's the link for the Visual Studio https://www.visualstudio.com/. Here's the link for the SQLite Browser http://sqlitebrowser.org/.

Setting up SQLite

SQLite is very carefully tested prior to every release and relevant to use in some way. SQLite is very usable in any environments especially in embedded devices. First all you need to do is to install the components of the SQLIte database, by right clicking in the Main project title in the solution explorer then selecting the Manage NuGet Packages. tut1 Then go to the browse and search sqlite, after that install it and wait until the process is completed. tut2 Next go to the Updates and update the needed framework to make sqlite work properly. tut3 Note: Update only the framework if there is an available new update.

Application Design

We will now create the design for the application, first locate the designer file called form1.Designer.cs, this is the default name when you create a new windows form. Rename the form as Main.cs and then write these codes inside your designer file.
  1. namespace Simple_AutoComplete_Text
  2. {
  3.     partial class Main
  4.     {
  5.         /// <summary>
  6.         /// Required designer variable.
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;
  9.  
  10.         /// <summary>
  11.         /// Clean up any resources being used.
  12.         /// </summary>
  13.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14.         protected override void Dispose(bool disposing)
  15.         {
  16.             if (disposing && (components != null))
  17.             {
  18.                 components.Dispose();
  19.             }
  20.             base.Dispose(disposing);
  21.         }
  22.  
  23.         #region Windows Form Designer generated code
  24.  
  25.         /// <summary>
  26.         /// Required method for Designer support - do not modify
  27.         /// the contents of this method with the code editor.
  28.         /// </summary>
  29.         private void InitializeComponent()
  30.         {
  31.             this.btn_submit = new System.Windows.Forms.Button();
  32.             this.label1 = new System.Windows.Forms.Label();
  33.             this.groupBox1 = new System.Windows.Forms.GroupBox();
  34.             this.txt_country = new System.Windows.Forms.TextBox();
  35.             this.label2 = new System.Windows.Forms.Label();
  36.             this.txt_name = new System.Windows.Forms.TextBox();
  37.             this.groupBox1.SuspendLayout();
  38.             this.SuspendLayout();
  39.             //
  40.             // btn_submit
  41.             //
  42.             this.btn_submit.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  43.             this.btn_submit.Location = new System.Drawing.Point(177, 297);
  44.             this.btn_submit.Name = "btn_submit";
  45.             this.btn_submit.Size = new System.Drawing.Size(131, 40);
  46.             this.btn_submit.TabIndex = 0;
  47.             this.btn_submit.Text = "Submit";
  48.             this.btn_submit.UseVisualStyleBackColor = true;
  49.             this.btn_submit.Click += new System.EventHandler(this.Submit);
  50.             //
  51.             // label1
  52.             //
  53.             this.label1.AutoSize = true;
  54.             this.label1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  55.             this.label1.Location = new System.Drawing.Point(76, 100);
  56.             this.label1.Name = "label1";
  57.             this.label1.Size = new System.Drawing.Size(86, 27);
  58.             this.label1.TabIndex = 1;
  59.             this.label1.Text = "NAME:";
  60.             //
  61.             // groupBox1
  62.             //
  63.             this.groupBox1.Controls.Add(this.txt_country);
  64.             this.groupBox1.Controls.Add(this.label2);
  65.             this.groupBox1.Controls.Add(this.txt_name);
  66.             this.groupBox1.Controls.Add(this.label1);
  67.             this.groupBox1.Controls.Add(this.btn_submit);
  68.             this.groupBox1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  69.             this.groupBox1.Location = new System.Drawing.Point(12, 12);
  70.             this.groupBox1.Name = "groupBox1";
  71.             this.groupBox1.Size = new System.Drawing.Size(489, 354);
  72.             this.groupBox1.TabIndex = 2;
  73.             this.groupBox1.TabStop = false;
  74.             this.groupBox1.Text = "Member Information";
  75.             //
  76.             // txt_country
  77.             //
  78.             this.txt_country.AutoCompleteCustomSource.AddRange(new string[] {
  79.             "Afghanistan",
  80.             "Albania",
  81.             "Algeria",
  82.             "Andorra",
  83.             "Angola",
  84.             "Anguilla",
  85.             "Antigua & Barbuda",
  86.             "Argentina",
  87.             "Armenia",
  88.             "Australia",
  89.             "Austria",
  90.             "Azerbaijan",
  91.             "Bahamas",
  92.             "Bahrain",
  93.             "Bangladesh",
  94.             "Barbados",
  95.             "Belarus",
  96.             "Belgium",
  97.             "Belize",
  98.             "Benin",
  99.             "Bermuda",
  100.             "Bhutan",
  101.             "Bolivia",
  102.             "Bosnia & Herzegovina",
  103.             "Botswana",
  104.             "Brazil",
  105.             "Brunei Darussalam",
  106.             "Bulgaria",
  107.             "Burkina Faso",
  108.             "Myanmar/Burma",
  109.             "Burundi",
  110.             "Cambodia",
  111.             "Cameroon",
  112.             "Canada",
  113.             "Cape Verde",
  114.             "Cayman Islands",
  115.             "Central African Republic",
  116.             "Chad",
  117.             "Chile",
  118.             "China",
  119.             "Colombia",
  120.             "Comoros",
  121.             "Congo",
  122.             "Costa Rica",
  123.             "Croatia",
  124.             "Cuba",
  125.             "Cyprus",
  126.             "Czech Republic",
  127.             "Democratic Republic of the Congo",
  128.             "Denmark",
  129.             "Djibouti",
  130.             "Dominica",
  131.             "Dominican Republic",
  132.             "Ecuador",
  133.             "Egypt",
  134.             "El Salvador",
  135.             "Equatorial Guinea",
  136.             "Eritrea",
  137.             "Estonia",
  138.             "Ethiopia",
  139.             "Fiji",
  140.             "Finland",
  141.             "France",
  142.             "French Guiana",
  143.             "Gabon",
  144.             "Gambia",
  145.             "Georgia",
  146.             "Germany",
  147.             "Ghana",
  148.             "Great Britain",
  149.             "Greece",
  150.             "Grenada",
  151.             "Guadeloupe",
  152.             "Guatemala",
  153.             "Guinea",
  154.             "Guinea-Bissau",
  155.             "Guyana",
  156.             "Haiti",
  157.             "Honduras",
  158.             "Hungary",
  159.             "Iceland",
  160.             "India",
  161.             "Indonesia",
  162.             "Iran",
  163.             "Iraq",
  164.             "Israel and the Occupied Territories",
  165.             "Italy",
  166.             "Ivory Coast (Cote d\'Ivoire)",
  167.             "Jamaica",
  168.             "Japan",
  169.             "Jordan",
  170.             "Kazakhstan",
  171.             "Kenya",
  172.             "Kosovo",
  173.             "Kuwait",
  174.             "Kyrgyz Republic (Kyrgyzstan)",
  175.             "Laos",
  176.             "Latvia",
  177.             "Lebanon",
  178.             "Lesotho",
  179.             "Liberia",
  180.             "Libya",
  181.             "Liechtenstein",
  182.             "Lithuania",
  183.             "Luxembourg",
  184.             "Republic of Macedonia",
  185.             "Madagascar",
  186.             "Malawi",
  187.             "Malaysia",
  188.             "Maldives",
  189.             "Mali",
  190.             "Malta",
  191.             "Martinique",
  192.             "Mauritania",
  193.             "Mauritius",
  194.             "Mayotte",
  195.             "Mexico",
  196.             "Moldova, Republic of",
  197.             "Monaco",
  198.             "Mongolia",
  199.             "Montenegro",
  200.             "Montserrat",
  201.             "Morocco",
  202.             "Mozambique",
  203.             "Namibia",
  204.             "Nepal",
  205.             "Netherlands",
  206.             "New Zealand",
  207.             "Nicaragua",
  208.             "Niger",
  209.             "Nigeria",
  210.             "Korea, Democratic Republic of (North Korea)",
  211.             "Norway",
  212.             "Oman",
  213.             "Pacific Islands",
  214.             "Pakistan",
  215.             "Panama",
  216.             "Papua New Guinea",
  217.             "Paraguay",
  218.             "Peru",
  219.             "Philippines",
  220.             "Poland",
  221.             "Portugal",
  222.             "Puerto Rico",
  223.             "Qatar",
  224.             "Reunion",
  225.             "Romania",
  226.             "Russian Federation",
  227.             "Rwanda",
  228.             "Saint Kitts and Nevis",
  229.             "Saint Lucia",
  230.             "Saint Vincent\'s & Grenadines",
  231.             "Samoa",
  232.             "Sao Tome and Principe",
  233.             "Saudi Arabia",
  234.             "Senegal",
  235.             "Serbia",
  236.             "Seychelles",
  237.             "Sierra Leone",
  238.             "Singapore",
  239.             "Slovak Republic (Slovakia)",
  240.             "Slovenia",
  241.             "Solomon Islands",
  242.             "Somalia",
  243.             "South Africa",
  244.             "Korea, Republic of (South Korea)",
  245.             "South Sudan",
  246.             "Spain",
  247.             "Sri Lanka",
  248.             "Sudan",
  249.             "Suriname",
  250.             "Swaziland",
  251.             "Sweden",
  252.             "Switzerland",
  253.             "Syria",
  254.             "Tajikistan",
  255.             "Tanzania",
  256.             "Thailand",
  257.             "Timor Leste",
  258.             "Togo",
  259.             "Trinidad & Tobago",
  260.             "Tunisia",
  261.             "Turkey",
  262.             "Turkmenistan",
  263.             "Turks & Caicos Islands",
  264.             "Uganda",
  265.             "Ukraine",
  266.             "United Arab Emirates",
  267.             "United States of America (USA)",
  268.             "Uruguay",
  269.             "Uzbekistan",
  270.             "Venezuela",
  271.             "Vietnam",
  272.             "Virgin Islands (UK)",
  273.             "Virgin Islands (US)",
  274.             "Yemen",
  275.             "Zambia",
  276.             "Zimbabwe"});
  277.             this.txt_country.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
  278.             this.txt_country.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
  279.             this.txt_country.Location = new System.Drawing.Point(177, 199);
  280.             this.txt_country.Name = "txt_country";
  281.             this.txt_country.Size = new System.Drawing.Size(262, 35);
  282.             this.txt_country.TabIndex = 4;
  283.             //
  284.             // label2
  285.             //
  286.             this.label2.AutoSize = true;
  287.             this.label2.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  288.             this.label2.Location = new System.Drawing.Point(27, 202);
  289.             this.label2.Name = "label2";
  290.             this.label2.Size = new System.Drawing.Size(135, 27);
  291.             this.label2.TabIndex = 3;
  292.             this.label2.Text = "COUNTRY:";
  293.             //
  294.             // txt_name
  295.             //
  296.             this.txt_name.Location = new System.Drawing.Point(177, 97);
  297.             this.txt_name.Name = "txt_name";
  298.             this.txt_name.Size = new System.Drawing.Size(262, 35);
  299.             this.txt_name.TabIndex = 2;
  300.             //
  301.             // Main
  302.             //
  303.             this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
  304.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  305.             this.ClientSize = new System.Drawing.Size(518, 378);
  306.             this.Controls.Add(this.groupBox1);
  307.             this.Name = "Main";
  308.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  309.             this.Text = "Main";
  310.             this.groupBox1.ResumeLayout(false);
  311.             this.groupBox1.PerformLayout();
  312.             this.ResumeLayout(false);
  313.  
  314.         }
  315.  
  316.         #endregion
  317.  
  318.         private System.Windows.Forms.Button btn_submit;
  319.         private System.Windows.Forms.Label label1;
  320.         private System.Windows.Forms.GroupBox groupBox1;
  321.         private System.Windows.Forms.TextBox txt_country;
  322.         private System.Windows.Forms.Label label2;
  323.         private System.Windows.Forms.TextBox txt_name;
  324.     }
  325. }
or also you create the layout by dragging the proper tools to the forms. To make a Auto Complete TextBox, just click the textbox then go to the properties and change the settings of the autocomplete as shown below. tut4 After that select the ... in the AutoCompleteCustomSource then put all the needed string here, tut5

Creating the Script

We will now create the script to make things work. To do that go to the csharp script called Main.cs then right click and select view code, this will force you to go to the text editor. Then write these block of codes inside the Class of the form.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SQLite;
  11. using System.IO;
  12.  
  13. namespace Simple_AutoComplete_Text
  14. {
  15.     public partial class Main : Form
  16.     {
  17.         SQLiteConnection conn;
  18.         SQLiteCommand cmd;
  19.         String connectString;
  20.  
  21.  
  22.         public Main()
  23.         {
  24.             InitializeComponent();
  25.             connectString = @"Data Source=" + Application.StartupPath + @"\data.db;version=3";
  26.             GenerateDatabase();
  27.         }
  28.  
  29.         private void Submit(object sender, EventArgs e) {
  30.             if (txt_name.Text != "" && txt_country.Text != "")
  31.             {
  32.                 try {
  33.                     conn = new SQLiteConnection(connectString);
  34.                     cmd = new SQLiteCommand();
  35.                     cmd.CommandText = @"INSERT INTO member (name, country) VALUES(@name, @country)";
  36.                     cmd.Connection = conn;
  37.                     cmd.Parameters.Add(new SQLiteParameter("@name", txt_name.Text));
  38.                     cmd.Parameters.Add(new SQLiteParameter("@country", txt_country.Text));
  39.                     conn.Open();
  40.  
  41.                     int i = cmd.ExecuteNonQuery();
  42.  
  43.                     if (i == 1)
  44.                     {
  45.                         MessageBox.Show("Successfully Created!");
  46.                         txt_name.Text = "";
  47.                         txt_country.Text = "";
  48.                     }
  49.  
  50.                     conn.Close();
  51.                 }
  52.                 catch (Exception ex) {
  53.                     MessageBox.Show(ex.Message);
  54.                 }
  55.             }
  56.             else {
  57.                 MessageBox.Show("Required Fields!");
  58.             }
  59.         }
  60.  
  61.         private void GenerateDatabase() {
  62.             String path = Application.StartupPath + @"\data.db";
  63.             if (!File.Exists(path)) {
  64.                 conn = new SQLiteConnection(connectString);
  65.                 conn.Open();
  66.                 string sql = "CREATE TABLE member (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, country TEXT)";
  67.                 cmd = new SQLiteCommand(sql, conn);
  68.                 cmd.ExecuteNonQuery();
  69.                 conn.Close();
  70.             }
  71.         }
  72.  
  73.  
  74.     }
  75. }
Try to run the application and see if it works. There you go we successfully created a Simple Auto Complete List With SQLite using C#. I hope that this tutorial help you understand on how to develop an application using C#. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment