How To Make A Simple Dictionary Application Using MySQL And Windows Form Application In C#

Steps for building/creating a Simple Dictionary Application

Prerequisite

  • Microsoft Visual Studio any version (2008/2010/2012/2013)
  • MySQL

Step 1: Open Microsoft Visual Studio by clicking on the start button, All programs, then Microsoft Visual Studio.

Step 2: Select File, new, project, then under installed templates select Visual C#. After that select windows option and then choose windows form application and provide the name for the application and select where you wish to save your project

Step 3: There will be default form which will be displayed in the IDE. Now drag and drop the required controls/components from the toolbox which is on the left side of the IDE. If the toolbox is not displayed goto the view tab and there you will find the toolbox

Step 4: After the controls/components are dragged and dropped from the toolbox, simply right click on the control and select the property and change the look and feel of that control according to one’s choice.

Step 5: Once the property of the controls are changed then click on the button which you might have dragged and dropped.

Sample Form is shown below:

Sample Form is shown

Simply double click on the button and you will be redirected to the Button_Click event and you have to write the following code:

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         con.Open();  
  6.         s = "select * from dictionary.dictionaryApp where word='" + this.Search_txt.Text + "' ";  
  7.         cmd = new MySqlCommand(s, con);  
  8.         myReader = cmd.ExecuteReader();  
  9.         if (myReader.Read())  
  10.         {  
  11.             lbl_type.Text = myReader.GetString("type");  
  12.             textBox2.Text = myReader.GetString("meaning");  
  13.             lbl_synonym.Text = myReader.GetString("synonyn");  
  14.             lbl_antonym.Text = myReader.GetString("antonym");  
  15.             lbl_sorry.Text = "";  
  16.         }  
  17.         else  
  18.         {  
  19.             lbl_sorry.Text = "Sorry no such word found!! Try different word";  
  20.             lbl_type.Text = "";  
  21.             textBox2.Text = "";  
  22.             lbl_synonym.Text = "";  
  23.             lbl_antonym.Text = "";  
  24.         }  
  25.     }  
  26.     catch (Exception ex)  
  27.     {  
  28.         MessageBox.Show(ex.Message);  
  29.     }  
  30.     finally  
  31.     {  
  32.         myReader.Close();  
  33.         con.Close();  
  34.     }  
  35. }  
This code will give you the search functionality in the application.

Along with that you also need to write the code for the textbox in the search form, this textbox is not a regular textbox, it will function as autocomplete textbox. For this you will create a user defined function and the code for that function is mentioned below and you need to call this function below the IntializeComponent(); function which is there in the constructor of the form i.e:public Form1().

The code is mentioned below:
  1. public Form1()  
  2. {  
  3.     InitializeComponent();  
  4.     AutoCompleteText();  
  5.     timer1.Start();  
  6. }  
  7.   
  8.   
  9. void AutoCompleteText()  
  10. {  
  11.     Search_txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;  
  12.     Search_txt.AutoCompleteSource = AutoCompleteSource.CustomSource;  
  13.     AutoCompleteStringCollection col = new AutoCompleteStringCollection();  
  14.   
  15.     string data = "datasource=localhost;port=3306;username=root;password=root";  
  16.     string query = "select * from dictionary.dictionaryApp";  
  17.     MySqlConnection con = new MySqlConnection(data);  
  18.     MySqlCommand cmd = new MySqlCommand(query, con);  
  19.     MySqlDataReader myReader;  
  20.     try  
  21.     {  
  22.         con.Open();  
  23.         myReader = cmd.ExecuteReader();  
  24.         while (myReader.Read())  
  25.         {  
  26.             string sword = myReader.GetString("word");  
  27.             col.Add(sword);  
  28.         }  
  29.     }  
  30.     catch (Exception ex)  
  31.     {  
  32.         MessageBox.Show(ex.Message);  
  33.     }  
  34.     Search_txt.AutoCompleteCustomSource = col;  
  35. }  
Step 6: Now you need to drag and drop the menustrip from the toolbox and add 2 tabs followed by:

 

  1. File
  2. About

And then in the File tab there should be 4 more sub-tabs which are:

  1. Add Word
  2. Update wordlist
  3. Delete Word
  4. Exit

And no sub-tab for About tab.

Step 7: Now, simply double click on the Add Word sub-tab and you will find there will be automated event generated for you like this:

  1. private void addWordToolStripMenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.   
  4. }  
  5.   
  6. Simply write the above code into the event which is been generated for you:  
  7.   
  8. private void addWordToolStripMenuItem_Click(object sender, EventArgs e)  
  9. {  
  10.    Form2 f = new Form2();  
  11.    f.Show();  
  12. }  
Step 8: Now you have to right click on the root folder in the solution explorer, and add new form

Step 9: Once you have completed till this step, drag and drop the labels, textboxes and button according to your desire. For example,

textboxes and button

Step 10: Now you need to double click on the add button which you have created in this form and write the following code:
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     string str = "datasource=localhost;port=3306;username=root;password=root";  
  4.     string query = "insert into dictionary.dictionaryApp(word,type,meaning,synonyn,antonym) values('" + this.txt_word.Text + "','" + this.txt_type.Text + "','" + this.txt_meng.Text + "','" + this.txt_syn.Text + "','" + this.txt_ant.Text + "'); ";  
  5.     MySqlConnection con = new MySqlConnection(str);  
  6.     MySqlCommand cmd = new MySqlCommand(query, con);  
  7.     MySqlDataReader myReader;  
  8.     try  
  9.     {  
  10.         con.Open();  
  11.         myReader = cmd.ExecuteReader();  
  12.         while (myReader.Read())  
  13.         {  
  14.   
  15.         }  
  16.         MessageBox.Show("Word Added Succesfully");  
  17.         this.Dispose();  
  18.     }  
  19.     catch (Exception ex)  
  20.     {  
  21.         MessageBox.Show(ex.Message);  
  22.     }  
  23. }  
Step 11: Once you done with that now simply go to your first form and double click on the Update WordList sub-tab and you will find the same menustrip event generated. In that you just mention the code which was earlier written in the previous event of menustrip.
  1. private void updateWordListToolStripMenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.    Form3 f = new Form3();  
  4.    f.Show();  
  5. }  
Step 12: Once again you need to right click on to your root folder and add a new form and add the controls you want to add.

Sample form is shown below:

add a new form

Step 13: Now double click on the button and you will be redirected to the event generated by the button and you need to write the following code which is mentioned below:
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     string str = "datasource=localhost;port=3306;username=root;password=root";  
  4.     string query = "update dictionary.dictionaryApp set word='" + this.txt_word.Text + "',meaning='" + this.txt_meang.Text + "' where word='" + this.txt_word.Text + "'";  
  5.     MySqlConnection con = new MySqlConnection(str);  
  6.     MySqlCommand cmd = new MySqlCommand(query, con);  
  7.     MySqlDataReader myReader;  
  8.     try  
  9.     {  
  10.         con.Open();  
  11.         myReader= cmd.ExecuteReader();  
  12.         MessageBox.Show("Data Updated");  
  13.         while (myReader.Read())  
  14.         {  
  15.   
  16.         }  
  17.         this.Dispose();  
  18.     }  
  19.     catch (Exception ex)  
  20.     {  
  21.         MessageBox.Show(ex.Message);  
  22.     }  
  23. }  
Step 14: Now once again go to your form1 and double click on the Delete Word sub-tab. You will find one same menustrip event generated. Simply write the following code:
  1. private void deleteWordToolStripMenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.    Form4 f = new Form4();  
  4.    f.Show();  
  5. }  
Step 15: Again right click on the root folder and add a new form and drag and drop the controls according to your requirement.

Sample form is shown below:

Sample form

Step 16: Now you need to double click on the button and there will be a button-click event which is automatically generated. Simply write this code into that event.
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.   
  4.     DialogResult d = MessageBox.Show("Do you really want to delete ""delete", MessageBoxButtons.YesNo);  
  5.     if (d == DialogResult.Yes)  
  6.     {  
  7.   
  8.   
  9.         string str = "datasource=localhost;port=3306;username=root;password=root";  
  10.         string query = "delete from dictionary.dictionaryApp where word='" + this.textBox1.Text + "'";  
  11.         MySqlConnection conn = new MySqlConnection(str);  
  12.   
  13.         MySqlCommand cmd = new MySqlCommand(query, conn);  
  14.         MySqlDataReader myReader;  
  15.   
  16.         try  
  17.         {  
  18.   
  19.             conn.Open();  
  20.             myReader = cmd.ExecuteReader();  
  21.             MessageBox.Show("Word Deleted");  
  22.             while (myReader.Read())  
  23.             {  
  24.             }  
  25.             this.Close();  
  26.         }  
  27.               
  28.         catch (Exception ex)  
  29.         {  
  30.             MessageBox.Show(ex.Message);  
  31.         }  
  32.     }  
  33.     else  
  34.     {  
  35.         MessageBox.Show("You'r word is safe");  
  36.         this.Dispose();  
  37.     }  
  38. }  
Step 17: Now simply follow the same steps as you followed for the menustrip. Go to your main form that is Form1 designer mode and double click on the “exit” sub tab and it will be generating the automated event of menustrip where you are suppose to write the following code:
  1. private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)  
  2. {  
  3.    Application.Exit();  
  4. }  
Step 18: Now for the about tab no need to create another form; simply right click on the root folder and select add new items. After that scroll down a little below and you will find one About Box. Select that and add your information according to your requirement.

A sample will look like the following;

sample

Now this was all about the UserInterface coding or the front end coding but there is still something which is needed. You will require to write the namespace in every form because we are using the database which is “MySql”. This namespace which you will write will allow the visual studio to create a link between visual studio and MySQL.

The namespace which you required is written below:
  1. using MySql.Data.MySqlClient;  
Once you wrote that you will be linked with the MySQL Database.

Steps To Create The Database And Required Table

Step 1: Open the MySql Command prompt:

MySql Command prompt

Step 2: Type your password which you have allotted to your MySQL and the above screenshot will be the result when you type the correct password into your MySQL command prompt.

Step 3: Firstly,t you need to create the Database for your application, so you will require to write a query for that that is written below:

CREATE DATABASE <DATABASE-NAME>;

For e.g: we have taken
  1. CREATE DATABASE DICTIONARY;  
Hit ENTER button.

Step 4: Now you need to use that Database so write the query which is written below:

USE DICTIONARY;

USE DICTIONARY

Step 5: Now you need to create a table in the database or that query is mentioned below:
  1. CREATE TABLE DICTIONARYAPP (  
  2.    WORD VARCHAR(40) PRIMARY KEY NOT NULL,  
  3.    TYPE VARCHAR(40) NOT NULL,  
  4.    MEANING VARCHAR(1000) NOT NULL,  
  5.    SYNONYM VARCHAR(50) NOT NULL,  
  6.    ANTONYM VARCHAR(50) NOT NULL  
  7. );  
Step 6: Now you can insert some values into the table for the demonstration purpose of the application.
For that the query is mentioned below:

query

Step 7: Now you can run your application. Once your database and table is created there are some values already into your table so that you can test your application is running well.

Step 8: Ensure, few things which are very significant while running the application like you mentioned the database name and table which you have created into the program accurately. It sometimes gives an error, if one have not appropriately mentioned the database name or table’s name.

 


Similar Articles