Question

enter image description here

I am trying to follow this tutorial http://www.daypilot.org/calendar-tutorial.html. I want to create a calendar that uses the data in my database. I already create a table called event and data that is same as the tutorial. table name is called event, and there are 5 fields inside which is id, name, eventstart, eventend and resource.

But I am using .mdf for my database. And the tutorial is using sqlite for database. And I have errors in the code because I am not using sqlite database. I want to use .mdf for my database, how do I change my code?

public partial class number2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblVenue.Text = Session["roomvalue"] != null ? Session["roomvalue"].ToString() : "";

        if (!IsPostBack)
        {
            DayPilotCalendar1.StartDate = DayPilot.Utils.Week.FirstDayOfWeek(new DateTime(2009, 1, 1));
            DayPilotCalendar1.DataSource = dbGetEvents(DayPilotCalendar1.StartDate, DayPilotCalendar1.Days);
            DataBind();
        }
    }
    protected void DayPilotCalendar1_EventMove(object sender, DayPilot.Web.Ui.Events.EventMoveEventArgs e)
    {
        dbUpdateEvent(e.Value, e.NewStart, e.NewEnd);
        DayPilotCalendar1.DataSource = dbGetEvents(DayPilotCalendar1.StartDate, DayPilotCalendar1.Days);
        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update();
    }

        private DataTable dbGetEvents(DateTime start, int days)
    {
        SQLiteDataAdapter da = new SQLiteDataAdapter("SELECT [id], [name], [eventstart], [eventend] FROM [event] WHERE NOT (([eventend] <= @start) OR ([eventstart] >= @end))", ConfigurationManager.ConnectionStrings["projectConnectionString"].ConnectionString);
        da.SelectCommand.Parameters.AddWithValue("start", start);
        da.SelectCommand.Parameters.AddWithValue("end", start.AddDays(days));
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }

    private void dbUpdateEvent(string id, DateTime start, DateTime end)
    {
        using (SQLiteConnection con = new SQLiteConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))
        {
            con.Open();
            SQLiteCommand cmd = new SQLiteCommand("UPDATE [event] SET [eventstart] = @start, [eventend] = @end WHERE [id] = @id", con);
            cmd.Parameters.AddWithValue("id", id);
            cmd.Parameters.AddWithValue("start", start);
            cmd.Parameters.AddWithValue("end", end);
            cmd.ExecuteNonQuery();
        }
    }
}
Was it helpful?

Solution

I think you can achieve your goal by just replace 'SQLiteCommand' to "SqlCommand" and "SQLiteDataAdapter" to "SqlDataAdapter" and also the connectionstring . However, I do suggest you to do some refactoring to improve the maintainability of your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top