Make A Simple Phone Directory Application

I am going to demonstrate you how to make a simple PhoneDirectoryApplication using the windows form application and c# language.



Figure 1: PD App

This is how it will look after completing the project.

Step 1



Figure 2: New Project

Step 2



Figure 3: Windows Form App

Step 3

Once you done with the naming project and clicking OK, you will be redirected to an empty blank Windows Form.



Figure 4: Windows Form

Step 4

Now you need to drag and drop few controls from toolbox window which are required.

For example, Buttons, Labels, TextBoxes, and DataGridView.



Figure 5: ToolTip

Step 6

Once you drag and drop the controls on an empty blank form your form will look like the following  and you can enhance the look and feel the controls on the form according to one’s requirement.



Figure 6: Look and Feel

Step 7

Then open Microsoft Access and create a table with some dummy values in it.



Figure 7: Dummy

Step 8

Now you need to import or mention one namespace in the above section where all namespaces are mentioned.

i.e : using System.Data.OleDb;



Figure 8: System Data

Step 9

After importing this namespace in your application, ensure that you can use Microsoft Access in your application.

Once importing the namespace, you need to create the instance(object ) of the OleDbConnection.



Figure 9: OleDbConnection

Step 10

Now write the connection string of Microsoft Access, every ConnectionString varies from other database. So you need to type one website for that i.e: http://www.connectionstrings.com/.


Figure 10: Connection String

Select the access option from there and copy paste the connection string from there that is available for access database



Figure 11: Access Point

Step 11

Now what you can do is you can double click on the add button which was created by you in the user interface section i.e on the windows form application, and just write the following code:

  1. private void btn_add_Click(object sender, EventArgs e)   
  2. {  
  3.     try   
  4.     {  
  5.         connect.Open();  
  6.         OleDbCommand cmd = new OleDbCommand();  
  7.         cmd.Connection = connect;  
  8.         cmd.CommandText = "insert into Phone (FirstName,LastName,MobileNo,EmailId) values('" + txt_fname.Text + "','" + txt_lname.Text + "','" + txt_mobile.Text + "','" + txt_email.Text + "')";  
  9.         cmd.ExecuteNonQuery();  
  10.         MessageBox.Show("Data Inserted");  
  11.         connect.Close();  
  12.     }   
  13.     catch (Exception ex)   
  14.     {  
  15.         MessageBox.Show("Error" + ex);  
  16.     }  
  17. }
For Delete Button
  1. private void btn_delete_Click(object sender, EventArgs e)   
  2. {  
  3.     try   
  4.     {  
  5.         connect.Open();  
  6.         OleDbCommand cmd = new OleDbCommand();  
  7.         cmd.Connection = connect;  
  8.         string query = "delete from Phone where FirstName='" + txt_fname.Text + "'";  
  9.         cmd.CommandText = query;  
  10.         MessageBox.Show(query);  
  11.         cmd.ExecuteNonQuery();  
  12.         MessageBox.Show("Data Deleted");  
  13.         connect.Close();  
  14.     }   
  15.     catch (Exception ex)   
  16.     {  
  17.         MessageBox.Show("Error" + ex);  
  18.     }  
  19. }  
For Update Button
  1. private void btn_update_Click(object sender, EventArgs e)   
  2. {  
  3.     try   
  4.     {  
  5.         connect.Open();  
  6.         OleDbCommand cmd = new OleDbCommand();  
  7.         cmd.Connection = connect;  
  8.         string query = "update Phone set FirstName='" + txt_fname.Text + "' ,LastName='" + txt_lname.Text + "' ,MobileNo='" + txt_mobile.Text + "' ,EmailId='" + txt_email.Text + "' where FirstName='" + txt_fname.Text + "'";  
  9.         cmd.CommandText = query;  
  10.         MessageBox.Show(query);  
  11.         cmd.ExecuteNonQuery();  
  12.         MessageBox.Show("Data Updated");  
  13.         connect.Close();  
  14.     }   
  15.     catch (Exception ex)  
  16.     {  
  17.         MessageBox.Show("Error" + ex);  
  18.     }  
  19. }
For Load Button
  1. private void btn_load_Click(object sender, EventArgs e)   
  2. {  
  3.     try   
  4.     {  
  5.         connect.Open();  
  6.         OleDbCommand cmd = new OleDbCommand();  
  7.         cmd.Connection = connect;  
  8.         string query = "select * from Phone";  
  9.         cmd.CommandText = query;  
  10.         OleDbDataAdapter da = new OleDbDataAdapter(cmd);  
  11.         DataTable dt = new DataTable();  
  12.         da.Fill(dt);  
  13.         dataGridView1.DataSource = dt;  
  14.         MessageBox.Show("Data Loaded");  
  15.         connect.Close();  
  16.     }   
  17.     catch (Exception ex)   
  18.     {  
  19.         MessageBox.Show("Error" + ex);  
  20.     }  
  21. }
Step 13

Congrats, you are done with the application, simply run and enjoy.

When you add the data to the database.



Figure 12: Add the data

You can view it in the table, whether your data has been inserted.



Figure 13: Data Insert

You can view whether the data has been inserted in your current application by clicking the LOAD button.

Same goes for the update and delete button.

Next Recommended Readings