public static void Initialize(KioskContext context) { if (!context.Kiosks.Any()) { context.Kiosks.AddRange( new Kiosk { Nomer = "1", ModelKioska = "C", Arenda = DateTime.Now.Date, Town = Towns.Гомель, Adress = "Ленина", Area = 10 }, new Kiosk { Nomer = "2", ModelKioska = "X", Arenda = DateTime.Now.Date, Town = Towns.Брагин, Adress = "Победы", Area = 15 }, new Kiosk { Nomer = "3", ModelKioska = "C", Arenda = DateTime.Now.Date, Town = Towns.Добруш, Adress = "Ильича", Area = 8 }, new Kiosk { Nomer = "4", ModelKioska = "X", Arenda = DateTime.Now.Date, Town = Towns.Гомель, Adress = "Советская", Area = 5 }, new Kiosk { Nomer = "5", ModelKioska = "Павильон", Arenda = DateTime.Now.Date, Town = Towns.Гомель, Adress = "Рогачевская", Area = 20 } ); context.SaveChanges(); } }
protected void Page_Load(object sender, EventArgs e) { //To get the string to create new reservation table string userType = Request.QueryString["UserType"]; string userID = Request.QueryString["UserID"]; string facilityID = Request.QueryString["FacilityID"]; string startDateTime = Request.QueryString["StartDateTime"]; string endDateTime = Request.QueryString["EndDateTime"]; string description = Request.QueryString["Description"]; //change the date and time info if necessary // see what is passed in to to the startDateTime & endDateTime DateTime startDate = DateTime.ParseExact(startDateTime, "dd-MMM-yyyy HH:mm", CultureInfo.InvariantCulture); DateTime endDate = DateTime.ParseExact(endDateTime, "dd-MMM-yyyy HH:mm", CultureInfo.InvariantCulture); string startDateString = startDate.ToString("dd-MMM-yyyy"); string startTimeString = startDate.ToString("HH:mm"); string endDateString = endDate.ToString("dd-MMM-yyyy"); string endTimeString = endDate.ToString("HH:mm"); FRSWS.WSfrsClient ws = new FRSWS.WSfrsClient(); //call the NYP saveFRSEntries method to insert to database string result = HttpUtility.UrlDecode(ws.saveFRSEntries("STAFF", "S1999557YF", "STAFF", "S1999557YF", facilityID, "", "", startDateString, endDateString, startTimeString, endTimeString, "Y", "Y", description, "N", "", "", "", "", "", "")); //split the string result //if 0~ , success //else -1~ERRORMESSAGE....., error string[] tokens = result.Split(new[] { "~" }, StringSplitOptions.None); //if successfully return OK if (tokens[0] == "0") { //trigger a refresh of the cache database!!! ** using (var db = new KioskContext()) { FacilityReservation reser = new FacilityReservation(); Random r = new Random(); //set all fields here reser.FacilityReservationID = "T" + DateTime.Now.ToString("MMddHHmmSS") + "_" + r.Next(9999).ToString("0000"); reser.FacilityID = facilityID; reser.StartDateTime = startDate; reser.EndDateTime = endDate; reser.UseDescription = description; db.Reservations.Add(reser); db.SaveChanges(); } //returns ok/error message to caller Response.Write("{"); Response.Write(" Result: \"OK\","); Response.Write(" Message: \"The facility reservation is received and inserted into NYP's database successfully.\""); Response.Write("}"); Response.End(); } else { //returns ok/error message to caller Response.Write("{"); Response.Write(" Result: \"ERROR\","); Response.Write(" Message: \"" + tokens[1] + "\""); Response.Write("}"); Response.End(); } }
//******write codes to return OK/Error Message to the Mr Chow's console app!!!!!!**** protected void Page_Load(object sender, EventArgs e) { //Get the department ID and json string pass to my webservice string departmentID = Request.QueryString["DepartmentID"]; string json = Request.Form["Json"]; //count the number of exception catch from db.SaveChanges int exceptionCount = 0; if (departmentID != null && json != null) { //store the deserialized json array to a list of reservations ReservationList list = JsonConvert.DeserializeObject<ReservationList>(json); using (var db = new KioskContext()) { //delete the whole FacilityReservation Table //db.Database.ExecuteSqlCommand( // "DELETE FacilityReservation"); //FROM Department INNER JOIN Facility ON Department.DepartmentID = Facility.DepartmentID " + //"INNER JOIN FacilityReservation ON Facility.FacilityID = FacilityReservation.FacilityID WHERE Department.DepartmentID = '" + departmentID + "'"); // DELETE reservations records not found in the list // Hashtable listOfReservationIDs = new Hashtable(); var reservationIDs = from r in db.Reservations select new { r.FacilityReservationID }; foreach(var reservationID in reservationIDs) listOfReservationIDs[reservationID.FacilityReservationID] = 1; foreach (Reservation res in list.Reservations) listOfReservationIDs.Remove(res.facilityReservationID); foreach (string reservationIDToDelete in listOfReservationIDs.Keys) { db.Database.ExecuteSqlCommand( "DELETE FacilityReservation WHERE FacilityReservationID = '" + reservationIDToDelete + "'"); } db.SaveChanges(); //loop through each reservations and insert into the database //record by record foreach (Reservation res in list.Reservations) { var reservations = from r in db.Reservations where r.FacilityReservationID == res.facilityReservationID select r; if (reservations.Count() > 0) { // Record exists, just update. foreach(var reservation in reservations) { reservation.FacilityID = res.facilityID; reservation.StartDateTime = res.startDateTime; reservation.EndDateTime = res.endDateTime; reservation.UseDescription = res.useDescription; reservation.UseShortDescription = res.useShortDescription; break; } } else { // Record does not exist, insert. FacilityReservation reser = new FacilityReservation(); //set all fields here reser.FacilityReservationID = res.facilityReservationID; reser.FacilityID = res.facilityID; reser.StartDateTime = res.startDateTime; reser.EndDateTime = res.endDateTime; reser.UseShortDescription = res.useShortDescription; reser.UseDescription = res.useDescription; db.Reservations.Add(reser); } } try { db.SaveChanges(); } catch (Exception) { exceptionCount += 1; } } //returns in Json format Response.Write("{"); Response.Write(" Result: \"OK\""); Response.Write(" Message: \"The record is received and inserted into database successfully. " + exceptionCount + " records not inserted possibly due to duplicate primary key or some other errors.\""); Response.Write("}"); Response.End(); } else { Response.Write("{"); Response.Write(" Result: \"ERROR\""); Response.Write(" Message: \"There is an error occured and data cannot be received.\""); Response.Write("}"); Response.End(); } }