In this step you'll add a database to your project, and begin the process of binding the grid. Note that in this step you'll be using the standard Northwind database and an example code file, both of which should be installed with the Studio for Silverlight samples.
To set up your project, complete the following steps:
1. In the Solution Explorer, expand the .Web project (for example ComponentOneDataGrid.Web) and if the App_Data folder is not visible, right click the .Web project, and select Add | New Folder. Name the folder "App_Data".
2. In the Solution Explorer, right click the App_Data node, and select Add | Existing Item.
3. In the Add Existing Item dialog box, navigate to the ComponentOne Samples\Studio for Silverlight\C1.Silverlight.DataGrid\C1DataGrid_MDSL\C1_MDSLWeb\App_Data directory within the Documents or My Documents folder, select the NWIND.mdb file, and click Add to add it to your project.
4. In the Solution Explorer, select the NWIND.MDB file you just added, and in the Properties window set its Build Action property to None
5. In the Solution Explorer, right-click the .Web project (for example ComponentOneDataGrid.Web) and select Add | Existing Item.
6. In the Add Existing Item dialog box, navigate to the ComponentOne Samples\Studio for Silverlight\C1.Silverlight.DataGrid\C1DataGrid_MDSL\C1_MDSLWeb directory within the Documents or My Documents folder, select the SmartDataSet.cs file, and click Add to add it to your project.
Note that for Visual Basic projects, you can find the SmartDataSet.vb file posted online in the forums. This file contains code allowing data transfer to and from the database.
7. In the Solution Explorer, right-click the .Web project and select Add | New Item.
8. In the left pane of the Add New Item dialog box, select the Web item.
9. In the templates list, select Web Service, name the Web Service "DataService.asmx", and click the Add button. Note that the Web Service file will be added to your project and automatically opened.
10. In the DataService.asmx file, add the following using statements at the top of the file:
Imports System.IO
Imports System.Data
Imports C1_MDSLWeb ' SmartDataSet namespace
•C#
using System.IO;
using System.Data;
using C1_MDSLWeb; // SmartDataSet namespace
11. Next, uncomment the [System.Web.Script.Services.ScriptService] or <System.Web.Script.Services.ScriptService()> line.
This will allow the Web Service to be called from script.
12. Delete the existing HelloWorld method and replace it with the following code:
<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
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
Return dataSet
End Function
•C#
[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();
}
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;
return dataSet;
}
This code will create a dataset and take data from the database.
13. Right-click the .Web project (for example ComponentOneDataGrid.Web) and select Build from the context menu. Note that you'll now be done with the ComponentOneDataGrid.Web project and will return to working with the ComponentOneDataGrid project.
What You've Accomplished
In this step you've added a database to your project and created a Web Service. In the next step you'll finish connecting the Web Service to your project and you'll run your application.