public string InsertOrder(OrderDTO order, string username) { string orderID = ""; do { orderID = GenerateCode(); } while (IsOrderIDExist(orderID)); SqlConnection conn = new SqlConnection(strConnection); if (conn != null) { if (conn.State == System.Data.ConnectionState.Closed) { conn.Open(); } try { DateTime currentDate = DateTime.Now; string sql = "Insert into Orders values(@orderID, @scheduleID, @orderDate, @username, @phone, @email, @isCheckOut)"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@orderID", orderID); cmd.Parameters.AddWithValue("@scheduleID", order.ScheduleID); cmd.Parameters.AddWithValue("@orderDate", currentDate); cmd.Parameters.AddWithValue("@username", username); cmd.Parameters.AddWithValue("@phone", order.Phone); cmd.Parameters.AddWithValue("@email", order.Email); cmd.Parameters.AddWithValue("@isCheckOut", false); bool result = (cmd.ExecuteNonQuery() > 0); if (result) { OrderDetailDAO detailDAO = new OrderDetailDAO(); foreach (string seat in order.ListOfSeat) { result = detailDAO.InsertOrderDetail(orderID, seat); } } } finally { conn.Close(); } } return(orderID); }
public OrderDTO CheckOrder(string orderID) { OrderDTO output = null; SqlConnection conn = new SqlConnection(strConnection); if (conn != null) { if (conn.State == System.Data.ConnectionState.Closed) { conn.Open(); } try { string sql = "Select scheduleID, phone, email from Orders where orderID=@orderID and isCheckOut=@isCheckOut"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@orderID", orderID); cmd.Parameters.AddWithValue("@isCheckOut", false); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { string scheduleID = reader["scheduleID"].ToString(); string phone = reader["phone"].ToString(); string email = reader["email"].ToString(); OrderDetailDAO dao = new OrderDetailDAO(); List <string> listSeat = dao.GetAllSeatsInOrder(orderID); output = new OrderDTO { OrderID = orderID, Email = email, IsCheckOut = false, ListOfSeat = listSeat, Phone = phone, ScheduleID = scheduleID }; } } finally { conn.Close(); } } return(output); }