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

Editing a Contact

When the User clicks on Edit LinkLabel Control in the WelcomeForm or AdvanceSearch Form, we open another Form called EditContact.cs. This Form gets populated with the data of an existing selected contact. The user can edit it from there.

Snapshot of Edit Contact Form EditContact.cs.


The code for EditContact.cs is below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CIMS.BO;
using CIMS.BL;

namespace CIMS.UI
    public partial class EditContact : Form
        public EditContact()

        //Fetching an Existing Contact Details in Text Boxes
        private void EditContact_Load(object sender, EventArgs e)
            Int32 iContactId = Convert.ToInt32(textBoxContactId.Text);
            ContactBO objContactBO = new ContactBO();

            objContactBO = ContactBL.GetContactDetails(iContactId);

            textBoxFName.Text = objContactBO.FirstName;
            textBoxLName.Text = objContactBO.LastName;
            textBoxMobile.Text = objContactBO.MobileNumber;
            textBoxEmail.Text = objContactBO.EmailId;
            dateTimePickerDOB.Value = objContactBO.DOB;

        //Updating the Contact in DB
        private void buttonUpdate_Click(object sender, EventArgs e)
            ContactBO objContactBO = new ContactBO();

            objContactBO.ContactId = Convert.ToInt32(textBoxContactId.Text);
            objContactBO.FirstName = textBoxFName.Text;
            objContactBO.LastName = textBoxLName.Text;
            objContactBO.MobileNumber = textBoxMobile.Text;
            objContactBO.EmailId = textBoxEmail.Text;
            objContactBO.DOB = dateTimePickerDOB.Value;

            int result= ContactBL.UpdateContactDetails(objContactBO);

            if (result > 0)
                MessageBox.Show("Contact Updated Successfully");

                MessageBox.Show("Failed to update Contact");


Here on the Update Button Click Event, we are calling a method in ContactBL that takes a ContactBO Business Object.

 public static int UpdateContactDetails(ContactBO objContactBO)
            int result = ContactDL.UpdateContactDetails(objContactBO);
            return result;

This method calls another method in ContactDL called UpdateContactDetails
This method takes a ContactBO Business Object, fetch all data from it and update it in the Database.

 public static int UpdateContactDetails(ContactBO objContactBO)

                Int32 iContactId = objContactBO.ContactId;
                string sFName = objContactBO.FirstName;
                string sLName = objContactBO.LastName;
                string sMobileNumber = objContactBO.MobileNumber;
                string sEmailId = objContactBO.EmailId;
                DateTime dtDOB = objContactBO.DOB;

                SqlCommand cmd = new SqlCommand("UPDATE contact_details SET FirstName = @FName, LastName = @LName,"+
                    "MobileNumber = @MobileNumber , EmailId = @EmailId, DOB = @DOB WHERE ContactId = @Id", conn);

                cmd.Parameters.Add(new SqlParameter("@Id", iContactId));
                cmd.Parameters.Add(new SqlParameter("@FName", sFName));
                cmd.Parameters.Add(new SqlParameter("@LName", sLName));
                cmd.Parameters.Add(new SqlParameter("@MobileNumber", sMobileNumber));
                cmd.Parameters.Add(new SqlParameter("@EmailId", sEmailId));
                cmd.Parameters.Add(new SqlParameter("@DOB", dtDOB));

                int result = cmd.ExecuteNonQuery();


                return result;

            catch (Exception e)
                throw e;


Snapshots of Editing a Contact

Select a Contact for Editing by clicking on the Edit Link below a Contact

Another Window will open with text boxes to edit that contact


Update the Contact Details

When the user clicks on the Update button the Contact gets updated.



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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s