I've gone through the tutorial here: http://www.telerik.com/help/winforms/scheduler-appointments-and-dialogs-adding-a-custom-field-to-the-editappointment-dialog.html. I have been able to add my custom field to the scheduler, however when I run the application I get the following error:
Unable to cast object of type 'RadSchedulerDemo.GuardianAppointment' to type 'Telerik.WinControls.UI.IEvent'
This error occurs in the custom AppointmentFactory class I created, as directed by the tutorial, within the CreateNewAppointment function. I've searched for an answer to this issue but have not been able to find anything related to it.
Any ideas on what could be the problem?
10 Answers, 1 is accepted
Thanks,
David
Thank you for writing.
I am glad that you resolved that problem that you were facing. Your custom Appointment class should be a derivative of the Telerik.WinControls.UI.Appointment class as it is demonstrated in the referred help article.
Should you have further questions, I would be glad to help.
Regards,
Desislava
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
Hello - I have encountered a somewhat similar casting error when adding a custom field Email.
Unable to cast object of type 'Telerik.WinControls.UI.Appointment' to type 'RegWinRadScheduleBizObj.AppointmentWithEmail' in protected override void LoadSettingsFromEvent(Telerik.WinControls.UI.IEvent sourceEvent) My situation is slightly different in that I want to do away from creating dataset via .xds. I am trying to connect to biz object following this tutorial video starting at at https://youtu.be/RHgMwE2vlsc?t=30m40s
I've got all 3 classes MyAppointment.cs, MyPrepository.cs, and MyResouce.cs implemented per instructions and the appointments show up graphically on the schedule, apparently the mapping went well. However when trying get the 'inherit' AppointmentWithEmailEditForm to load (with additional Email field) that casting error pops up.
As far as inheritance, the implements are:
public class MyAppointment : INotifyPropertyChanged
public class AppointmentWithEmail : AppointmentWithEmail : Appointment
These blocks coming from the DataSet connection tutorial though
if I then do AppointmentWithEmail : MyAppointment, I will get Cannot implicitly convert type 'RegWinRadScheduleBizObj.BizModels.AppointmentWithEmail' to 'Telerik.WinControls.UI.IEvent' in the factory class
public class AppointmentWithEmailFactory : IAppointmentFactory
{ public IEvent CreateNewAppointment()
{
return new AppointmentWithEmail();
}
}
So creating the factory (1 of the 5 steps in the video) only applies for the SchedulerDataSet.xds scenario? Right now kind of confused, can you help me with the explicit conversion if the factory needed?
private void radScheduler1_AppointmentEditDialogShowing(object sender, AppointmentEditDialogShowingEventArgs e)
{
if (appointmentDiaglog == null)
appointmentDiaglog = new AppointmentWithEmailEditForm();
e.AppointmentEditDialog = appointmentDiaglog;
}
protected override void LoadSettingsFromEvent(Telerik.WinControls.UI.IEvent sourceEvent)
{ base.LoadSettingsFromEvent(sourceEvent);
var appointmentWithEmail = (AppointmentWithEmail)sourceEvent;
if (appointmentWithEmail.Email != null)
txtEmail.Text = appointmentWithEmail.Email;
}
protected override void ApplySettingsToEvent(Telerik.WinControls.UI.IEvent targetEvent)
{ var appointmentWithEmail = (AppointmentWithEmail)targetEvent; (error poped up here)
if (appointmentWithEmail.Email != null)
appointmentWithEmail.Email = txtEmail.Text;
base.ApplySettingsToEvent(targetEvent);
}
What am I missing?
Thanks in advance.
Thank you for writing.
The Adding a custom field to the EditAppointment dialog help article demonstrates a sample approach how to create custom appointments and use your own edit dialog. Note that it is important to assign the custom AppointmentFactory to the RadScheduler which is a possible reason for the cast error. In of data binding scenario, it is necessary to set the SchedulerBindingDataSource.EventProvider.AppointmentFactory with the custom one.
If you are still experiencing any further difficulties, feel free to open a support ticket and provide a sample project demonstrating the undesired behavior. Thus, we would be able to investigate the precise case and assist you further.
I hope this information helps. Should you have further questions I would be glad to help.
Dess
Telerik
I added this line
schedulerBindingDataSource1.EventProvider.AppointmentFactory = new AppointmentWithEmailFactory();
and radScheduler1.AppointmentFactory = new AppointmentWithEmailFactory(); prior
What go into my Form1 constructor is:
public Form1()
{
InitializeComponent();
MyRepository repo = new MyRepository();
this.myRepositoryBindingSource.DataSource = repo;
this.radScheduler1.ActiveViewType = SchedulerViewType.Week;
radScheduler1.AppointmentFactory = new AppointmentWithEmailFactory();
schedulerBindingDataSource1.EventProvider.AppointmentFactory = new AppointmentWithEmailFactory();
AppointmentMappingInfo appointmentMappingInfo = (AppointmentMappingInfo)this.schedulerBindingDataSource1.EventProvider.Mapping;
appointmentMappingInfo.Mappings.Add(new SchedulerMapping("Email", "Email"));
this.radScheduler1.AppointmentEditDialogShowing += new System.EventHandler<Telerik.WinControls.UI.AppointmentEditDialogShowingEventArgs>(this.radScheduler1_AppointmentEditDialogShowing);
}
I noted that Telerik inherits, i.e Appointment : Event and Event implements INotifyPropertyChanged. So I kept everything the same way the tutorial did
public class MyAppointment : INotifyPropertyChanged
public class AppointmentWithEmail : Telerik.WinControls.UI.Appointment
public class AppointmentWithEmailFactory : IAppointmentFactory
{
public IEvent CreateNewAppointment()
{
return new AppointmentWithEmail();
}
}
Also these are code-behind for the inherited AppointmentWithEmailEditForm
protected override void LoadSettingsFromEvent(Telerik.WinControls.UI.IEvent sourceEvent)
{
base.LoadSettingsFromEvent(sourceEvent);
var appointmentWithEmail = (AppointmentWithEmail)sourceEvent;
if (appointmentWithEmail.Email != null)
txtEmail.Text = appointmentWithEmail.Email;
}
protected override void ApplySettingsToEvent(Telerik.WinControls.UI.IEvent targetEvent)
{
var appointmentWithEmail = (AppointmentWithEmail)targetEvent;
if (appointmentWithEmail.Email != null)
appointmentWithEmail.Email = txtEmail.Text;
base.ApplySettingsToEvent(targetEvent);
}
protected override Telerik.WinControls.UI.IEvent CreateNewEvent()
{
return new AppointmentWithEmail();
}
still getting that error Unable to cast object of type 'Telerik.WinControls.UI.Appointment' to type 'RegWinRadScheduleBizObj.BizModels.AppointmentWithEmail' in LoadSettingsFromEvent when radScheduler1_AppointmentEditDialogShowing is fired.
Any suggestion?
Never mind now. The 'as' casting style seems to work better than the parentheses and I no longer have that casting error.
protected override void LoadSettingsFromEvent(Telerik.WinControls.UI.IEvent sourceEvent)
{
base.LoadSettingsFromEvent(sourceEvent);
AppointmentWithEmail appointmentWithEmail = new AppointmentWithEmail();
appointmentWithEmail = sourceEvent as AppointmentWithEmail;
if (appointmentWithEmail != null) // make sure null-checking the appointmentWithEmail object, not its "Email" property because the old code snippet did this and caused "Object reference not set to an instance of an object" error even though the casting has been resolved
txtEmail.Text = appointmentWithEmail.Email;
}
Hello - This is probably a follow-up question in regarding AppointmentWithEmail and getting the AppointmentWithEmailEditForm show up data fields correctly.
Now that I got the AppointmentWithEmailEditForm shows up without any casting error, I wish the assigned Room show up correctly in the Resource drop-down. In other words, my Resources in this case are available Rooms, the room-item in the Resource drop-down must reflected the correct room I specify when I create MyAppointment object programatically.
1. I would like to see it behave like the BackgroundId (the id corresponding to a certain color)
2. I seem to have trouble with ResouceId assignment here because it looks like the tutorial show how to bind a list of resouces id's to the Resources (because Resource is a composite entity I figure). Now I got the binding working but, when going about the other reverse direction, I wish to set my First Apppointment to Room 1XYZ (say if Resources was dropdown object, then, Resource.SelectedValue = _resource[0].id in this fashion....but it's not possilbe? How can I get around this to get it behave like the backgroundId, should I use Int instead of Guid for Resouces data type?
public MyRepository()
{
_resources = new List<MyResource>();
_resources.Add(new MyResource() { Name = "Room 1ABC" });
_resources.Add(new MyResource() { Name = "Room 1DEF" });
_resources.Add(new MyResource() { Name = "Room 1LMN" });
_resources.Add(new MyResource() { Name = "Room 1XYZ" });
_appointments = new List<MyAppointment>();
_appointments.Add(
new MyAppointment()
{
Start = DateTime.Now.AddDays(1),
End = DateTime.Now.AddDays(1).AddHours(2),
Location = "JARDINE HALL",
Description = "Tuition Financial Aid Workshop",
RecurrenceRule = "",
BackgroundId = 1,
//Resources = (_resources[0].Id), // I would like Room 1XYZ assignment happen here
Summary = "Important Forms and Deadlines"
});
_appointments[0].Resources.Add(_resources[0].Id); // this comes from the code snippet of the video tutorial
_appointments.Add(
new MyAppointment()
{
Start = DateTime.Now.AddDays(2),
End = DateTime.Now.AddDays(2).AddHours(3),
Location = "WALLACE HALL",
Description = "EE Workshop",
RecurrenceRule = "",
BackgroundId = 2,
//Resources = (_resources[1].Id), //I would like Room 1ABC assignment happen here and this value reflected in the AppoinmentWithEmailEditForm
Summary = "Circuit Diagram and Software Simulations" });
_appointments[1].Resources.Add(_resources[1].Id);
Any help please?
Thank you for writing back.
I am glad that the casting error you were facing is now resolved. However, according to the provided code snippets, it is difficult to investigate what is the exact problem with mapping the relevant resource id. That is why I have prepared a sample project which contains a backup file of the database as well. When opening the edit dialog, the correct resource is shown.
If you are still experiencing any further difficulties, I would kindly ask you again to open a support ticket and provide a sample project demonstrating the undesired behavior. Thus, we would be the fastest way to investigate the precise case and assist you further.
I hope this information helps. If you have any additional questions, please let me know.
Regards,
Dess
Telerik
Ms Dess,
Thanks for your replies. The sample you gave me is a Typed DataSet project which I got it working already but it is not the direction I am heading towards as I mentioned earlier. I'm trying to get the scheduler working with Business Objects which is the second part for the tutorial presentation.
The Dataset -- the creation of the .xsd would require an established database with several tables so that one can drag and drop them over from Server Explorer. The connection string is static, it works on one machine but not necessarily on the other. Even if one can create such datasets programmatically with a run time executed connection-string, it's an ordeal (if you look at the code behind designer.cs, it is a mess) Unless you guys have some advice to enlighten me, DataSet via .xsd file creation is only for demonstration purpose and not a robot solution for my situation.
I shall submit a support ticket soon should I feel the need. Thanks anyway.
Thank you for writing back.
The file is automatically generated when binding to a database from a local server for example. If you need to have access to the database from several machines, it is necessary to specify the connection string. However, note that the RadScheduler data binding approach is similar when using custom objects. It is necessary to specify the mapping information. The following help article is quite useful on this topic: http://docs.telerik.com/devtools/winforms/scheduler/data-binding/binding-to-business-objects
Indeed, opening a support ticket with your sample project would be the fastest way to investigate the precise case and proceed further.
If you have any additional questions, please let me know.
Regards,
Dess
Telerik