예제 #1
0
        public ActionResult Delete(int id, DeleteComputerViewModel computer)
        {
            try
            {
                // TODO: Add delete logic here
                using (SqlConnection conn = Connection)
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = @"DELETE
                                            FROM Computer
                                            WHERE Id = @id";

                        cmd.Parameters.Add(new SqlParameter("@id", id));

                        var rowsAffected = cmd.ExecuteNonQuery();

                        if (rowsAffected < 1)
                        {
                            return(NotFound());
                        }

                        return(RedirectToAction(nameof(Index)));
                    }
                }
            }
            catch (Exception ex)
            {
                return(View());
            }
        }
예제 #2
0
        // GET: Computers/Delete/5
        public ActionResult Delete(int id)
        {
            try
            {
                using (SqlConnection conn = Connection)
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = @"Select c.Id, c.Make, c.Model, c.PurchaseDate, COALESCE(e.FirstName, 'n/a') AS FirstName, COALESCE(e.LastName, 'n/a') AS LastName, COALESCE(e.Id, 0) AS EmployeeId
                                            FROM computer c
                                            LEFT JOIN employee e 
                                            on e.ComputerId = c.Id
                                            WHERE c.Id = @id";

                        cmd.Parameters.Add(new SqlParameter("@id", id));

                        var reader = cmd.ExecuteReader();
                        DeleteComputerViewModel computer = null;

                        if (reader.Read())
                        {
                            computer = new DeleteComputerViewModel()
                            {
                                Id       = reader.GetInt32(reader.GetOrdinal("Id")),
                                Make     = reader.GetString(reader.GetOrdinal("Make")),
                                Model    = reader.GetString(reader.GetOrdinal("Model")),
                                Employee = new Employee()
                                {
                                    Id        = reader.GetInt32(reader.GetOrdinal("EmployeeId")),
                                    FirstName = reader.GetString(reader.GetOrdinal("FirstName")),
                                    LastName  = reader.GetString(reader.GetOrdinal("LastName"))
                                }
                            };
                        }

                        return(View(computer));
                    }
                }
            }
            catch (Exception ex)
            {
                return(View());
            }
        }