Connecting Multiple Databases in .NET using C# – Part 1 Introduction

We have shifted..

https://programmingpalace.wordpress.com/ is now http://thetechstory.com/

Find this Tutorial in the given link.

http://thetechstory.com/2012/09/29/connecting-multiple-databases-in-net-using-c-part-1-introduction/

.NET Project Demonstrating MVC Pattern using C# – Contact Information Management System(CIMS)-Part 7 Viewing All Contacts

We have shifted..

https://programmingpalace.wordpress.com/ is now http://thetechstory.com/

Find this Tutorial in the given link.

http://thetechstory.com/2012/09/27/net-project-demonstrating-mvc-pattern-using-c-contact-information-management-systemcims-part-7-viewing-all-contacts/

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

We have shifted..

https://programmingpalace.wordpress.com/ is now http://thetechstory.com/

Find this Tutorial in the given link.

http://thetechstory.com/2012/09/27/net-project-demonstrating-mvc-pattern-using-c-contact-information-management-systemcims-part-6-adding-a-new-contact/

.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

.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.

EditContactForm

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()
        {
            InitializeComponent();
        }

        //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");
                this.Hide();

            }
            else
                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)
        {

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

                conn.Open();
                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();

                conn.Close();

                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
SelectContactForEditing

Another Window will open with text boxes to edit that contact

EditFormOpens

Update the Contact Details

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

ContactGetUpdated