Пример #1
0
        public ActionResult UpdateSupplier(int supplierId)
        {
            ActionResult response;

            if (ModelState.IsValid)
            {
                try
                {
                    //Obtain supplier info from SQL using given ID.
                    SupplierDO supplierDO = _SupplierDAO.ObtainSupplierById(supplierId);

                    //Map DO to PO.
                    SupplierPO supplier = SupplierMapper.DoToPo(supplierDO);

                    //Give view page with obtained info.
                    response = View(supplier);
                }
                catch (SqlException sqlex)
                {
                    response = RedirectToAction("Error", "Shared");
                }
                catch (Exception ex)
                {
                    response = RedirectToAction("Error", "Shared");

                    Logger.ErrorLogPath = _LogPath;
                    Logger.ExceptionLog(ex);
                }
            }
            else
            {
                response = View();
            }
            return(response);
        }
Пример #2
0
        public ActionResult UpdateSupplier(SupplierPO form)
        {
            ActionResult response;

            if (ModelState.IsValid)
            {
                try
                {
                    //Map given info to DO.
                    SupplierDO supplierDO = SupplierMapper.PoToDo(form);

                    //Update supplier in SQL giving DO info.
                    _SupplierDAO.UpdateSupplier(supplierDO);

                    //Return to view all suppliers page.
                    response = RedirectToAction("Index", "Supplier");
                }
                catch (SqlException sqlex)
                {
                    response = RedirectToAction("Error", "Shared");
                }
                catch (Exception ex)
                {
                    response = RedirectToAction("Error", "Shared");

                    Logger.ErrorLogPath = _LogPath;
                    Logger.ExceptionLog(ex);
                }
            }
            else
            {
                response = View();
            }
            return(response);
        }
Пример #3
0
        public void CreateSupplier(SupplierDO supplier)
        {
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
                    using (SqlCommand newSupplier = new SqlCommand("CREATE_NEW_SUPPLIER_ENTRY", sqlConnection))
                    {
                        newSupplier.CommandType    = CommandType.StoredProcedure;
                        newSupplier.CommandTimeout = 60;
                        sqlConnection.Open();

                        newSupplier.Parameters.AddWithValue("CompanyName", supplier.CompanyName);
                        newSupplier.Parameters.AddWithValue("ContactName", supplier.ContactName);
                        newSupplier.Parameters.AddWithValue("ContactTitle", supplier.ContactTitle);
                        newSupplier.Parameters.AddWithValue("Address", supplier.Address);
                        newSupplier.Parameters.AddWithValue("City", supplier.City);
                        newSupplier.Parameters.AddWithValue("Region", supplier.Region);
                        newSupplier.Parameters.AddWithValue("PostalCode", supplier.PostalCode);
                        newSupplier.Parameters.AddWithValue("Country", supplier.Country);
                        newSupplier.Parameters.AddWithValue("Phone", supplier.Phone);
                        newSupplier.Parameters.AddWithValue("Fax", supplier.Fax);
                        newSupplier.Parameters.AddWithValue("HomePage", supplier.HomePage);

                        newSupplier.ExecuteNonQuery();
                    }
            }
            catch (SqlException sqlEx)
            {
                Logger.ErrorLogPath = _LogPath;
                Logger.SqlExceptionLog(sqlEx);

                throw sqlEx;
            }
        }
Пример #4
0
        /// <summary>
        /// Maps all suppliers from the datarow and returns the SupplierDO object.
        /// </summary>
        /// <param name="dataRow"></param>
        /// <returns></returns>
        public SupplierDO MapAllSuppliers(DataRow dataRow)
        {
            string currentMethod = "MapAllSuppliers";

            try
            {
                SupplierDO supplier = new SupplierDO();

                //If the supplier Id is not null then add values to the supplier object from the database.
                if (dataRow["SupplierID"] != DBNull.Value)
                {
                    supplier.SupplierId = (int)dataRow["SupplierID"];
                }
                supplier.ContactName  = dataRow["ContactName"].ToString();
                supplier.ContactTitle = dataRow["ContactTitle"].ToString();
                supplier.PostalCode   = dataRow["PostalCode"].ToString();
                supplier.Country      = dataRow["Country"].ToString();
                supplier.PhoneNumber  = dataRow["Phone"].ToString();

                //Returning the object with a row updated from SQL.
                return(supplier);
            }
            catch (Exception ex)
            {
                //Prints error to console and logs.

                SupplierErrorHandler(ex, currentClass, currentMethod, ex.StackTrace);
                throw ex;
            }
        }
Пример #5
0
        /// <summary>
        /// This will CREATE a new supplier.
        /// </summary>
        /// <param name="contactName">The name of the person that owns the company.</param>
        /// <param name="contactTitle"></param>
        /// <param name="postalCode"></param>
        /// <param name="country"></param>
        /// <param name="phoneNumber"></param>
        public void CreateNewSuppliers(SupplierDO supplier)
        {
            string currentMethod = "CreateNewSuppliers";

            try
            {
                //Creates a new connections.
                using (SqlConnection northWndConn = new SqlConnection(connectionString))
                {
                    //Creating a new SqlCommand to use a stored procedure.
                    SqlCommand enterCommand = new SqlCommand("CREATE_SUPPLIER", northWndConn);
                    enterCommand.CommandType = CommandType.StoredProcedure;

                    //Parameters that are being passed to the stored procedures.
                    enterCommand.Parameters.AddWithValue("@ContactName", supplier.ContactName);
                    enterCommand.Parameters.AddWithValue("@ContactTitle", supplier.ContactTitle);
                    enterCommand.Parameters.AddWithValue("@PostalCode", supplier.PostalCode);
                    enterCommand.Parameters.AddWithValue("@Country", supplier.Country);
                    enterCommand.Parameters.AddWithValue("@PhoneNumber", supplier.PhoneNumber);

                    //Opening connection.
                    northWndConn.Open();
                    //Execute Non Query command.
                    enterCommand.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                //Prints error to console.
                SupplierErrorHandler(ex, currentClass, currentMethod, ex.StackTrace);
                throw ex;
            }
        }
Пример #6
0
        public SupplierDO ObtainSupplierById(int supplierId)
        {
            SupplierDO supplier = new SupplierDO();

            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
                    using (SqlCommand getIdInfo = new SqlCommand("SELECT_BY_ID", sqlConnection))
                    {
                        sqlConnection.Open();
                        getIdInfo.Parameters.AddWithValue("SupplierID", supplierId);
                        getIdInfo.CommandType    = CommandType.StoredProcedure;
                        getIdInfo.CommandTimeout = 60;
                        SqlDataReader obtainOne = getIdInfo.ExecuteReader();

                        if (obtainOne.Read())
                        {
                            supplier = Mapper.ReaderToSupplier(obtainOne);
                        }
                    }
                return(supplier);
            }
            catch (SqlException sqlEx)
            {
                Logger.ErrorLogPath = _LogPath;
                Logger.SqlExceptionLog(sqlEx);

                throw sqlEx;
            }
        }
Пример #7
0
        public List <SupplierDO> ObtainAllSuppliers()
        {
            List <SupplierDO> Suppliers = new List <SupplierDO>();

            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
                    using (SqlCommand storedcommand = new SqlCommand("VIEW_ALL_SUPPLIERS", sqlConnection))
                    {
                        storedcommand.CommandType = CommandType.StoredProcedure;
                        sqlConnection.Open();
                        SqlDataReader obtainAll = storedcommand.ExecuteReader();
                        while (obtainAll.Read())
                        {
                            SupplierDO supplier = Mapper.ReaderToSupplier(obtainAll);

                            Suppliers.Add(supplier);
                        }
                    }
                return(Suppliers);
            }
            catch (SqlException sqlEx)
            {
                Logger.ErrorLogPath = _LogPath;
                Logger.SqlExceptionLog(sqlEx);

                throw sqlEx;
            }
        }
Пример #8
0
        public ActionResult UpdateSupplier(int id)
        {
            ActionResult response;

            try
            {
                SupplierDO  supDO    = dao.ObtainSupplierSingle(id);
                SuppliersPO supplier = Mapper.SupplierDOtoSupplierPO(supDO);
                response = View(supplier);
            }
            catch (SqlException sqlEx)
            {
                response = RedirectToAction("Index", "Suppliers");
            }
            return(response);
        }
Пример #9
0
        public ActionResult UpdateSupplier(SuppliersPO form)
        {
            ActionResult response;

            try
            {
                SupplierDO sup = Mapper.SupplierPOtoSupplierDO(form);
                dao.UpdateInformation(sup);
                response = RedirectToAction("Index", "Suppliers");
            }
            catch (SqlException sqlEx)
            {
                response = View(form);
            }

            return(response);
        }
Пример #10
0
        public void UpdateSupplier(SupplierDO supplier)
        {
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
                    using (SqlCommand update = new SqlCommand("UPDATE_SUPPLIER_INFORMATION", sqlConnection))
                    {
                        update.Parameters.AddWithValue("SupplierID", supplier.SupplierID);
                        update.CommandType = CommandType.StoredProcedure;
                        sqlConnection.Open();
                        update.CommandTimeout = 60;

                        update.Parameters.AddWithValue("CompanyName", supplier.CompanyName);
                        update.Parameters.AddWithValue("ContactName", supplier.ContactName);
                        update.Parameters.AddWithValue("ContactTitle", supplier.ContactTitle);
                        update.Parameters.AddWithValue("Address", supplier.Address);
                        update.Parameters.AddWithValue("City", supplier.City);
                        update.Parameters.AddWithValue("Region", supplier.Region);
                        update.Parameters.AddWithValue("PostalCode", supplier.PostalCode);
                        update.Parameters.AddWithValue("Country", supplier.Country);
                        update.Parameters.AddWithValue("Phone", supplier.Phone);
                        update.Parameters.AddWithValue("Fax", supplier.Fax);
                        update.Parameters.AddWithValue("HomePage", supplier.HomePage);

                        update.ExecuteNonQuery();
                    }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("exception");
                string filePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\NorthWIND_Supplier_Log.txt";

                using (StreamWriter updateLog = new StreamWriter(filePath, true))
                {
                    string methodName = "UpdateSupplier";
                    updateLog.WriteLine(new String('-', 5));

                    updateLog.WriteLine("Time: {0}\r\nMethod:{1}\r\n{2}", DateTime.Now.ToString(), methodName, ex);
                }
                Console.ReadKey();

                throw ex;
            }
        }
Пример #11
0
        public static SupplierDO ReaderToSupplier(SqlDataReader from)
        {
            SupplierDO to = new SupplierDO();

            to.SupplierID   = from.GetInt32(0);
            to.CompanyName  = from.GetValue(1) as string;
            to.ContactName  = from.GetValue(2) as string;
            to.ContactTitle = from.GetValue(3) as string;
            to.Address      = from.GetValue(4) as string;
            to.City         = from.GetValue(5) as string;
            to.Region       = from.GetValue(6) as string;
            to.PostalCode   = from.GetValue(7) as string;
            to.Country      = from.GetValue(8) as string;
            to.Phone        = from.GetValue(9) as string;
            to.Fax          = from.GetValue(10) as string;
            to.HomePage     = from.GetValue(11) as string;

            return(to);
        }
Пример #12
0
        public static SupplierPO DoToPo(SupplierDO from)
        {
            SupplierPO suppInfo = new SupplierPO();

            suppInfo.SupplierID   = from.SupplierID;
            suppInfo.CompanyName  = from.CompanyName;
            suppInfo.ContactName  = from.ContactName;
            suppInfo.ContactTitle = from.ContactTitle;
            suppInfo.Address      = from.Address;
            suppInfo.City         = from.City;
            suppInfo.Region       = from.Region;
            suppInfo.PostalCode   = from.PostalCode;
            suppInfo.Country      = from.Country;
            suppInfo.Phone        = from.Phone;
            suppInfo.Fax          = from.Fax;
            suppInfo.HomePage     = from.HomePage;

            return(suppInfo);
        }
Пример #13
0
        public static SuppliersPO SupplierDOtoSupplierPO(SupplierDO from)
        {
            SuppliersPO to = new SuppliersPO
            {
                SupplierID   = from.SupplierID,
                CompanyName  = from.CompanyName,
                ContactName  = from.ContactName,
                ContactTitle = from.ContactTitle,
                Address      = from.Address,
                City         = from.City,
                Region       = from.Region,
                PostalCode   = from.PostalCode,
                Country      = from.Country,
                Phone        = from.Phone,
                Fax          = from.Fax,
                HomePage     = from.HomePage
            };

            return(to);
        }
Пример #14
0
        /// <summary>
        /// This will DISPLAY a new supplier.
        /// </summary>
        /// <returns></returns>
        public List <SupplierDO> ViewAllSuppliers()
        {
            string currentMethod = "ViewAllSuppliers";

            try
            {
                List <SupplierDO> allSuppliers = new List <SupplierDO>();

                //Opening SQL connection.
                using (SqlConnection northWndConn = new SqlConnection(connectionString))
                {
                    //Creating a new SqlCommand to use a stored procedure.
                    SqlCommand enterCommand = new SqlCommand("DISPLAY_SUPPLIER", northWndConn);
                    enterCommand.CommandType = CommandType.StoredProcedure;
                    northWndConn.Open();

                    //Using SqlDataAdapter to get SQL table.
                    DataTable supplyInfo = new DataTable();
                    using (SqlDataAdapter supplierAdapter = new SqlDataAdapter(enterCommand))
                    {
                        supplierAdapter.Fill(supplyInfo);
                        supplierAdapter.Dispose();
                    }

                    //Putting datarow into a List of the supplier object.
                    foreach (DataRow row in supplyInfo.Rows)
                    {
                        SupplierDO mappedRow = MapAllSuppliers(row);
                        allSuppliers.Add(mappedRow);
                    }
                }
                //Returning an updated list of the supplier object.
                return(allSuppliers);
            }
            catch (Exception ex)
            {
                //Prints error to console and logs.
                SupplierErrorHandler(ex, currentClass, currentMethod, ex.StackTrace);
                throw ex;
            }
        }
Пример #15
0
        public SupplierDO ObtainSupplierById(int supplierId)
        {
            SupplierDO supplier = new SupplierDO();

            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
                    using (SqlCommand getIdInfo = new SqlCommand("SELECT_BY_ID", sqlConnection))
                    {
                        sqlConnection.Open();
                        getIdInfo.Parameters.AddWithValue("SupplierID", supplierId);
                        getIdInfo.CommandType    = CommandType.StoredProcedure;
                        getIdInfo.CommandTimeout = 60;
                        SqlDataReader obtainOne = getIdInfo.ExecuteReader();

                        if (obtainOne.Read())
                        {
                            supplier = Mapper.ReaderToSupplier(obtainOne);
                        }
                    }
                return(supplier);
            }
            catch (SqlException ex)
            {
                Console.WriteLine("exception");
                string filePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\NorthWIND_Supplier_Log.txt";

                using (StreamWriter updateLog = new StreamWriter(filePath, true))
                {
                    string methodName = "ObtainSupplierById";
                    updateLog.WriteLine(new String('-', 5));

                    updateLog.WriteLine("Time: {0}\r\nMethod:{1}\r\n{2}", DateTime.Now.ToString(), methodName, ex);
                }
                Console.ReadKey();

                throw ex;
            }
        }
Пример #16
0
        public int CreateNewSupplier(SupplierDO suppInfo)
        {
            int rowsAffected = 0;

            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(connectionString))
                    using (SqlCommand createSupplier = new SqlCommand("CREATE_SUPPLIER", sqlConnection))
                    {
                        createSupplier.CommandType    = System.Data.CommandType.StoredProcedure;
                        createSupplier.CommandTimeout = 90;

                        createSupplier.Parameters.AddWithValue("CompanyName", suppInfo.CompanyName);
                        createSupplier.Parameters.AddWithValue("ContactName", suppInfo.ContactName);
                        createSupplier.Parameters.AddWithValue("ContactTitle", suppInfo.ContactTitle);
                        createSupplier.Parameters.AddWithValue("Address", suppInfo.Address);
                        createSupplier.Parameters.AddWithValue("City", suppInfo.City);
                        createSupplier.Parameters.AddWithValue("Region", suppInfo.Region);
                        createSupplier.Parameters.AddWithValue("PostalCode", suppInfo.PostalCode);
                        createSupplier.Parameters.AddWithValue("Country", suppInfo.Country);
                        createSupplier.Parameters.AddWithValue("Phone", suppInfo.Phone);
                        createSupplier.Parameters.AddWithValue("Fax", suppInfo.Fax);
                        createSupplier.Parameters.AddWithValue("HomePage", suppInfo.HomePage);

                        sqlConnection.Open();
                        rowsAffected = createSupplier.ExecuteNonQuery();

                        sqlConnection.Close();
                    }
            }
            catch (SqlException sqlEx)
            {
                Logger.ErrorLogPath = errorLogPath;
                Logger.SqlExceptionLog(sqlEx);

                throw sqlEx;
            }
            return(rowsAffected);
        }
Пример #17
0
        public List <SupplierDO> ObtainAllSuppliers()
        {
            List <SupplierDO> Suppliers = new List <SupplierDO>();

            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
                    using (SqlCommand storedcommand = new SqlCommand("VIEW_ALL_SUPPLIERS", sqlConnection))
                    {
                        storedcommand.CommandType = CommandType.StoredProcedure;
                        sqlConnection.Open();
                        SqlDataReader obtainAll = storedcommand.ExecuteReader();
                        while (obtainAll.Read())
                        {
                            SupplierDO supplier = Mapper.ReaderToSupplier(obtainAll);

                            Suppliers.Add(supplier);
                        }
                    }
                return(Suppliers);
            }
            catch (SqlException ex)
            {
                Console.WriteLine("exception");
                string filePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\NorthWIND_Supplier_Log.txt";

                using (StreamWriter updateLog = new StreamWriter(filePath, true))
                {
                    string methodName = "ObtainAllSuppliers";
                    updateLog.WriteLine(new String('-', 5));

                    updateLog.WriteLine("Time: {0}\r\nMethod:{1}\r\n{2}", DateTime.Now.ToString(), methodName, ex);
                }
                Console.ReadKey();

                throw ex;
            }
        }
Пример #18
0
        public ActionResult CreateSupplier(Supplier form)
        {
            ActionResult response;

            if (ModelState.IsValid)
            {
                try
                {
                    SupplierDO newSupplier = MVCMapper.PoToDo(form);
                    Dao.CreateSupplier(newSupplier);
                    response = RedirectToAction("Index", "Supplier");
                }
                catch (SqlException sqlex)
                {
                    response = View(form);
                }
            }
            else
            {
                response = View();
            }
            return(response);
        }
Пример #19
0
        /// <summary>
        /// Gets required information to make changes from the user.
        /// </summary>
        /// <param name="contactTitle"></param>
        /// <param name="postalCode"></param>
        /// <param name="country"></param>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>
        private static SupplierDO GetUserInput()
        {
            string     currentMethod = "GetUserInput";
            SupplierDO supplier      = new SupplierDO();

            //Try catch to get any exeptions that may occur.
            do
            {
                try
                {
                    //Collect information from the user.
                    Console.WriteLine("Please enter contact name.");
                    supplier.ContactName = Console.ReadLine();
                    Console.WriteLine("Please enter contact title.");
                    supplier.ContactTitle = Console.ReadLine();
                    Console.WriteLine("Please enter Postal Code.");
                    int.TryParse(Console.ReadLine(), out int postalCode);
                    Console.WriteLine("Please enter Country.");
                    supplier.Country = Console.ReadLine();
                    Console.WriteLine("Please enter Phone number");
                    supplier.PhoneNumber = Console.ReadLine();
                    supplier.PostalCode  = postalCode.ToString();

                    //Returning object back to who called it.
                    return(supplier);
                }
                catch (Exception ex)
                {
                    string      currentClass = "Program";
                    SupplierDAO supplierDAO  = new SupplierDAO();

                    //Prints error to console, logs, and restarts the menu.
                    supplierDAO.SupplierErrorHandler(ex, currentClass, currentMethod, ex.StackTrace);
                    throw ex;
                }
            }while (true);
        }
Пример #20
0
        public void UpdateInformation(SupplierDO suppInfo)
        {
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(connectionString))
                    using (SqlCommand updateSupplier = new SqlCommand("UPDATE_SUPPLIER_INFO", sqlConnection))
                    {
                        updateSupplier.CommandType    = System.Data.CommandType.StoredProcedure;
                        updateSupplier.CommandTimeout = 90;

                        updateSupplier.Parameters.AddWithValue("supplierID", suppInfo.SupplierID);
                        updateSupplier.Parameters.AddWithValue("CompanyName", suppInfo.CompanyName);
                        updateSupplier.Parameters.AddWithValue("ContactName", suppInfo.ContactName);
                        updateSupplier.Parameters.AddWithValue("ContactTitle", suppInfo.ContactTitle);
                        updateSupplier.Parameters.AddWithValue("Address", suppInfo.Address);
                        updateSupplier.Parameters.AddWithValue("City", suppInfo.City);
                        updateSupplier.Parameters.AddWithValue("Region", suppInfo.Region);
                        updateSupplier.Parameters.AddWithValue("PostalCode", suppInfo.PostalCode);
                        updateSupplier.Parameters.AddWithValue("Country", suppInfo.Country);
                        updateSupplier.Parameters.AddWithValue("Phone", suppInfo.Phone);
                        updateSupplier.Parameters.AddWithValue("Fax", suppInfo.Fax);
                        updateSupplier.Parameters.AddWithValue("HomePage", suppInfo.HomePage);

                        sqlConnection.Open();
                        int i = updateSupplier.ExecuteNonQuery();

                        sqlConnection.Close();
                    }
            }
            catch (SqlException sqlEx)
            {
                Logger.ErrorLogPath = errorLogPath;
                Logger.SqlExceptionLog(sqlEx);

                throw sqlEx;
            }
        }
Пример #21
0
        public SupplierDO ObtainSupplierSingle(int ID)
        {
            SupplierDO response = new SupplierDO();

            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(connectionString))
                    using (SqlCommand viewSupplierByID = new SqlCommand("VIEW_ROW_BY_ID", sqlConnection))
                    {
                        viewSupplierByID.CommandType    = System.Data.CommandType.StoredProcedure;
                        viewSupplierByID.CommandTimeout = 90;

                        viewSupplierByID.Parameters.AddWithValue("SupplierID", ID);

                        sqlConnection.Open();

                        using (SqlDataReader reader = viewSupplierByID.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                response = Mapper.ReaderToSupplier(reader);
                            }
                        }

                        sqlConnection.Close();
                    }
            }
            catch (SqlException sqlEx)
            {
                Logger.ErrorLogPath = errorLogPath;
                Logger.SqlExceptionLog(sqlEx);

                throw sqlEx;
            }
            return(response);
        }
Пример #22
0
        public ActionResult CreateSupplier(SupplierPO form)
        {
            ActionResult response;

            //Check if valid info was given, Back to create supplier view page if not.
            if (ModelState.IsValid)
            {
                try
                {
                    //Map given info to DO.
                    SupplierDO newSupplier = SupplierMapper.PoToDo(form);

                    //Create supplier in SQL, giving DO info.
                    _SupplierDAO.CreateSupplier(newSupplier);

                    //Return to view all supplier page.
                    response = RedirectToAction("Index", "Supplier");
                }
                catch (SqlException sqlex)
                {
                    response = RedirectToAction("Error", "Shared");
                }
                catch (Exception ex)
                {
                    response = RedirectToAction("Error", "Shared");

                    Logger.ErrorLogPath = _LogPath;
                    Logger.ExceptionLog(ex);
                }
            }
            else
            {
                response = View();
            }
            return(response);
        }
Пример #23
0
        /// <summary>
        /// Menu to take the user through all actions.
        /// </summary>
        static void Menu()
        {
            string currentMethod = "Menu";
            //Instantiating objects.
            SupplierDAO supplierDAO = new SupplierDAO();
            SupplierDO  supplier    = new SupplierDO();

            //Loop to keep menu selection going.
            do
            {
                //Try catch to get any exeptions that may occur.
                try
                {
                    //Menu Options.
                    Console.Clear();
                    Console.WriteLine("\t\tPlease select option by pressing a key:\n");
                    Console.WriteLine(" 1) Display all");
                    Console.WriteLine(" 2) Create new");
                    Console.WriteLine(" 3) Update existing");
                    Console.WriteLine(" 4) Delete");
                    Console.WriteLine(" 5) Product menu");
                    Console.WriteLine(" 6) Exit Application");

                    //Allows user to press either number keys on keyboard or use num pad.
                    ConsoleKeyInfo keyPressed = Console.ReadKey();
                    Console.Clear();

                    //Declaring variables.
                    int supplierId = 0;

                    //Creates a new list object called items to store our list object returned from our ViewAllSuppliers().
                    List <SupplierDO> items = supplierDAO.ViewAllSuppliers();

                    //Displays an updated supplier list.
                    if (keyPressed.Key != ConsoleKey.D6 && keyPressed.Key != ConsoleKey.NumPad6)
                    {
                        DisplaySuppliers(items);
                    }

                    //Determines which key was pressed.
                    switch (keyPressed.Key)
                    {
                    //Display---------------------------------------------------------------------
                    case ConsoleKey.NumPad1:
                    case ConsoleKey.D1:
                        break;

                    //Create----------------------------------------------------------------------
                    case ConsoleKey.NumPad2:
                    case ConsoleKey.D2:
                        //Passing the object returned from GetUserInput method into the parameters for our CreateNewSuppliers().
                        supplierDAO.CreateNewSuppliers(GetUserInput());
                        break;

                    //Update----------------------------------------------------------------------
                    case ConsoleKey.NumPad3:
                    case ConsoleKey.D3:
                        Console.WriteLine("Please enter Supplier Id for the row you would like to change.");
                        //Getting the supplier Id that user wants to update.
                        int.TryParse(Console.ReadLine(), out supplierId);

                        //Adding information returned from GetUserInput() into our object.
                        supplier = GetUserInput();

                        //Setting the value of our object variable for supplierId.
                        supplier.SupplierId = supplierId;

                        //Passing all the user input into UpdateSuppliers() using our object named supplier.
                        supplierDAO.UpdateSuppliers(supplier);
                        break;

                    //Delete----------------------------------------------------------------------
                    case ConsoleKey.NumPad4:
                    case ConsoleKey.D4:
                        Console.WriteLine("Please enter Supplier Id.");

                        //Getting supplier Id that user wants to delete.
                        int.TryParse(Console.ReadLine(), out supplierId);

                        //Setting the value of our object variable for supplierId.
                        supplier.SupplierId = supplierId;

                        //Passing supplier Id to the DeleteSuppliers() so that user can select which row to delete.
                        supplierDAO.DeleteSuppliers(supplier.SupplierId);
                        break;

                    //Product Menu----------------------------------------------------------------
                    case ConsoleKey.NumPad5:
                    case ConsoleKey.D5:

                        //Products menu.
                        ProductsMenu();
                        break;

                    //Exit-------------------------------------------------------------------------
                    case ConsoleKey.NumPad6:
                    case ConsoleKey.D6:

                        //Exiting the console.
                        Environment.Exit(0);
                        break;

                    default:
                        break;
                    }

                    //Displays updated supply list if user does NOT press 1.
                    if (keyPressed.Key != ConsoleKey.NumPad1 && keyPressed.Key != ConsoleKey.D1)
                    {
                        items = supplierDAO.ViewAllSuppliers();
                        DisplaySuppliers(items);
                    }
                }
                //Catches any exception that gets thrown.
                catch (Exception ex)
                {
                    string currentClass = "Program";
                    //Prints error to console, logs, and restarts the menu.
                    supplierDAO.SupplierErrorHandler(ex, currentClass, currentMethod, ex.StackTrace);
                    throw ex;
                }
            } while (true);
        }
Пример #24
0
        static void Main(string[] args)
        {
            try
            {
                SupplierDAO dao = new SupplierDAO(connectionString, logsPath);

                Console.BufferHeight = 500;
                Console.WindowHeight = 30;

                bool exit = false;

                do
                {
                    bool wrongNum = false;
                    int  ID       = 0;
                    int  rowsAffected;

                    Console.WriteLine("We are in the Supplier table of NORTHWIND. \nWould you like to: \n\t" +
                                      "1) View all suppliers.\n\t" +
                                      "2) Update an existing supplier information.\n\t" +
                                      "3) Create a new supplier.\n\t" +
                                      "4) Delete a supplier.\n\t" +
                                      "5) Exit this program.");

                    //Ask to put 1-5, if not.
                    if (wrongNum)
                    {
                        Console.WriteLine("\nInput number 1-5:");
                        wrongNum = false;
                    }

                    int menuChoice = GetIntInput("Please enter a number between 1 and 5");
                    switch (menuChoice)
                    {
                    case 1:
                        //view all suppliers
                        List <SupplierDO> supInfoDO = dao.ObtainTableInfo();

                        List <SupplierPO> supplierInfo = Mapper.DOListToPOList(supInfoDO);

                        for (int i = 0; i < supplierInfo.Count; i++)
                        {
                            ViewSuppliers(supplierInfo[i]);
                        }

                        Console.WriteLine("Press any key to return to menu...");
                        Console.ReadKey();
                        break;

                    case 2:
                        //Update supplier
                        ID = GetIntInput("Input supplier ID");
                        Console.Clear();
                        //obtains single supplier based on id
                        SupplierDO suppDO = dao.ObtainSupplierSingle(ID);
                        //converts supplier to Presentation layer object
                        SupplierPO suppPO = Mapper.SupplierDOtoSupplierPO(suppDO);
                        //checks to see if the supplier exists, cannot have an id of 0 in the DB
                        if (suppPO.SupplierID != 0)
                        {
                            ViewSuppliers(suppPO);

                            suppPO = ObtainUpdatedInformation(suppPO);
                            suppDO = Mapper.SupplierPOtoSupplierDO(suppPO);
                            dao.UpdateInformation(suppDO);
                            Console.Clear();
                        }
                        //If Supplier does not exist this will be displayed
                        else
                        {
                            Console.WriteLine("The provided Supplier ID does not exist.\n" +
                                              "If you would like to create a new Supplier please select menu option 3.\n\n");
                        }

                        break;

                    case 3:
                        //Create New Supplier
                        Console.Clear();
                        //Obtains supplier table
                        SupplierPO newSupplier = ObtainNewSupplierInfo();
                        //converts from a Data layer Object to a presentation layer object
                        SupplierDO newDOSupplier = Mapper.SupplierPOtoSupplierDO(newSupplier);
                        //checks to see if supplier was added
                        rowsAffected = dao.CreateNewSupplier(newDOSupplier);
                        Console.Clear();
                        //if no rows affected, supplier was not added, print the following
                        if (rowsAffected == 0)
                        {
                            Console.WriteLine("Failed to create new Supplier.\n\n");
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("New supplier has been created.\n\n");
                            Console.ResetColor();
                            Console.WriteLine("Press any key to Return to menu...");
                            Console.ReadKey();
                            Console.Clear();
                        }
                        break;

                    case 4:
                        //Delete Supplier
                        ID = GetIntInput("Input supplier ID");
                        Console.Clear();

                        Console.WriteLine("\nThis row will be deleted: ");
                        suppDO = dao.ObtainSupplierSingle(ID);
                        SupplierPO suppPo = Mapper.SupplierDOtoSupplierPO(suppDO);
                        ViewSuppliers(suppPo);
                        if (GetIntInput("\nAre you sure you wish to delete this supplier?\n1) Yes 2)No") == 1)
                        {
                            //checks to see if the indicated supplier was deleted
                            rowsAffected = dao.DeleteSupplier(ID);
                            //prints failure or success message
                            if (rowsAffected == 0)
                            {
                                Console.WriteLine("Failed to Delete Supplier.\n\n");
                            }
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Green;
                                Console.WriteLine("Supplier Deleted\n\n");
                                Console.ResetColor();
                            }
                        }
                        Console.Clear();
                        break;

                    case 5:
                        exit = true;
                        break;

                    default:
                        Console.Clear();
                        wrongNum = true;
                        break;
                    }
                } while (exit == false);
            }
            catch (SqlException sqlEx)
            {
                Logger.errorLogPath = logsPath;
                Logger.ExceptionMessage(sqlEx);

                if (!(sqlEx.Data.Contains("Logged") && (bool)sqlEx.Data["Logged"] == true))
                {
                    Logger.SqlExceptionLog(sqlEx);
                }

                Console.WriteLine("\npress any key to close");
                Console.ReadKey();
            }
            catch (Exception exception)
            {
                Logger.errorLogPath = logsPath;
                Logger.ExceptionMessage(exception);

                if (!(exception.Data.Contains("Logged") && (bool)exception.Data["Logged"] == true))
                {
                    Logger.ExceptionLog(exception, "");
                }

                Console.WriteLine("\npress any key to close");
                Console.ReadKey();
            }
            finally
            {
                Console.Write("Closing Program...");
                Thread.Sleep(2500);
            }
        }
Пример #25
0
        static void Main(string[] args)
        {
            bool continueProgram = true;

            while (continueProgram)
            {
                Console.Clear();
                Console.ResetColor();
                Console.WriteLine("What would you like to do?\n");
                Console.WriteLine("\t" + "1) View all Suppliers");
                Console.WriteLine("\t" + "2) Add a new Supplier");
                Console.WriteLine("\t" + "3) Update Supplier Information");
                Console.WriteLine("\t" + "4) Delete a Supplier");
                Console.WriteLine("\t" + "5) Exit Application");

                switch (Console.ReadKey(true).Key)
                {
                case ConsoleKey.NumPad1:
                case ConsoleKey.D1:
                    List <SupplierDO> allSuppliers = dao.ObtainAllSuppliers();
                    List <SupplierPO> supplierInfo = Mapper.FromDoToPo(allSuppliers);
                    for (int i = 0; i < supplierInfo.Count; i++)
                    {
                        ViewAllSuppliers(supplierInfo[i]);
                    }
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                    break;

                case ConsoleKey.NumPad2:
                case ConsoleKey.D2:
                    Console.Clear();
                    SupplierPO supplier = new SupplierPO();
                    UserInput(supplier);
                    SupplierDO newSupp = Mapper.PoToDo(supplier);
                    dao.CreateSupplier(newSupp);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Data Stored");
                    Thread.Sleep(1800);
                    break;

                case ConsoleKey.NumPad3:
                case ConsoleKey.D3:
                    Console.Clear();
                    SupplierPO udatedSupplier = new SupplierPO();
                    UpdateInformation(udatedSupplier);
                    break;

                case ConsoleKey.NumPad4:
                case ConsoleKey.D4:
                    Console.Clear();
                    int suppId = GetId();
                    dao.DeleteSupplier(suppId);
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Entry Successfully deleted");
                    Thread.Sleep(1800);
                    break;

                case ConsoleKey.NumPad5:
                case ConsoleKey.D5:
                    continueProgram         = false;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Shutting down program");
                    Thread.Sleep(1800);
                    break;
                }
            }
        }
Пример #26
0
        public static void UpdateInformation(SupplierPO updateSupplier)
        {
            bool changesMade    = true;
            bool continueUpdate = true;
            int  ID             = GetId();

            ID = updateSupplier.SupplierID;
            dao.ObtainSupplierById(ID);
            SupplierDO suppliers = new SupplierDO();

            updateSupplier            = Mapper.DoToPo(suppliers);
            updateSupplier.SupplierID = ID;

            while (continueUpdate)
            {
                Console.Clear();
                Console.ResetColor();
                Console.WriteLine("What would you like to update");
                Console.WriteLine("\ta) Company Name");
                Console.WriteLine("\tb) Contact Name");
                Console.WriteLine("\tc) Contact Title");
                Console.WriteLine("\td) Address");
                Console.WriteLine("\te) City");
                Console.WriteLine("\tf) Region");
                Console.WriteLine("\tg) Postal Code");
                Console.WriteLine("\th) Country");
                Console.WriteLine("\ti) Phone");
                Console.WriteLine("\tj) Fax");
                Console.WriteLine("\tk) Home Page");
                Console.WriteLine("\tl) Exit");

                switch (Console.ReadKey(true).Key)
                {
                case ConsoleKey.A:
                    Console.Clear();
                    Console.WriteLine("Please Enter New Company Name");
                    updateSupplier.CompanyName = Console.ReadLine();
                    changesMade = true;
                    break;

                case ConsoleKey.B:
                    Console.Clear();
                    Console.WriteLine("Please Enter New Contact Name");
                    updateSupplier.ContactName = Console.ReadLine();
                    changesMade = true;
                    break;

                case ConsoleKey.C:
                    Console.Clear();
                    Console.WriteLine("Please Enter New Contact Title");
                    updateSupplier.ContactTitle = Console.ReadLine();
                    changesMade = true;
                    break;

                case ConsoleKey.D:
                    Console.Clear();
                    Console.WriteLine("Please Enter New Address");
                    updateSupplier.Address = Console.ReadLine();
                    changesMade            = true;

                    break;

                case ConsoleKey.E:
                    Console.Clear();
                    Console.WriteLine("Please Enter New City");
                    updateSupplier.City = Console.ReadLine();
                    changesMade         = true;
                    break;

                case ConsoleKey.F:
                    Console.Clear();
                    Console.WriteLine("Please Enter New Region");
                    updateSupplier.Region = Console.ReadLine();
                    changesMade           = true;
                    break;

                case ConsoleKey.G:
                    Console.Clear();
                    Console.WriteLine("Please Enter New Postal Code");
                    updateSupplier.PostalCode = Console.ReadLine();
                    changesMade = true;
                    break;

                case ConsoleKey.H:
                    Console.Clear();
                    Console.WriteLine("Please Enter New Country");
                    updateSupplier.Country = Console.ReadLine();
                    changesMade            = true;
                    break;

                case ConsoleKey.I:
                    Console.Clear();
                    Console.WriteLine("Please Enter New Phone Number");
                    updateSupplier.Phone = Console.ReadLine();
                    changesMade          = true;
                    break;

                case ConsoleKey.J:
                    Console.Clear();
                    Console.WriteLine("Please Enter New Fax Number");
                    updateSupplier.Fax = Console.ReadLine();
                    changesMade        = true;
                    break;

                case ConsoleKey.K:
                    Console.Clear();
                    Console.WriteLine("Please Enter New Home Page");
                    updateSupplier.HomePage = Console.ReadLine();
                    changesMade             = true;
                    break;

                case ConsoleKey.L:
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Returning to the Main menu");
                    continueUpdate = false;
                    Thread.Sleep(1800);
                    break;
                }
                if (changesMade)
                {
                    SupplierDO supplier = Mapper.PoToDo(updateSupplier);
                    dao.UpdateSupplier(supplier);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Update Successful");
                    Thread.Sleep(500);
                }
            }
        }