Blogs

Rachel Reese - DevExpress Scheduler & RichEdit Blog

ASP.NET Scheduler: Getting Started - Binding to a Data Source

     

Since I'm just as new to our Scheduler control as some of you, I thought I'd start with a series of posts showing you just how easy it is to do some of the basics! I've gotten several questions already on how to bind the data to the ASP.NET Scheduler control, so we'll start here! :)

First, if you need to create your database to work with our Scheduler, you will need the scripts from this article.

We'll begin with a brand new ASP.NET application, and just drag the ASPxScheduler from the toolbox:

image

into the MainContent section, then click the arrow on the upper right corner of the ASP.NET Scheduler:

image

and you'll see the ASPxScheduler Tasks menu. We'll need to set the data source for both appointments and resources (what it is what we're scheduling: people, rooms, equipment, etc.). So, click the data source menu, and choose "<New Data Source>."

image

This will pop up the Data Source Configuration Wizard:

image

I'm going to use a SQL Server Compact file, so I click SQL Server, enter a name for the data source, and click OK. Next, I need to create the connection string. So, I click New Connection:

image

Then, enter the path to the file, and click OK:

image

Save the connection string to the application, and click Next:

imageSpecify the columns that you will need for your appointments, then click Advanced:

image

and check "Generate INSERT, UPDATE, and DELETE statements," then click OK.

image

And click Next, and then Finish. Next, the Mappings Wizard will pop up, and let you verify that the columns that the Scheduler requires are mapped to the columns in your database properly. Required fields are ID, Start, and End. Verify these, and click Next:

image

Now, you can set the Custom Fields. Choose the additional fields you'd like to include, move them to the right column and, finally, click Finish:

image

Once that is done, reopen the Tasks menu on the ASPxScheduler, and repeat for the resource data source. If you need to modify the data source at any time, there is an available Tasks menu with a "Configure Data Source…" option which will bring back up the Data Source Configuration Wizard:

image

One final step to be able to post data back into the database. We need to determine (using SELECT @@IDENTITY), and then assign, the correctly calculated ID value in the INSERT statement. To do this, highlight the data source, then click the InsertQuery property:

image

to invoke the Command and Parameter Editor:

image

Now, remove the ID parameter from the INSERT statement text and the parameter list. To generate the correct ID, we'll need to handle a couple events, like so:

int lastInsertedAppointmentId;

protected void ASPxScheduler1_AppointmentRowInserting(object sender,
ASPxSchedulerDataInsertingEventArgs e)
{
    // Remove unnecessary ID field. 
    e.NewValues.Remove("ID");
}

protected void appointmentDataSource_Inserted(object sender,
SqlDataSourceStatusEventArgs e)
{
    // Obtain the identity value. 
    SqlConnection connection = (SqlConnection)e.Command.Connection;
    using (SqlCommand cmd = new SqlCommand("SELECT @@IDENTITY", connection))
    {
        lastInsertedAppointmentId = (int)cmd.ExecuteScalar();
    }
}

protected void ASPxScheduler1_AppointmentRowInserted(object sender,
ASPxSchedulerDataInsertedEventArgs e)
{
    // Specify new ID field value. 
    e.KeyFieldValue = lastInsertedAppointmentId;
}

protected void ASPxScheduler1_OnAppointmentsInserted(object sender,
PersistentObjectsEventArgs e)
{
    // Store the new appointment. 
    int count = e.Objects.Count;
    System.Diagnostics.Debug.Assert(count == 1);
    Appointment apt = (Appointment)e.Objects[0];
    ASPxSchedulerStorage storage = (ASPxSchedulerStorage)sender;
    storage.SetAppointmentId(apt, lastInsertedAppointmentId);
}

For more information, see Step 13-14 in How to: Bind an ASPxScheduler to Data at Design Time.

And when we run the project, we're good to go! 

image

Hope this helps!

For other data sources and additional examples:

How to bind ASPxScheduler to SQL Server database
How to bind ASPxScheduler to ObjectDataSource
How to bind ASPxScheduler to SyBase ASE 15 database
How to bind ASPxScheduler to LINQ Data Source
How to bind ASPxScheduler with multi-resource appointments to SQL Server database
How to bind ASPxScheduler to XPO via the Unit of Work

For additional documentation:

http://documentation.devexpress.com/#AspNet/CustomDocument3685
http://documentation.devexpress.com/#AspNet/CustomDocument3844

Published Aug 03 2011, 09:59 AM by Rachel Reese (DevExpress)
Technorati tags: Scheduler, ASP.NET, ASPxScheduler
Bookmark and Share

Comments

No Comments
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.