/// <summary>Stores all contacts to list.</summary> public static void StoreAllContactsToList() { try { using (SqlConnection connection = new SqlConnection(connetionString)) { SqlCommand command = new SqlCommand("RetriveContacts", connection); command.CommandType = System.Data.CommandType.StoredProcedure; connection.Open(); SqlDataReader dr = command.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { AddressBookModel addressBookObj = new AddressBookModel(); addressBookObj.AddressBookName = dr.GetString(0); addressBookObj.FirstName = dr.GetString(1); addressBookObj.LastName = dr.GetString(2); addressBookObj.Address = dr.GetString(3); addressBookObj.City = dr.GetString(4); addressBookObj.State = dr.GetString(5); addressBookObj.Zip = dr.GetString(6); addressBookObj.PhoneNo = dr.GetString(7); addressBookObj.Email = dr.GetString(8); addressBookObj.DateAdded = dr.GetDateTime(9); Contacts.listContacts.Add(addressBookObj); } } connection.Close(); } } catch (Exception e) { CustomPrint.PrintInMagenta(e.Message); } }
/// <summary>Deletes the contact from database.</summary> /// <param name="addressBookObj">The address book object.</param> /// <returns> /// <br /> /// </returns> public static bool DeleteContactFromDB(AddressBookModel addressBookObj) { bool result; try { using (SqlConnection connection = new SqlConnection(connetionString)) { connection.Open(); SqlCommand command = new SqlCommand($"delete from people_contact " + $"where FirstName = '{addressBookObj.FirstName}' and LastName = '{addressBookObj.LastName}'", connection); int noOfRow = command.ExecuteNonQuery(); CustomPrint.PrintInRed($"{noOfRow} rows affected"); result = true; connection.Close(); } } catch (Exception e) { CustomPrint.PrintInMagenta(e.Message); result = false; } return(result); }