In this blog post I'm going to show you how to create a phonebook from scratch using C# language. I'm creating this project on visual studio 2012 platform. The entire tutorial consists of 4 videos, that will walk you through the entire process of creating the project.
Here is the first video of the project.
Here is the SQL code
Here is the first video of the project.
Here is the second video of the project.
Here is the third video of the project.
Here is the forth video of the project.
Here is the code which I've used
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace PhoneBook { public partial class PhoneBook : Form { SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename='D:\C sharp\Projects\PhoneBook\PhoneBook\phonebook.mdf';Integrated Security=True;Connect Timeout=30;"); public PhoneBook() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } private void PhoneBook_Load(object sender, EventArgs e) { Display(); this.ActiveControl = textBox1; textBox1.Focus(); } private void panel2_Paint(object sender, PaintEventArgs e) { } private void button3_Click(object sender, EventArgs e) { textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); comboBox1.SelectedIndex = -1; textBox1.Focus(); } private void button4_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand(@" INSERT INTO PhoneBook (FirstName,LastName,Mobile,Email,Catagory) VALUES ('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+comboBox1.Text+"')",con); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Successfully Saved!"); Display(); } void Display() { SqlDataAdapter sda = new SqlDataAdapter("Select * from PhoneBook",con); DataTable dt = new DataTable(); sda.Fill(dt); dataGridView1.Rows.Clear(); foreach (DataRow item in dt.Rows) { int n = dataGridView1.Rows.Add(); dataGridView1.Rows[n].Cells[0].Value = item[0].ToString(); dataGridView1.Rows[n].Cells[1].Value = item[1].ToString(); dataGridView1.Rows[n].Cells[2].Value = item[2].ToString(); dataGridView1.Rows[n].Cells[3].Value = item[3].ToString(); dataGridView1.Rows[n].Cells[4].Value = item[4].ToString(); } } private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); textBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); textBox3.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString(); textBox4.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString(); comboBox1.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString(); } private void button1_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand(@"DELETE FROM PhoneBook WHERE (Mobile = '"+textBox3.Text+"')", con); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Successfully Deleted!"); Display(); } private void button2_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand(@"UPDATE Phonebook SET FirstName='"+textBox1.Text+"',LastName='"+textBox2.Text+"',Mobile='"+textBox3.Text+"',Email='"+textBox4.Text+"',Catagory='"+comboBox1.Text+"'WHERE (Mobile='"+textBox3.Text+"')", con); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Successfully Updated!"); Display(); } private void textBox5_TextChanged(object sender, EventArgs e) { SqlDataAdapter sda = new SqlDataAdapter("Select * from PhoneBook Where (Mobile like '%" + textBox5.Text + "%') or (FirstName like '%" + textBox5.Text + "%') or (LastName like '%" + textBox5.Text + "%') or (Email like '%" + textBox5.Text + "%') or (Catagory like '%" + textBox5.Text + "%')", con); DataTable dt = new DataTable(); sda.Fill(dt); dataGridView1.Rows.Clear(); foreach (DataRow item in dt.Rows) { int n = dataGridView1.Rows.Add(); dataGridView1.Rows[n].Cells[0].Value = item[0].ToString(); dataGridView1.Rows[n].Cells[1].Value = item[1].ToString(); dataGridView1.Rows[n].Cells[2].Value = item[2].ToString(); dataGridView1.Rows[n].Cells[3].Value = item[3].ToString(); dataGridView1.Rows[n].Cells[4].Value = item[4].ToString(); } } } } |
Here is the SQL code
1 2 3 4 5 6 7 8 | CREATE TABLE [dbo].[PhoneBook] ( [FirstName] NVARCHAR (50) NULL, [LastName] NVARCHAR (50) NULL, [Mobile] NVARCHAR (50) NOT NULL, [Email] NVARCHAR (50) NULL, [Catagory] NVARCHAR (50) NULL, PRIMARY KEY CLUSTERED ([Mobile] ASC) ); |
0 Comments