Example #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int entryid = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString());

                using (ContactContext db = new ContactContext())
                {
                    co1 = db.Contacts.Find(entryid);

                    db.Contacts.Remove(co1);

                    db.SaveChanges();

                    MessageBox.Show("Deleted Successfully");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
Example #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //ID
                if (textBox3.Text != "")
                {
                    co.contactID = Convert.ToInt32(textBox3.Text);
                }

                //First Name
                if (textBox8.Text != "")
                {
                    co.fname = textBox8.Text;
                }
                else
                {
                    MessageBox.Show("First Name cannot be left empty");
                    return;
                }

                //Last Name
                if (textBox1.Text != "")
                {
                    co.lname = textBox1.Text;
                }
                else
                {
                    MessageBox.Show("Last Name cannot be left empty");
                    return;
                }


                Regex reg = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
                Match match;

                //Email
                if (textBox2.Text != "")
                {
                    match = reg.Match(textBox2.Text);

                    if (match.Success)
                    {
                        co.email = textBox2.Text;
                    }
                    else
                    {
                        MessageBox.Show("Email not valid");
                        return;
                    }
                }
                else
                {
                    co.email = textBox2.Text;
                }

                //Phone Number
                co.mobilephone = maskedTextBox2.Text;

                //Birth Date
                if (dateTimePicker1.CustomFormat == " ")
                {
                    co.birthdate = "";
                }
                else if (dateTimePicker1.CustomFormat == "dd/MM/yyyy")
                {
                    co.birthdate = dateTimePicker1.Text;
                }

                //Address
                co.address = textBox6.Text;

                //Description
                co.description = textBox7.Text;

                //create ContactContext object
                using (ContactContext db = new ContactContext())
                {
                    if (textBox3.Text == "") //Create a new database entry
                    {
                        db.Contacts.Add(co);
                    }
                    else //Update an existing database entry
                    {
                        db.Entry(co).State = EntityState.Modified;
                    }

                    db.SaveChanges();

                    MessageBox.Show("Database Updated");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            Close();
        }
Example #3
0
 private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
 {
     using (ContactContext db = new ContactContext())
     {
         if (fla == false)
         {
             if (dataGridView1.CurrentCell.ColumnIndex == 1)
             {
                 dataGridView1.DataSource = db.Contacts.OrderByDescending(s => s.fname).ToList();
                 fla = true;
             }
             else if (dataGridView1.CurrentCell.ColumnIndex == 2)
             {
                 dataGridView1.DataSource = db.Contacts.OrderByDescending(s => s.lname).ToList();
                 fla = true;
             }
             else if (dataGridView1.CurrentCell.ColumnIndex == 3)
             {
                 dataGridView1.DataSource = db.Contacts.OrderByDescending(s => s.email).ToList();
                 fla = true;
             }
             else if (dataGridView1.CurrentCell.ColumnIndex == 4)
             {
                 dataGridView1.DataSource = db.Contacts.OrderByDescending(s => s.mobilephone).ToList();
                 fla = true;
             }
             else if (dataGridView1.CurrentCell.ColumnIndex == 5)
             {
                 dataGridView1.DataSource = db.Contacts.OrderByDescending(s => s.birthdate).ToList();
                 fla = true;
             }
             else if (dataGridView1.CurrentCell.ColumnIndex == 6)
             {
                 dataGridView1.DataSource = db.Contacts.OrderByDescending(s => s.address).ToList();
                 fla = true;
             }
             else if (dataGridView1.CurrentCell.ColumnIndex == 7)
             {
                 dataGridView1.DataSource = db.Contacts.OrderByDescending(s => s.description).ToList();
                 fla = true;
             }
         }
         else
         {
             if (dataGridView1.CurrentCell.ColumnIndex == 1)
             {
                 dataGridView1.DataSource = db.Contacts.OrderBy(s => s.fname).ToList();
                 fla = false;
             }
             else if (dataGridView1.CurrentCell.ColumnIndex == 2)
             {
                 dataGridView1.DataSource = db.Contacts.OrderBy(s => s.lname).ToList();
                 fla = false;
             }
             else if (dataGridView1.CurrentCell.ColumnIndex == 3)
             {
                 dataGridView1.DataSource = db.Contacts.OrderBy(s => s.email).ToList();
                 fla = false;
             }
             else if (dataGridView1.CurrentCell.ColumnIndex == 4)
             {
                 dataGridView1.DataSource = db.Contacts.OrderBy(s => s.mobilephone).ToList();
                 fla = false;
             }
             else if (dataGridView1.CurrentCell.ColumnIndex == 5)
             {
                 dataGridView1.DataSource = db.Contacts.OrderBy(s => s.birthdate).ToList();
                 fla = false;
             }
             else if (dataGridView1.CurrentCell.ColumnIndex == 6)
             {
                 dataGridView1.DataSource = db.Contacts.OrderBy(s => s.address).ToList();
                 fla = false;
             }
             else if (dataGridView1.CurrentCell.ColumnIndex == 7)
             {
                 dataGridView1.DataSource = db.Contacts.OrderBy(s => s.description).ToList();
                 fla = false;
             }
         }
     }
 }