.NET Project Demonstrating MVC Pattern using C# – Contact Information Management System(CIMS)-Part 5-Deleting a Contact

Deleting a Contact

When the User clicks on Delete LinkLabel Control in the WelcomeForm or AdvanceSearch Form, a Message Box pops us asking the user whether he really wants to delete the contact. Based on the user selection the contact gets removed.

The Code in WelcomeForm.cs for Delete Link Label Control click event is below


   //Code for Delete Link Control
        private void linkLabelDelete_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Int32 iContactId = Convert.ToInt32(labelContactIdResult.Text);

             DialogResult result1= MessageBox.Show("Are you sure you want to delete this Contact?",
                                   "Delete the Contact",
                                    MessageBoxButtons.YesNo);

             if (result1 == DialogResult.Yes)
             {
                 int result = ContactBL.RemoveContact(iContactId);

                 if (result > 0)
                 {
                     MessageBox.Show("Contact Removed Successfully");
                 }
             }

        }

In the WelcomeForm.cs, in the Delete Link click event, we are opening a Message Box with Yes, No Buttons. If the user select Yes then we are calling a Method called RemoveContact  in ContactBL taking the Contact Id of the User as an Input Parameter.


  public static int RemoveContact(Int32 iContactId)
        {
            int result = ContactDL.DeleteContactDetails(iContactId);
            return result;
        }

This method calls a method in ContactDL called  DeleteContactDetails

 public static int DeleteContactDetails(Int32 iContactId)
        {
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("DELETE FROM contact_details WHERE ContactId = @Id", conn);

                cmd.Parameters.Add(new SqlParameter("@Id", iContactId));

                int result = cmd.ExecuteNonQuery();

                conn.Close();
                return result;

            }
            catch (Exception e)
            {
                throw e;
            }

        }

This method deletes the Contact from the Database table.

Snapshots of Removing a Contact.

Select a contact for Deleting and click on the Delete link. 

SelectContactForDeleting

A message box appears asking whether you want to delete or not.

UserCanSelectWhetherToDeleteOrNot

When the user select Yes the Contact get removed and a message box pops us giving a Confirmation Message.

IfUserSelectsYesThenContactGetParmanentlyRemoved

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s