public static string UpdateEvent(CalendarEvent cevent) { List<int> idList = (List<int>)System.Web.HttpContext.Current.Session["idList"]; if (idList != null && idList.Contains(cevent.idCal)) { if (CheckAlphaNumeric(cevent.Nome) && CheckAlphaNumeric(cevent.Oggetto)) { EventDAO.updateEvent(cevent.idCal, cevent.Nome, cevent.Oggetto); return "updated event with id:" + cevent.idCal + " update title to: " + cevent.Nome + " update description to: " + cevent.Oggetto; } } return "unable to update event with id:" + cevent.idCal + " title : " + cevent.Nome + " description : " + cevent.Oggetto; }
//this method retrieves all events within range start-end public static List<CalendarEvent> getEvents(DateTime start, DateTime end) { List<CalendarEvent> events = new List<CalendarEvent>(); SqlConnection con = new SqlConnection(connectionString); string sSQL = @"SELECT IDCal, IDUser, IDAss, Data, OraBegin, OraEnd, Nome, Oggetto, bArchiviato,bAllDay timeCreator, IDGruppoBase FROM Calendario WHERE (Data >= @start) AND (Data <= @end)"; SqlCommand cmd = new SqlCommand(sSQL, con); cmd.Parameters.Add("@start", SqlDbType.DateTime).Value = start; cmd.Parameters.Add("@end", SqlDbType.DateTime).Value = end; using (con) { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { CalendarEvent cevent = new CalendarEvent(); cevent.idCal = (int)reader["IDCal"]; cevent.Nome = reader["Nome"].ToString(); cevent.Oggetto = (string)reader["Oggetto"]; cevent.Data = (DateTime)reader["Data"]; //TODO: Gestione impegni prolungati cevent.DataEnd = ((DateTime)reader["Data"]).AddHours(1); //cevent.allDay = (bool)reader["bAllDay"]; events.Add(cevent); } } return events; //side note: if you want to show events only related to particular users, //if user id of that user is stored in session as Session["userid"] //the event table also contains an extra field named 'user_id' to mark the event for that particular user //then you can modify the SQL as: //SELECT event_id, description, title, event_start, event_end FROM event where user_id=@user_id AND event_start>=@start AND event_end<=@end //then add paramter as:cmd.Parameters.AddWithValue("@user_id", HttpContext.Current.Session["userid"]); }
public static int addEvent(ImproperCalendarEvent improperEvent) { // FullCalendar 1.x //CalendarEvent cevent = new CalendarEvent() //{ // title = improperEvent.title, // description = improperEvent.description, // start = DateTime.ParseExact(improperEvent.start, "dd-MM-yyyy hh:mm:ss tt", CultureInfo.InvariantCulture), // end = DateTime.ParseExact(improperEvent.end, "dd-MM-yyyy hh:mm:ss tt", CultureInfo.InvariantCulture) //}; // FullCalendar 2.x CalendarEvent cevent = new CalendarEvent() { Nome = improperEvent.title, Oggetto = improperEvent.description, Data = Convert.ToDateTime(improperEvent.start).ToUniversalTime(), DataEnd = Convert.ToDateTime(improperEvent.end).ToUniversalTime(), allDay = improperEvent.allDay }; if (CheckAlphaNumeric(cevent.Nome) && CheckAlphaNumeric(cevent.Oggetto)) { int key = EventDAO.addEvent(cevent); List<int> idList = (List<int>)System.Web.HttpContext.Current.Session["idList"]; if (idList != null) { idList.Add(key); } return key; //return the primary key of the added cevent object } return -1; //return a negative number just to signify nothing has been added }
//this method adds events to the database public static int addEvent(CalendarEvent cevent) { //add event to the database and return the primary key of the added event row //insert SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("INSERT INTO ECICalendarEvent_Test(title, description, event_start, event_end, all_day) VALUES(@title, @description, @event_start, @event_end, @all_day)", con); cmd.Parameters.Add("@title", SqlDbType.VarChar).Value = cevent.Nome; cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = cevent.Oggetto; cmd.Parameters.Add("@event_start", SqlDbType.DateTime).Value = cevent.Data; cmd.Parameters.Add("@event_end", SqlDbType.DateTime).Value = cevent.DataEnd; cmd.Parameters.Add("@all_day", SqlDbType.Bit).Value = cevent.allDay; int key = 0; using (con) { con.Open(); cmd.ExecuteNonQuery(); //get primary key of inserted row cmd = new SqlCommand("SELECT max(event_id) FROM ECICalendarEvent_Test where title=@title AND description=@description AND event_start=@event_start AND event_end=@event_end AND all_day=@all_day", con); cmd.Parameters.Add("@title", SqlDbType.VarChar).Value = cevent.Nome; cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = cevent.Oggetto; cmd.Parameters.Add("@event_start", SqlDbType.DateTime).Value = cevent.Data; cmd.Parameters.Add("@event_end", SqlDbType.DateTime).Value = cevent.DataEnd; cmd.Parameters.Add("@all_day", SqlDbType.Bit).Value = cevent.allDay; key = (int)cmd.ExecuteScalar(); } return key; }