/// <summary>Gets the state of the contacts in given.</summary>
 /// <param name="state">The state.</param>
 /// <returns>
 ///   <br />
 /// </returns>
 /// <exception cref="AddressBookException">No Contacts in Given State</exception>
 public static bool GetContactsInGivenState(string state)
 {
     try
     {
         AddressBookModel addressBookObj = new AddressBookModel();
         using (SqlConnection connection = new SqlConnection(connetionString))
         {
             SqlCommand command = new SqlCommand($"select AddressBookName, pc.FirstName,pc.LastName,Address,City,State,Zipcode,PhoneNumber,Email,date_added " +
                                                 $"from address_book_person_name adp inner join people_contact pc " +
                                                 $"on adp.FirstName = pc.FirstName and adp.LastName = pc.LastName " +
                                                 $"where State = '{state}'", connection);
             connection.Open();
             SqlDataReader dr = command.ExecuteReader();
             if (dr.HasRows)
             {
                 CustomPrint.PrintInRed($"All Contacts from DB in state {state}");
                 CustomPrint.PrintDashLine();
                 Console.WriteLine(CustomPrint.PrintRow("AddressBookName", "Name", "Address", "City", "State", "Zip", "PhoneNo", "Email", "Date Added"));
                 CustomPrint.PrintDashLine();
                 while (dr.Read())
                 {
                     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);
                     Console.WriteLine(addressBookObj);
                 }
                 CustomPrint.PrintDashLine();
                 connection.Close();
                 return(true);
             }
             throw new AddressBookException(AddressBookException.ExceptionType.No_DATA, "No Contacts in Given State");
         }
     }
     catch (AddressBookException e)
     {
         CustomPrint.PrintInMagenta(e.Message);
         return(false);
     }
     catch (Exception e)
     {
         CustomPrint.PrintInMagenta(e.Message);
         return(false);
     }
 }
Esempio n. 2
0
 /// <summary>View all contacts in DB.</summary>
 public static void AllContacts()
 {
     //For Sorting Accoring to sorting type choosed
     SortContacts.SortOnConditionChooses(Contacts.listContacts);
     CustomPrint.PrintInRed($"All Contacts in every address book");
     CustomPrint.PrintDashLine();
     Console.WriteLine(CustomPrint.PrintRow("AddressBookName", "Name", "Address", "City", "State", "Zip", "PhoneNo", "Email", "Date Added"));
     CustomPrint.PrintDashLine();
     foreach (AddressBookModel item in listContacts)
     {
         Console.WriteLine(item);
     }
     CustomPrint.PrintDashLine();
 }
        //Read from JSON File
        public static void ReadAddressBookJSON()
        {
            string path = @"F:\MyPrograms\Assignments\A4-AddressBook\AddressBookSystem\AddressBookSystem\Utility\AddressBook.json";

            IList <AddressBookModel> addressDatas = JsonConvert.DeserializeObject <IList <AddressBookModel> >(File.ReadAllText(path));

            CustomPrint.PrintInRed("Read Data Successfully");
            CustomPrint.PrintDashLine();
            Console.WriteLine(CustomPrint.PrintRow("AddressBookName", "Name", "Address", "City", "State", "Zip", "PhoneNo", "Email"));
            CustomPrint.PrintDashLine();
            foreach (AddressBookModel personDetail in addressDatas)
            {
                Console.WriteLine(personDetail);
            }
            CustomPrint.PrintDashLine();
            WriteAddressBookCSV();
        }
 /// <summary>Retrives all contacts from database.</summary>
 /// <returns></returns>
 public static bool RetriveAllContactsFromDB()
 {
     try
     {
         AddressBookModel addressBookObj = new AddressBookModel();
         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)
             {
                 CustomPrint.PrintInRed("All Contacts from DB");
                 CustomPrint.PrintDashLine();
                 Console.WriteLine(CustomPrint.PrintRow("AddressBookName", "Name", "Address", "City", "State", "Zip", "PhoneNo", "Email", "Date Added"));
                 CustomPrint.PrintDashLine();
                 while (dr.Read())
                 {
                     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);
                     Console.WriteLine(addressBookObj);
                 }
                 CustomPrint.PrintDashLine();
             }
             connection.Close();
             return(true);
         }
     }
     catch (Exception e)
     {
         CustomPrint.PrintInMagenta(e.Message);
         return(false);
     }
 }
Esempio n. 5
0
        /// <summary>Views All contacts in same state or city for all address book.</summary>
        public static void SearchPersonByCityOrState()
        {
            //For Sorting Accoring to sorting type choosed
            SortContacts.SortOnConditionChooses(Contacts.listContacts);
            //For counting no of people in same city/state
            int slNo = 0;

            Console.Write("Enter City : ");
            string city = Console.ReadLine();

            Console.Write("Enter State : ");
            string state = Console.ReadLine();

            CustomPrint.PrintInRed("Search by City " + city + " are :\n");
            CustomPrint.PrintDashLine();
            Console.WriteLine(CustomPrint.PrintRow("AddressBookName", "Name", "Address", "City", "State", "Zip", "PhoneNo", "Email", "Date Added"));
            CustomPrint.PrintDashLine();
            foreach (AddressBookModel personDetails in listContacts.Where(x => (x.City.ToLower().Equals(city.ToLower()) && x.State.ToLower().Equals(state.ToLower()))))
            {
                Console.WriteLine(personDetails);
                slNo++;
            }
            CustomPrint.PrintDashLine();
            Console.WriteLine("\nCount by City is : " + slNo);
            CustomPrint.PrintInRed("Search by State " + state + " are :\n");
            CustomPrint.PrintDashLine();
            Console.WriteLine(CustomPrint.PrintRow("AddressBookName", "Name", "Address", "City", "State", "Zip", "PhoneNo", "Email", "Date Added"));
            CustomPrint.PrintDashLine();
            slNo = 0;
            foreach (AddressBookModel personDetails in listContacts)
            {
                if (personDetails.State.Equals(state))
                {
                    Console.WriteLine(personDetails);
                    slNo++;
                }
            }
            CustomPrint.PrintDashLine();
            Console.WriteLine("\nCount by State is : " + slNo);
        }