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:
- private void btn_add_Click(object sender, EventArgs e)
- {
- try
- {
- connect.Open();
- OleDbCommand cmd = new OleDbCommand();
- cmd.Connection = connect;
- cmd.CommandText = "insert into Phone (FirstName,LastName,MobileNo,EmailId) values('" + txt_fname.Text + "','" + txt_lname.Text + "','" + txt_mobile.Text + "','" + txt_email.Text + "')";
- cmd.ExecuteNonQuery();
- MessageBox.Show("Data Inserted");
- connect.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error" + ex);
- }
- }
For Delete Button- private void btn_delete_Click(object sender, EventArgs e)
- {
- try
- {
- connect.Open();
- OleDbCommand cmd = new OleDbCommand();
- cmd.Connection = connect;
- string query = "delete from Phone where FirstName='" + txt_fname.Text + "'";
- cmd.CommandText = query;
- MessageBox.Show(query);
- cmd.ExecuteNonQuery();
- MessageBox.Show("Data Deleted");
- connect.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error" + ex);
- }
- }
For Update Button- private void btn_update_Click(object sender, EventArgs e)
- {
- try
- {
- connect.Open();
- OleDbCommand cmd = new OleDbCommand();
- cmd.Connection = connect;
- 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 + "'";
- cmd.CommandText = query;
- MessageBox.Show(query);
- cmd.ExecuteNonQuery();
- MessageBox.Show("Data Updated");
- connect.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error" + ex);
- }
- }
For Load Button- private void btn_load_Click(object sender, EventArgs e)
- {
- try
- {
- connect.Open();
- OleDbCommand cmd = new OleDbCommand();
- cmd.Connection = connect;
- string query = "select * from Phone";
- cmd.CommandText = query;
- OleDbDataAdapter da = new OleDbDataAdapter(cmd);
- DataTable dt = new DataTable();
- da.Fill(dt);
- dataGridView1.DataSource = dt;
- MessageBox.Show("Data Loaded");
- connect.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error" + ex);
- }
- }
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.