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

Custom DbSchedulerProvider throws a null reference error

3 Answers 186 Views
Scheduler
This is a migrated thread and some comments may be shown as answers.
Dheepa
Top achievements
Rank 1
Dheepa asked on 05 Oct 2007, 01:28 PM
I have defined a custom dbSchedulerProvider for my scheduler and when I run the appication I get a NullReference Exception. When i set a break point in different functions, it throws the error after the call to GetResourcesByType().  I wish to know what I am doing wrong here.


Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   Telerik.Web.UI.RadScheduler.BindResourcesFromProvider(IEnumerable`1 providedResourceTypes) +165
   Telerik.Web.UI.RadScheduler.PerformSelect() +147
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
   Telerik.Web.UI.RadScheduler.EnsureDataBound() +74
   Telerik.Web.UI.RadScheduler.CreateChildControls(Boolean bindFromDataSource) +65
   Telerik.Web.UI.RadScheduler.CreateChildControls() +29
   System.Web.UI.Control.EnsureChildControls() +87
   System.Web.UI.Control.PreRenderRecursiveInternal() +41
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360

Provider file:

using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Configuration;

using System.Configuration.Provider;

using System.Data;

using System.Data.Common;

using System.Data.SqlClient;

using Telerik.Web.UI;

public class WorldBridgeSchedulerProvider : DbSchedulerProviderBase

{

public override IEnumerable<Appointment> GetAppointments(RadScheduler owner)

{

List<Appointment> exceptions = new List<Appointment>();

using (DbConnection conn = OpenConnection())

{

DbCommand cmd = DbFactory.CreateCommand();

cmd.Connection = conn;

cmd.CommandText = "SELECT [VACExceptionID], [Reason], [StartDate], [EndDate], [RecurrenceRule], [RecurrenceParentId] FROM [VACException]";

using (DbDataReader reader = cmd.ExecuteReader())

{

while (reader.Read())

{

Appointment apt = new Appointment();

apt.Owner = owner;

apt.ID = reader["VACExceptionID"];

apt.Subject = Convert.ToString(reader["Reason"]);

apt.Start = DateTime.SpecifyKind(Convert.ToDateTime(reader["StartDate"]), DateTimeKind.Utc);

apt.End = DateTime.SpecifyKind(Convert.ToDateTime(reader["EndDate"]), DateTimeKind.Utc);

apt.RecurrenceRule = Convert.ToString(reader["RecurrenceRule"]);

apt.RecurrenceParentID = reader["RecurrenceParentId"] == DBNull.Value ? null : reader["RecurrenceParentId"];

if (apt.RecurrenceParentID != null)

{

apt.RecurrenceState = RecurrenceState.Exception;

}

else

if (apt.RecurrenceRule != string.Empty)

{

apt.RecurrenceState = RecurrenceState.Master;

}

LoadResources(apt);

exceptions.Add(apt);

}

}

}

return exceptions;

}

public override void Insert(RadScheduler owner, Appointment appointmentToInsert)

{

if (!PersistChanges)

{

return;

}

using (DbConnection conn = OpenConnection())

{

using (DbTransaction tran = conn.BeginTransaction())

{

DbCommand cmd = DbFactory.CreateCommand();

cmd.Connection = conn;

cmd.Transaction = tran;

PopulateAppointmentParameters(cmd, appointmentToInsert);

cmd.CommandText =

@" INSERT INTO [VACException]

([Reason], [StartDate], [EndDate], [VACID],

[RecurrenceRule], [RecurrenceParentID])

VALUES (@Reason, @StartDate, @EndDate, @VACID,

@RecurrenceRule, @RecurrenceParentID)";

if (DbFactory is SqlClientFactory)

{

cmd.CommandText += Environment.NewLine + "SELECT SCOPE_IDENTITY()";

}

else

{

cmd.ExecuteNonQuery();

cmd.CommandText = "SELECT @@IDENTITY";

}

int identity = Convert.ToInt32(cmd.ExecuteScalar());

//FillClassStudents(appointmentToInsert, cmd, identity);

tran.Commit();

}

}

}

public override void Update(RadScheduler owner, Appointment appointmentToUpdate)

{

if (!PersistChanges)

{

return;

}

using (DbConnection conn = OpenConnection())

{

using (DbTransaction tran = conn.BeginTransaction())

{

DbCommand cmd = DbFactory.CreateCommand();

cmd.Connection = conn;

cmd.Transaction = tran;

PopulateAppointmentParameters(cmd, appointmentToUpdate);

cmd.Parameters.Add(CreateParameter("@VACExceptionID", appointmentToUpdate.ID));

cmd.CommandText = "UPDATE [VACException] SET [Reason] = @Reason, [StartDate] = @StartDate, [EndDate] = @EndDate, [VACID] = @VACID, [RecurrenceRule] = @RecurrenceRule, [RecurrenceParentID] = @RecurrenceParentID WHERE [VACExceptionID] = @VACExceptionID";

cmd.ExecuteNonQuery();

//ClearClassStudents(appointmentToUpdate.ID, cmd);

//FillClassStudents(appointmentToUpdate, cmd, appointmentToUpdate.ID);

tran.Commit();

}

}

}

public override void Delete(RadScheduler owner, Appointment appointmentToDelete)

{

if (!PersistChanges)

{

return;

}

using (DbConnection conn = OpenConnection())

{

DbCommand cmd = DbFactory.CreateCommand();

cmd.Connection = conn;

using (DbTransaction tran = conn.BeginTransaction())

{

cmd.Transaction = tran;

//ClearClassStudents(appointmentToDelete.ID, cmd);

cmd.Parameters.Clear();

cmd.Parameters.Add(CreateParameter("@VACExceptionID", appointmentToDelete.ID));

cmd.CommandText = "DELETE FROM [VACException] WHERE [VACExceptionID] = @VACExceptionID";

cmd.ExecuteNonQuery();

tran.Commit();

}

}

}

public override IEnumerable<ResourceType> GetResourceTypes(RadScheduler owner)

{

ResourceType[] resourceTypes = new ResourceType[2];

resourceTypes[0] = new ResourceType("VAC", false);

return resourceTypes;

}

public override IEnumerable<Resource> GetResourcesByType(RadScheduler owner, string resourceType)

{

switch (resourceType)

{

case "VAC":

return GetVACs();

default:

throw new InvalidOperationException("Unknown resource type: " + resourceType);

}

}

private void LoadResources(Appointment apt)

{

using (DbConnection conn = OpenConnection())

{

DbCommand cmd = DbFactory.CreateCommand();

cmd.Connection = conn;

cmd.Parameters.Add(CreateParameter("@VACExceptionID", apt.ID));

cmd.CommandText = "SELECT [VACID] FROM [VACException] WHERE [VACExceptionID] = @VACExceptionID AND [VACID] IS NOT NULL";

using (DbDataReader reader = cmd.ExecuteReader())

{

if (reader.Read())

{

Resource VAC = apt.Owner.Resources.GetResource("VAC", reader["VACID"]);

apt.Resources.Add(VAC);

}

}

}

}

private IEnumerable<Resource> GetVACs()

{

List<Resource> resources = new List<Resource>();

using (DbConnection conn = OpenConnection())

{

DbCommand cmd = DbFactory.CreateCommand();

cmd.Connection = conn;

cmd.CommandText = "SELECT [VACID], [VACName] FROM [VAC]";

using (DbDataReader reader = cmd.ExecuteReader())

{

while (reader.Read())

{

Resource res = new Resource();

res.Type = "VAC";

res.Key = reader["VACID"];

res.Text = Convert.ToString(reader["VACName"]);

resources.Add(res);

}

}

}

return resources;

}

private void PopulateAppointmentParameters(DbCommand cmd, Appointment apt)

{

cmd.Parameters.Add(CreateParameter("@Reason", apt.Subject));

cmd.Parameters.Add(CreateParameter("@StartDate", apt.Start));

cmd.Parameters.Add(CreateParameter("@EndDate", apt.End));

Resource VAC = apt.Resources.GetResourceByType("VAC");

object VACID = null;

if (VAC != null)

{

VACID = VAC.Key;

}

cmd.Parameters.Add(CreateParameter("@VACID", VACID));

string rrule = null;

if (apt.RecurrenceRule != string.Empty)

{

rrule = apt.RecurrenceRule;

}

cmd.Parameters.Add(CreateParameter("@RecurrenceRule", rrule));

object parentId = null;

if (apt.RecurrenceParentID != null)

{

parentId = apt.RecurrenceParentID;

}

cmd.Parameters.Add(CreateParameter("@RecurrenceParentId", parentId));

}

}


Create Scripts for the tables:
CREATE TABLE [dbo].[VAC](
 [VACID] [int] IDENTITY(1,1) NOT NULL,
 [VACName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 CONSTRAINT [PK_VAC_1] PRIMARY KEY CLUSTERED
(
 [VACID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[VACException](
 [VACExceptionID] [int] IDENTITY(1,1) NOT NULL,
 [VACID] [int] NOT NULL,
 [StartDate] [datetime] NOT NULL,
 [EndDate] [datetime] NOT NULL,
 [Reason] [nvarchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 [RecurrenceRule] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 [RecurrenceParentID] [int] NULL,
 CONSTRAINT [PK_VACException] PRIMARY KEY CLUSTERED
(
 [VACExceptionID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
GO
ALTER TABLE [dbo].[VACException]  WITH CHECK ADD  CONSTRAINT [FK_VACException_VAC] FOREIGN KEY([VACID])
REFERENCES [dbo].[VAC] ([VACID])


3 Answers, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 08 Oct 2007, 04:50 PM

Hi Dheepa,

Could you please open a support ticket and send us a sample project demonstrating the issue. Also, you can use a debugger to see where exactly the exception is thrown and let us know. This will give us some clue as to what is causing the problem.


Best wishes,
Peter
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Ian Poley
Top achievements
Rank 1
answered on 14 Oct 2007, 06:56 AM
Dheepa,

I believe this is the cause of your problem since I just came across the same issue:

ResourceType[] resourceTypes = new ResourceType[2]; //Problem line
resourceTypes[0] =
new ResourceType("VAC", false);
return resourceTypes;

the problem line should read
ResourceType[] resourceTypes = new ResourceType[0]; 

You only define one of the resourceTypes in the array but allocate space for three. I hope this helps.

-Ian

0
Dheepa
Top achievements
Rank 1
answered on 15 Oct 2007, 12:52 PM
That solved it, thanks. I had copied teh example from the telerik site and forgot to change the resources. Thanks a lot.
Tags
Scheduler
Asked by
Dheepa
Top achievements
Rank 1
Answers by
Peter
Telerik team
Ian Poley
Top achievements
Rank 1
Dheepa
Top achievements
Rank 1
Share this question
or