We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

How to perform CRUD action on Schedule using DataManager?

The following steps shows the way to perform CRUD operations on schedule using dataManager.

 

Step 1:  Create a default schedule and bind the appointment dataSource through dataManager as shown below,  

<ej:Schedule ID="Schedule1" ClientIDMode="Static" Height="525px" Width="100%" CurrentDate="10/10/2015" runat="server">

         <AppointmentSettings ApplyTimeOffset="false" Id="Id" Subject="Subject" AllDay="AllDay" StartTime="StartTime" EndTime="EndTime" Description="Description" Recurrence="Recurrence"  />

         <DataManager CrossDomain="true" URL="Default.aspx/GetData" CrudURL="Default.aspx/CrudResult" InsertURL="Default.aspx/InsertData" UpdateURL="Default.aspx/UpdateData" RemoveURL="Default.aspx/RemoveData" Adaptor="UrlAdaptor" />

</ej:Schedule>

 

Step 2: In controller section, define the GetData function which gets called on initial loading of the control as shown below.

 [WebMethod]

 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

  public static List<ScheduleAppointment> GetData()

        {

            // Mention the correct path of the database to connect. Also ensure, whether the provided database already exists.

            SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ScheduleData.mdf;Integrated Security=True");

            using (connection)

            {

 

                DataTable myDataSet = new DataTable();

                SqlConnection myAccessConn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ScheduleData.mdf;Integrated Security=True");

                SqlCommand myAccessCommand = new SqlCommand("SELECT * FROM ScheduleAppointments", myAccessConn);

                SqlDataAdapter myDataAdapter = new SqlDataAdapter(myAccessCommand);

                myAccessConn.Open();

                myDataAdapter.Fill(myDataSet);

                List<DataRow> datasource = myDataSet.AsEnumerable().ToList();

 

                List<ScheduleAppointment> studentList = new List<ScheduleAppointment>();

                for (int i = 0; i < datasource.Count; i++)

                {

                    ScheduleAppointment student = new ScheduleAppointment();

                    student.Id = Convert.ToInt32(datasource[i]["Id"]);

                    student.Subject = datasource[i]["Subject"].ToString();

                    student.StartTime = Convert.ToDateTime(datasource[i]["StartTime"]);

                    student.EndTime = Convert.ToDateTime(datasource[i]["EndTime"]);

                    student.AllDay = Convert.ToBoolean(datasource[i]["AllDay"]);

                    student.Recurrence = Convert.ToBoolean(datasource[i]["Recurrence"]);

                    student.RecurrenceRule = datasource[i]["RecurrenceRule"].ToString();

                    studentList.Add(student);

                }

                myAccessConn.Close();

                return studentList.ToList();

            }

        }   

 

Step 3: Define CrudResult, InsertData, UpdateData, RemoveData functions which gets called on every CRUD operations on schedule appointments respectively as shown below.

  [WebMethod]

  [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

    public static List<ScheduleAppointment> CrudResult(List<ScheduleAppointment> added, List<ScheduleAppointment> changed, List<ScheduleAppointment> deleted)

        {

            SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ScheduleData.mdf;Integrated Security=True");

            SqlDataAdapter adapter1 = new SqlDataAdapter();

            string sql = null;

            ScheduleDataDataContext db = new ScheduleDataDataContext();

            int intMax = db.ScheduleAppointments.ToList().Count > 0 ? db.ScheduleAppointments.ToList().Max(p => p.Id) : 0;

            if (added != null && added.Count > 0) // it will be called when an appointment is added through detailed window

            {

                var value = added[0];

                connection.Open();

                sql = "insert into ScheduleAppointments (Id,EndTime,Recurrence,StartTime,Subject,AllDay,RecurrenceRule) values(" + (intMax + 1) + ",'" + value.EndTime + "','" + value.Recurrence + "','" + value.StartTime + "','" + value.Subject + "','" + value.AllDay + "','" + value.RecurrenceRule + "')";

                adapter1.InsertCommand = new SqlCommand(sql, connection);

                adapter1.InsertCommand.ExecuteNonQuery();

                connection.Close();

            }

            if (changed != null && changed.Count > 0) // it will be called when an appointment is edited through detailed window

            {

                var filterData = db.ScheduleAppointments.Where(c => c.Id == Convert.ToInt32(changed[0].Id));

                if (filterData.Count() > 0)

                {

                    var value = changed[0];

                    connection.Open();

                    sql = "update ScheduleAppointments set EndTime='" + value.EndTime + "',Recurrence='" + value.Recurrence + "',StartTime='" + value.StartTime + "',Subject='" + value.Subject + "',AllDay='" + value.AllDay + "',RecurrenceRule='" + value.RecurrenceRule + "'where Id='" + value.Id + "'";

                    adapter1.UpdateCommand = new SqlCommand(sql, connection);

                    adapter1.UpdateCommand.ExecuteNonQuery();

                    connection.Close();

                }

            }

 

            if (deleted != null && deleted.Count > 0)

            {

                connection.Open();

                foreach (ScheduleAppointment dele in deleted)

                {

                    var app = db.ScheduleAppointments.ToList().Where(c => c.Id == Convert.ToInt32(dele.Id)).FirstOrDefault();

                    if (app != null)

                    {

                        SqlCommand delete = new SqlCommand("delete from ScheduleAppointments where Id=" + app.Id + "", connection);

                        delete.ExecuteNonQuery();

                    }

                }

                connection.Close();

            }

            return GetData();

        }

 

    [WebMethod]

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

       public static List<ScheduleAppointment> InsertData(ScheduleAppointment value) // this function will be called when an appointment is added through quick window

        {

            ScheduleDataDataContext db = new ScheduleDataDataContext();

            int intMax = db.ScheduleAppointments.ToList().Count > 0 ? db.ScheduleAppointments.ToList().Max(p => p.Id) : 0;

            SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ScheduleData.mdf;Integrated Security=True");

            SqlDataAdapter adapter1 = new SqlDataAdapter();

            string sql = null;

            connection.Open();

            sql = "insert into ScheduleAppointments (Id,EndTime,Recurrence,StartTime,Subject,AllDay,RecurrenceRule) values(" + (intMax + 1) + ",'" + value.EndTime + "','" + value.Recurrence + "','" + value.StartTime + "','" + value.Subject + "','" + value.AllDay + "','" + value.RecurrenceRule + "')";

            adapter1.InsertCommand = new SqlCommand(sql, connection);

            adapter1.InsertCommand.ExecuteNonQuery();

            connection.Close();

            return GetData();

        }

     [WebMethod]

     [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

       public static List<ScheduleAppointment> RemoveData(string key) // this function will be called when an appointment is deleted

        {

            SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ScheduleData.mdf;Integrated Security=True");

            connection.Open();

            int appId = Convert.ToInt32(key);

            SqlCommand delete = new SqlCommand("delete from ScheduleAppointments where Id=" + appId + "", connection);

            delete.ExecuteNonQuery();

            return GetData();

        }

 

    [WebMethod]

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

        public static List<ScheduleAppointment> UpdateData(ScheduleAppointment value) // this function will be called when an appointment is updated

        {

            ScheduleDataDataContext db = new ScheduleDataDataContext();

            SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ScheduleData.mdf;Integrated Security=True");

            string sql = null;

            SqlDataAdapter adapter1 = new SqlDataAdapter();

            var filterData = db.ScheduleAppointments.Where(c => c.Id == Convert.ToInt32(value.Id));

            if (filterData.Count() > 0)

            {

                connection.Open();

                sql = "update ScheduleAppointments set EndTime='" + value.EndTime + "',Recurrence='" + value.Recurrence + "',StartTime='" + value.StartTime + "',Subject='" + value.Subject + "',AllDay='" + value.AllDay + "',RecurrenceRule='" + value.RecurrenceRule + "'where Id='" + value.Id + "'";

                adapter1.UpdateCommand = new SqlCommand(sql, connection);

                adapter1.UpdateCommand.ExecuteNonQuery();

                connection.Close();

            }

            return GetData();

     }

 

Sample Links:

C#:

http://www.syncfusion.com/downloads/support/directtrac/general/ze/sample_CS1094729747

VB:

http://www.syncfusion.com/downloads/support/directtrac/general/ze/sample_VB-1252267230

Figure 1: Inserting appointment with a subject ‘New appointment’ into scheduler.

Figure 2: ‘New appointment’ getting stored into database.

 

Article ID: Published Date: Last Revised Date: Platform: Control:
8905 07/03/2018 07/03/2018 ASP.NET Web Forms Schedule
Did you find this information helpful?
Add Comment
You must log in to leave a comment

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.