user44129
user44129

Reputation: 71

Connecting SQL to FullCalendar

I have a SQL Server Database with this layout

Table (    Giftee int
           UserEmail varchar (100)
           BDayDate Date()    
       );

I would like to connect it to FullCalendar to create a full day event. I don't need a start and end time. I'm trying to use the "Giftee" value as the "id" and "title" fields for full calendar. I'm also trying to use the "BDayDate" as the "start" date for fullcalendar.

I have been using this post for reference.

Creating a JSON result from SQL server database

I have an Events.cshtml page containing:

@{    var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
      var db = Database.Open ("table");
      var zach = WebSecurity.CurrentUserName;
      var sql = "SELECT BDayDate, Giftee FROM Giftee_Info WHERE UserEmail = @0";
      var result = db.Query(sql, zach);
      var data = result.Select(x => new
      {
          id = x.Giftee,
          title = x.Giftee,
          start = x.BDayDate.ToString("yyyy-MM-dd"),
          allDay = true
      }).ToArray();

      Json.Write(data, Response.Output);
      Response.ContentType = "application/json";
}

I also have a separate Calendar.cshtml page to connect to the Events.cshtml page. The Calendar page contains this code:

<script>  $(document).ready(function() {
           $('#calendar').fullCalendar({
           editable: false,
           events: '/Events.cshtml'});
          }); </script>

When I test the calendar, it opens correctly but does not display any dates that are currently in the SQL table. I'm not sure where the code went wrong. Is there anything I need to change or add? I'm not sure if my Calendar page and Events page are connecting properly.

Upvotes: 0

Views: 1027

Answers (1)

user44129
user44129

Reputation: 71

The Events.cshtml page needs to be 100% empty of anything outside the @{ }. The above code will work.

Upvotes: 1

Related Questions