public void CreateEvent(CalendarEvents s) { using (var cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename" + "=|DataDirectory|\\Database.mdf; Integrated Security=True")) { string _insertSql = @"INSERT INTO [dbo].[CalendarEvents] ([Assignment], [Class], [Due_Date], [Creator], [Recurring]) VALUES (@Assignment, @Class, @Due_Date, @Creator, @Recurring)"; var insertCmd = new SqlCommand(_insertSql, cn); insertCmd.Parameters .Add(new SqlParameter("@Assignment", SqlDbType.NVarChar)) .Value = s.Assignment; insertCmd.Parameters .Add(new SqlParameter("@Class", SqlDbType.NVarChar)) .Value = s.Class; insertCmd.Parameters .Add(new SqlParameter("@Due_Date", SqlDbType.DateTime)) .Value = s.Due_Date; insertCmd.Parameters .Add(new SqlParameter("@Creator", SqlDbType.NVarChar)) .Value = s.Creator; insertCmd.Parameters .Add(new SqlParameter("@Recurring", SqlDbType.NVarChar)) .Value = s.SelectedRecurring; cn.Open(); insertCmd.ExecuteNonQuery(); cn.Close(); } }
public ActionResult Delete(int id) { if (User.Identity.IsAuthenticated) { CalendarEvents Events = new CalendarEvents(); Events.deleteEventByID(id); return Json(true); } return RedirectToAction("Login", "Account"); }
public ActionResult Edit(CalendarEvents Events) { if (User.Identity.IsAuthenticated) { if (ModelState.IsValid) { Events.UpdateEvent(Events); return RedirectToAction("Index"); } return View(Events); } return RedirectToAction("Login", "Account"); }
public ActionResult Create(CalendarEvents calendar) { if (User.Identity.IsAuthenticated) { if (ModelState.IsValid) { calendar.Creator = User.Identity.Name; calendar.CreateEvent(calendar); return RedirectToAction("Index"); } return View(calendar); } return RedirectToAction("Login", "Account"); }
// GET: CalendarEvents/Edit/5 public ActionResult Edit(int? id) { if (User.Identity.IsAuthenticated) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } CalendarEvents Events = new CalendarEvents(); Events = Events.getEventByID(Convert.ToInt32(id)); return View(Events); } return RedirectToAction("Login", "Account"); }
// GET: CalendarEvents public ActionResult Index() { if (User.Identity.IsAuthenticated) { var userName = User.Identity.Name.ToString(); CalendarEvents Events = new CalendarEvents(); List<CalendarEvents> returnedList = new List<CalendarEvents>(); var list = Events.getEvents(); foreach (var items in list) { returnedList.Add(items); } return View(returnedList); } return RedirectToAction("Login", "Account"); }
public CalendarEvents getEventByID(int id) { CalendarEvents item = new CalendarEvents(); using (var cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename" + "=|DataDirectory|\\Database.mdf; Integrated Security=True")) { string _sql = @"SELECT * FROM [dbo].[CalendarEvents] WHERE [Id] = @i "; var cmd = new SqlCommand(_sql, cn); cmd.Parameters .Add(new SqlParameter("@i", SqlDbType.NVarChar)) .Value = id; cn.Open(); var reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { item.ID = Convert.ToInt32(reader["ID"].ToString()); item.Assignment = reader["Assignment"].ToString(); item.Class = reader["Class"].ToString(); item.Due_Date = Convert.ToDateTime(reader["Due_Date"].ToString()); item.Creator = reader["Creator"].ToString(); item.SelectedRecurring = reader["Recurring"].ToString(); } } reader.Dispose(); cmd.Dispose(); } return item; }
public void UpdateEvent(CalendarEvents s) { using (var cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename" + "=|DataDirectory|\\Database.mdf; Integrated Security=True")) { string _insertSql = @"UPDATE [dbo].[CalendarEvents] SET Assignment = @Assignment, Class = @Class, Due_Date = @Due_Date, Recurring = @Recurring WHERE Id = @Id"; var insertCmd = new SqlCommand(_insertSql, cn); insertCmd.Parameters .Add(new SqlParameter("@Assignment", SqlDbType.NVarChar)) .Value = s.Assignment; insertCmd.Parameters .Add(new SqlParameter("@Class", SqlDbType.NVarChar)) .Value = s.Class; insertCmd.Parameters .Add(new SqlParameter("@Due_Date", SqlDbType.DateTime)) .Value = s.Due_Date; insertCmd.Parameters .Add(new SqlParameter("@Id", SqlDbType.Int)) .Value = s.ID; insertCmd.Parameters .Add(new SqlParameter("@Recurring", SqlDbType.NVarChar)) .Value = s.SelectedRecurring; cn.Open(); insertCmd.ExecuteNonQuery(); cn.Close(); } }
public List<CalendarEvents> getEvents() { List<CalendarEvents> items = new List<CalendarEvents>(); using (var cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename" + "=|DataDirectory|\\Database.mdf; Integrated Security=True")) { string _sql = @"SELECT * FROM [dbo].[CalendarEvents]"; var cmd = new SqlCommand(_sql, cn); cn.Open(); var reader = cmd.ExecuteReader(); if (reader.HasRows) { try { while (reader.Read()) { CalendarEvents item = new CalendarEvents(); item.ID = Convert.ToInt32(reader["ID"].ToString()); item.Assignment = reader["Assignment"].ToString(); item.Class = reader["Class"].ToString(); item.Due_Date = Convert.ToDateTime(reader["Due_Date"].ToString()); item.Creator = reader["Creator"].ToString(); item.SelectedRecurring = reader["Recurring"].ToString(); items.Add(item); } } catch (Exception e) { return items; } } reader.Dispose(); cmd.Dispose(); } return items; }