This is a migrated thread and some comments may be shown as answers.

[Solved] AppointmentDataBound error after OnAppointmentInsert

2 Answers 405 Views
Scheduler
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 2
Jason asked on 22 Jan 2010, 04:46 PM
Hi,

I'm using AppointmentDataBound to apply my own cssClasses to scheduler appointments based on their type, status and colour.  The type, status and colour are fields I store in the appointments record in my SQL table.

This all works really well and I get icons based on the appointment type and colours based on their status etc...

My problem occurs after I insert a new appointment.  I get an error 'object variable or with block variable not set' in the AppointmentDataBound code.  I think this is because the dataitem is not available at this point.

This is really stumping me and I just can't seem to get around it.  Here's the code :-


Protected Sub Calendar_AppointmentDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.SchedulerEventArgs) Handles Calendar.AppointmentDataBound  
 
        'Grab details about the appointments appearance  
        Dim apptComputer As String = e.Appointment.DataItem("computer_name").ToString  
        Dim apptProject As String = e.Appointment.DataItem("project_name").ToString  
        Dim apptRelease As String = e.Appointment.DataItem("release_name").ToString  
        Dim apptType As Integer = e.Appointment.DataItem("appointment_type").ToString  
        Dim apptColour As String = "White" 
        Dim apptStatus As Integer = e.Appointment.DataItem("Status").ToString  
        Dim strStatus As String = "Scheduled" 
        Dim strCSSClass As String = "rsCategoryBlue" 
 
 
        e.Appointment.Subject = "Project: " & apptProject & Chr(13) & "Release: " & apptRelease & Chr(13) & "Computer: " & apptComputer  
 
        'Grab the appointment colour if it is assigned if not then  
        'leave the setting as white (generic).  
        If e.Appointment.DataItem("colour").ToString() <> "" Then 
            apptColour = e.Appointment.DataItem("colour").ToString()  
        End If 
 
        'apptStatus dictates the text colour of the appointment.  
        '1 - Scheduled = Blue  
        '2 - Failed = Red  
        '3 - Passed = Green  
        Select Case apptStatus  
            Case 1 'Scheduled  
                strStatus = "Scheduled" 
            Case 2 'Failed  
                strStatus = "Failed" 
            Case 3 'Passed  
                strStatus = "Passed" 
        End Select 
 
        'apptColour dictates the background colour of the appointment  
        'apptType dictates the icon displayed in the top left corner of the appointment  
        Select Case apptType  
            Case 1 'Generic Appointment  
            Case 2 'QualityControl  
                strCSSClass = "Quality_" & apptColour & "_" & strStatus & "_AppointmentStyle" 
            Case 3 'CompatibilityCheck  
                strCSSClass = "Compatible_" & apptColour & "_" & strStatus & "_AppointmentStyle" 
            Case 4 'PackageApp  
                strCSSClass = "Package_" & apptColour & "_" & strStatus & "_AppointmentStyle" 
            Case 5 'Release  
                strCSSClass = "Release_" & apptColour & "_" & strStatus & "_AppointmentStyle" 
        End Select 
 
        e.Appointment.CssClass = strCSSClass  
 
 
    End Sub 

Here's the Insert code which works everytime

    Protected Sub Calendar_OnAppointmentInsert(ByVal sender As ObjectByVal e As Telerik.Web.UI.SchedulerCancelEventArgs) Handles Calendar.AppointmentInsert  
 
        Dim sdsProjectAppointments As New SqlDataSource  
        Dim strConnection As String = ConfigurationManager.ConnectionStrings("RR3ConnectionString").ConnectionString  
        Dim myRecurrenceParentID As String = "" 
 
        'Check to see if the appointment has a RecurrenceParentID  
        'If so then set the variable if not then leave as "".  Avoids passing null to USP.  
        If e.Appointment.RecurrenceParentID <> Nothing Then 
            'If DirectCast(e.Appointment.RecurrenceParentID, Guid) <> Nothing Then  
            myRecurrenceParentID = e.Appointment.RecurrenceParentID.ToString  
        End If 
 
        sdsProjectAppointments.ConnectionString = strConnection  
        sdsProjectAppointments.InsertCommand = "usp_insert_project_appointment" 
        sdsProjectAppointments.InsertCommandType = SqlDataSourceCommandType.StoredProcedure  
 
        'Set the SQLDataSource Parameters.  The first set of parameters are grabbed from  
        'the new ExceptionAppointment part of the RecurrenceExceptionCreatedEventArgs.  
        'This contains the new records indivdual start and end times etc...  
        sdsProjectAppointments.InsertParameters.Add("Subject", e.Appointment.Subject.ToString)  
        sdsProjectAppointments.InsertParameters.Add("start", e.Appointment.Start.ToString)  
        sdsProjectAppointments.InsertParameters("start").DbType = DbType.DateTime  
        sdsProjectAppointments.InsertParameters.Add("end", e.Appointment.End.ToString)  
        sdsProjectAppointments.InsertParameters("end").DbType = DbType.DateTime  
        sdsProjectAppointments.InsertParameters.Add("recurrencerule", e.Appointment.RecurrenceRule.ToString)  
        sdsProjectAppointments.InsertParameters.Add("recurrenceparentid", myRecurrenceParentID)  
 
        'Now stuff in the parameters we got from our select statement  
        sdsProjectAppointments.InsertParameters.Add("project_guid""00000000-0000-0000-0000-000000000000")  
        sdsProjectAppointments.InsertParameters.Add("parent_project_guid""00000000-0000-0000-0000-000000000000")  
        sdsProjectAppointments.InsertParameters.Add("release_guid""00000000-0000-0000-0000-000000000000")  
        sdsProjectAppointments.InsertParameters.Add("app_env_guid""00000000-0000-0000-0000-000000000000")  
        sdsProjectAppointments.InsertParameters.Add("env_comp_guid""00000000-0000-0000-0000-000000000000")  
        sdsProjectAppointments.InsertParameters.Add("computer_guid""00000000-0000-0000-0000-000000000000")  
        sdsProjectAppointments.InsertParameters.Add("status""1"'Scheduled  
        sdsProjectAppointments.InsertParameters.Add("appointment_type""7"'Type 7 is an informational appointment for the build and release teams  
        sdsProjectAppointments.InsertParameters.Add("colour""White"'Unallocated  
 
        'Call the update method of the SQLDataSource  
        Try 
            sdsProjectAppointments.Insert()  
        Catch ex As Exception  
            MsgBox("An unhandled exception has occurred. " & ex.Message)  
        End Try 
 
    End Sub 
 


After the insert code runs the databound code is called again and any reference to e.appointment.dataitem or even e.appointment.id returns the 'object variable or with block variable not set' error.

Thanks for looking
Jase

2 Answers, 1 is accepted

Sort by
0
Jason
Top achievements
Rank 2
answered on 22 Jan 2010, 04:59 PM
Hi,

Isn't it always the way !!  You post a problem and then fix it yourself within a few minutes !!   I resolved this by putting the following statement at the start of my AppointmentDataBound code :-

If IsNothing(e.Appointment.DataItem) Then Exit Sub 

After the AppointmentInsert code the AppointmentDataBound code is called twice.  The first time with out the dataitem available.  Its then called again to rebind the scheduler and all of the appointments once again have their dataitems available.

Hope this helps others.

Thanks
Jase
0
Accepted
Peter
Telerik team
answered on 27 Jan 2010, 11:18 AM
Hello Jason,

Actually, you don't have to use e.Appointment.DataItem. You better access the appointments' attributes like this: e.Appointment.Attributes["appointment_type"]. This technique will work provided that 'appointment_type' is a field (column name) in your RadScheduler's data source and you have set CustomAttributeNames="appointment_type" for RadScheduler.


Greetings,
Peter
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Scheduler
Asked by
Jason
Top achievements
Rank 2
Answers by
Jason
Top achievements
Rank 2
Peter
Telerik team
Share this question
or