Scheduler for WPF and Silverlight | ComponentOne
Scheduler for Silverlight Tutorials / Creating a Multi-User Schedule / Step 1 of 4: Creating the Application
In This Topic
    Step 1 of 4: Creating the Application
    In This Topic

    In this step, you will create a new Silverlight application, add the appropriate references and namespaces, and add two code files and a Data Service to the application.

    Follow these steps:

    1. Create a new Silverlight application in Visual Studio and name it MultiUser.
    2. Add the following references to your application by right-clicking the References folder in the Solution Explorer and selecting Add Reference from the list. Browse to the folder where your references are located and select the following assemblies:
      • C1.Silverlight
      • C1.Silverlight.Data
      • C1.Silverlight.DateTimeEditors
      • C1.Silverlight.Schedule
    3. Right-click the MultiUser.Web project and select Add | Existing Item from the list. Add the SmartData file that applies to your application (either the SmartData.cs or the SmartData.vb file) and the PlatformUriTranslator file that applies to your application (either the PlatformUriTranslator.cs or the PlatformUriTranslator.vb file).
    4. Right-click MultiUser.Web again and select Add | New Item from the list. Add a basic Web Service and name it DataService.asmx.
    5. Add the following code to the DataService.asmx file below the summary description for the web service:

      Visual Basic
      Copy Code
      Public Class DataService
          Inherits System.Web.Services.WebService
      
          <WebMethod()> _
          Public Function GetData(tables As String) As Byte()
              ' Create DataSet with connection string
              Dim ds = GetDataSet()
      
              ' Load data into DataSet
              ds.Fill(tables.Split(","c))
      
              ' Persist to stream
              Dim ms = New System.IO.MemoryStream()
              ds.WriteXml(ms, XmlWriteMode.WriteSchema)
      
              ' Return stream data
              Return ms.ToArray()
          End Function
      
          <WebMethod()> _
          Public Function UpdateData(dtAdded As Byte(), dtModified As Byte(), dtDeleted As Byte()) As String
              Try
                  UpdateData(dtAdded, DataRowState.Added)
                  UpdateData(dtModified, DataRowState.Modified)
                  UpdateData(dtDeleted, DataRowState.Deleted)
                  Return Nothing
              Catch x As Exception
                  Return x.Message
              End Try
          End Function
      
          Private Sub UpdateData(data As Byte(), state As DataRowState)
              ' No changes, no work
              If data Is Nothing Then
                  Return
              End If
      
              ' Load data into dataset
              Dim ds = GetDataSet()
              Dim ms = New MemoryStream(data)
              ds.ReadXml(ms)
              ds.AcceptChanges()
      
              ' Update row states with changes
              For Each dt As DataTable In ds.Tables
                  For Each dr As DataRow In dt.Rows
                      Select Case state
                          Case DataRowState.Added
                              dr.SetAdded()
                              Exit Select
                          Case DataRowState.Modified
                              dr.SetModified()
                              Exit Select
                          Case DataRowState.Deleted
                              dr.Delete()
                              Exit Select
                      End Select
                  Next
              Next
      
              ' Update the database
              ds.Update()
          End Sub
      
          Private Function GetDataSet() As SmartDataSet
              ' Get physical location of the mdb file
              Dim mdb As String = Path.Combine(Context.Request.PhysicalApplicationPath, "App_Data\Nwind.mdb")
      
              ' Check that the file exists
              If Not File.Exists(mdb) Then
                  Dim msg As String = String.Format("Cannot find database file {0}.", mdb)
                  Throw New FileNotFoundException(msg)
              End If
      
              ' Make sure file is not read-only (source control often does this...)
              Dim att As FileAttributes = File.GetAttributes(mdb)
              If (att And FileAttributes.[ReadOnly]) <> 0 Then
                  att = att And Not FileAttributes.[ReadOnly]
                  File.SetAttributes(mdb, att)
              End If
      
              ' Create and initialize the SmartDataSet
              Dim dataSet = New SmartDataSet()
              dataSet.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=" & mdb
      
              ' sample connection string for SQL Server Express (replace Initial Catalog value by the valid catalog name)
              ' dataSet.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test;Data Source=.\\SQLEXPRESS";
              Return dataSet
          End Function
      End Class
      

      C#
      Copy Code
      public class DataService : System.Web.Services.WebService
          {
              [WebMethod]
      
              public byte[] GetData(string tables)
              {
                  // Create DataSet with connection string
                  var ds = GetDataSet();
      
                  // Load data into DataSet
                  ds.Fill(tables.Split(','));
      
                  // Persist to stream
                  var ms = new System.IO.MemoryStream();
                  ds.WriteXml(ms, XmlWriteMode.WriteSchema);
      
                  // Return stream data
                  return ms.ToArray();
              }
      
              [WebMethod]
              public string UpdateData(byte[] dtAdded, byte[] dtModified, byte[] dtDeleted)
              {
                  try
                  {
                      UpdateData(dtAdded, DataRowState.Added);
                      UpdateData(dtModified, DataRowState.Modified);
                      UpdateData(dtDeleted, DataRowState.Deleted);
                      return null;
                  }
                  catch (Exception x)
                  {
                      return x.Message;
                  }
              }
      
              void UpdateData(byte[] data, DataRowState state)
              {
                  // No changes, no work
                  if (data == null)
                  {
                      return;
                  }
      
                  // Load data into dataset
                  var ds = GetDataSet();
                  var ms = new MemoryStream(data);
                  ds.ReadXml(ms);
                  ds.AcceptChanges();
      
                  // Update row states with changes
                  foreach (DataTable dt in ds.Tables)
                  {
                      foreach (DataRow dr in dt.Rows)
                      {
                          switch (state)
                          {
                              case DataRowState.Added:
                                  dr.SetAdded();
                                  break;
                              case DataRowState.Modified:
                                  dr.SetModified();
                                  break;
                              case DataRowState.Deleted:
                                  dr.Delete();
                                  break;
                          }
                      }
                  }
      
                  // Update the database
                  ds.Update();
              }
      
              SmartDataSet GetDataSet()
              {
                  // Get physical location of the mdb file
                  string mdb = Path.Combine(
                    Context.Request.PhysicalApplicationPath, @"App_Data\Nwind.mdb");
      
                  // Check that the file exists
                  if (!File.Exists(mdb))
                  {
                      string msg = string.Format("Cannot find database file {0}.", mdb);
                      throw new FileNotFoundException(msg);
                  }
      
                  // Make sure file is not read-only (source control often does this...)
                  FileAttributes att = File.GetAttributes(mdb);
                  if ((att & FileAttributes.ReadOnly) != 0)
                  {
                      att &= ~FileAttributes.ReadOnly;
                      File.SetAttributes(mdb, att);
                  }
      
                  // Create and initialize the SmartDataSet
                  var dataSet = new SmartDataSet();
                  dataSet.ConnectionString =
                    "provider=microsoft.jet.oledb.4.0;data source=" + mdb;
      
                  // sample connection string for SQL Server Express (replace Initial Catalog value by the valid catalog name)
                  // dataSet.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test;Data Source=.\\SQLEXPRESS";
                  return dataSet;
              }
          }
      }
      

    6. Right-click MultiUser.web one more time and select Add | Folder from the list. Name the new folder App_Data.
    7. Right-click the App_Data folder and select Add | Existing Item from the list. Locate and select the Nwind.mdb database.
    8. Rebuild your entire application and then right-click your main MultiUser application. Select Add  Service Reference from the list. The Add Service Reference dialog box will appear:

    9. Click the Discover button to find the available service reference and then follow these steps:        
      • Select the available service reference in the Services listbox.
      • Rename the service reference DataService.
      • Click OK to add the service reference to your application.

    In this step you created a new Silverlight application, added the appropriate assembly references, and added a service reference and two code files. In the next step you will add XAML to your MultiUser application.