Skip to content

Instantly share code, notes, and snippets.

@MawCeron
Created June 24, 2019 22:03
Show Gist options
  • Select an option

  • Save MawCeron/ba3d9ca995840854d48ff4db588be136 to your computer and use it in GitHub Desktop.

Select an option

Save MawCeron/ba3d9ca995840854d48ff4db588be136 to your computer and use it in GitHub Desktop.
Obtener datos de SQL para llenar fullcalendar
public class cEventos
{
private static string con;
public cEventos()
{
con = ConfigurationManager.ConnectionStrings["CRMCapa"].ConnectionString;
}
internal List<CalendarEvent> ObtenerCalendario(DateTime inicio, DateTime final)
{
try
{
List<CalendarEvent> events = new List<CalendarEvent>();
SqlConnection conexion = new SqlConnection(con);
string sql = @"SELECT e.id, c.Nombre as curso, s.Nombre as sede, e.inicio, e.final FROM Eventos e
LEFT JOIN Cursos c ON c.id = e.idCurso
LEFT JOIN Sedes s ON s.id = e.idSede
WHERE e.inicio>=@inicio AND e.final<=@final";
SqlCommand cmd = new SqlCommand(sql, conexion);
cmd.Parameters.AddWithValue("@inicio", inicio);
cmd.Parameters.AddWithValue("@final", final);
using (conexion)
{
conexion.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
events.Add(new CalendarEvent()
{
id = Convert.ToInt32(reader["id"]),
title = Convert.ToString(reader["curso"]),
description = Convert.ToString(reader["sede"]),
start = Convert.ToDateTime(reader["inicio"]),
end = Convert.ToDateTime(reader["final"]),
});
}
}
return events;
}
catch (Exception)
{
throw;
}
}
}
public class CalendarEvent
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
public DateTime start { get; set; }
public DateTime end { get; set; }
public bool allDay { get; set; }
}
public class ImproperCalendarEvent
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
public string start { get; set; }
public string end { get; set; }
public bool allDay { get; set; }
}
public class JsonHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
DateTime inicio = Convert.ToDateTime(context.Request.QueryString["start"]);
DateTime final = Convert.ToDateTime(context.Request.QueryString["end"]);
List<int> idList = new List<int>();
List<ImproperCalendarEvent> listaEventos = new List<ImproperCalendarEvent>();
cEventos bd = new cEventos();
foreach (CalendarEvent evento in bd.ObtenerCalendario(inicio, final))
{
listaEventos.Add(new ImproperCalendarEvent
{
id = evento.id,
title = evento.title,
start = String.Format("{0:s}", evento.start),
end = String.Format("{0:s}", evento.end),
description = evento.description,
});
}
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string sJSON = oSerializer.Serialize(listaEventos);
context.Response.Write(sJSON);
}
public bool IsReusable
{
get { return false; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment