0

I using DayPilot Lite 3.1 from :

http://www.daypilot.org

Now I like to fill the calendar with value from the my sql database but I don't know how ?

Can you help me, what I need to write in the protected DataTable getData()?

Here is the code behind:

namespace Rezervacii{

public partial class Events : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DayPilotCalendar1.StartDate = firstDayOfWeek(DateTime.Now, DayOfWeek.Sunday);
            DayPilotCalendar1.DataSource = getData();
            DataBind();
        }

    }
    protected DataTable getData()
    {

        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=Korisnik;Integrated Security=True";
        SqlDataAdapter da = new SqlDataAdapter("SELECT [id], [name], [startevent], [endevent] FROM [Event] WHERE id=@id , name=@name, startevent=@startevent, endevent=@endevent", con);

        DataTable dt;
        dt = new DataTable();
        da.Fill(dt);

       return dt;

    }


    private static DateTime firstDayOfWeek(DateTime day, DayOfWeek weekStarts)
    {
        DateTime d = day;
        while (d.DayOfWeek != weekStarts)
        {
            d = d.AddDays(-1);
        }

        return d;
    }
    protected void DayPilotCalendar1_BeforeEventRender(object sender, DayPilot.Web.Ui.Events.Calendar.BeforeEventRenderEventArgs e)
    {
        string color = e.DataItem["color"] as string;
        if (!String.IsNullOrEmpty(color))
        {
            e.DurationBarColor = color;
        }
    }


}  

Here is the code how was before I edit :

protected DataTable getData()
{
    DataTable dt;
    dt = new DataTable();
    dt.Columns.Add("start", typeof(DateTime));
    dt.Columns.Add("end", typeof(DateTime));
    dt.Columns.Add("name", typeof(string));
    dt.Columns.Add("id", typeof(string));
    dt.Columns.Add("color", typeof (string));

    DataRow dr;

    dr = dt.NewRow();
    dr["id"] = 0;
    dr["start"] = Convert.ToDateTime("15:50");
    dr["end"] = Convert.ToDateTime("15:55");
    dr["name"] = "Event 1";
    dt.Rows.Add(dr);
    return dt;
Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51
user1408956
  • 109
  • 2
  • 6
  • 18

1 Answers1

1

You can use something like this:

protected DataTable getData()
{

    SqlConnection con = new SqlConnection();
    con.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=Korisnik;Integrated Security=True";
    SqlDataAdapter da = new SqlDataAdapter("SELECT [id], [name], [startevent], [endevent] FROM [Event] WHERE NOT (([endevent] <= @start) OR ([startevent] >= @end))", con);
    da.SelectCommand.Parameters.AddWithValue("start", DayPilotCalendar1.StartDate);
    da.SelectCommand.Parameters.AddWithValue("end", DayPilotCalendar1.EndDate.AddDays(1));

    DataTable dt;
    dt = new DataTable();
    da.Fill(dt);

   return dt;

}
Dan
  • 2,157
  • 21
  • 15