|
<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> |
|
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();
}
}
[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;
DataClasses1DataContext db = new DataClasses1DataContext();
//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 (EndTime,Recurrence,StartTime,Subject,AllDay,RecurrenceRule) values(" + value.EndTime + "','" + value.Recurrence + "','" + value.StartTime + "','" + value.Subject + "','" + value.AllDay + "','" + value.RecurrenceRule + "')";
if (value.RecurrenceRule == null)
{
unitsParam.Value = DBNull.Value;
}
cmd.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
{
DataClasses1DataContext db = new DataClasses1DataContext();
SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ScheduleData.mdf;Integrated Security=True");
connection.Open();
var command = "insert into ScheduleAppointments (Subject, StartTime, EndTime, AllDay, Recurrence, RecurrenceRule) values (@Subject, @StartTime, @EndTime, @AllDay, @Recurrence, @RecurrenceRule)";
cmd.Parameters.AddWithValue("@StartTime", value.StartTime);
cmd.Parameters.AddWithValue("@EndTime", value.EndTime);
cmd.Parameters.AddWithValue("@AllDay", value.AllDay);
cmd.Parameters.AddWithValue("@Recurrence", value.Recurrence);
SqlParameter unitsParam = cmd.Parameters.AddWithValue("@RecurrenceRule", value.RecurrenceRule);
if (value.RecurrenceRule == null)
{
unitsParam.Value = DBNull.Value;
}
cmd.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
{
DataClasses1DataContext db = new DataClasses1DataContext();
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 + "'";
SqlCommand cmd = new SqlCommand(sql, connection);
SqlParameter unitsParam = cmd.Parameters.AddWithValue("@RecurrenceRule", value.RecurrenceRule);
if (value.RecurrenceRule == null)
{
unitsParam.Value = DBNull.Value;
}
adapter1.UpdateCommand = new SqlCommand(sql, connection);
adapter1.UpdateCommand.ExecuteNonQuery();
connection.Close();
}
return GetData();
} |
|
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<ej:Schedule ID="Schedule1" runat="server" OnServerAppointmentCreated="Schedule1_ServerAppointmentCreated" OnServerAppointmentSaved="Schedule1_ServerAppointmentSaved" OnServerAppointmentClick="Schedule1_ServerAppointmentClick">
</ej:Schedule>
</div>
</form> |