SIGN UP MEMBER LOGIN:    
ARTICLE

Silverlight RIA Service using ADO.NET Entity Data Model: Part II

Posted by Raj Kumar Articles | Silverlight with C# June 03, 2010
This article demonstrates how to use RIA services with ADO.NET Entity Data Model and how to use page navigation in Silverlight.
Reader Level:
Download Files:
 

This article demonstrates how to use RIA services with ADO.NET Entity Data Model and how to use page navigation in Silverlight.

First of all as usual make a new Silverlight project and put name of project and location. By default is creates 2 project like this.

image1.gif

Image1.

There are two projects created there one is works as client and another one web project works as server.

Now add data model in web project and configure it to database.

image2.gif

Image2.

image3.gif

Image3

Click Next.

image4.gif

Image4.

Click Next. And choose database objects and click finish.

image5.gif

Image5

Now let's start working on client project.

First of all we have to fetch data on a grid. So declare some namespace on page.

xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices"
xmlns:dataform="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"
xmlns:my1="clr-namespace:RIAExample.Web.Services"
xmlns:my2="clr-namespace:RIAExample.Controls"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"

here is DomainDataSurce

<riaControls:DomainDataSourceAutoLoad="True" d:DesignData="{d:DesignInstancemy:Customer,CreateList=true}"Height="0"LoadedData="customerDomainDataSource_LoadedData" Name="customerDomainDataSource"LoadSize="10"QueryName="GetCustomersQuery" Width="0">

<
riaControls:DomainDataSource.DomainContext>
<
my1:CustomerDomainContext />
</
riaControls:DomainDataSource.DomainContext>

Filtering based on customer id with text box value

<riaControls:DomainDataSource.FilterDescriptors>
      <
riaControls:FilterDescriptor
      PropertyPath="CustomerID"
      Operator="StartsWith" Value="{BindingElementName=customerIDText, Path=Text}">
      </riaControls:FilterDescriptor>
</
riaControls:DomainDataSource.FilterDescriptors>

<riaControls:DomainDataSource.GroupDescriptors>
      <
riaControls:GroupDescriptorPropertyPath="CustomerID" />
</riaControls:DomainDataSource.GroupDescriptors>--></riaControls:DomainDataSource>

DataGrid part

<my2:BusyIndicator x:Name="busyIndicator1" Width="400"IsBusy="{BindingElementName=customerDomainDataSource, Path=DomainContext.IsLoading}" />

<sdk:DataGridAutoGenerateColumns="False"ItemsSource="{BindingElementName=customerDomainDataSource, Path=Data}"
Name="customerDataGrid"RowDetailsVisibilityMode="VisibleWhenSelected"MinHeight="250"
Height="Auto">
      <sdk:DataGrid.Columns>
            <
sdk:DataGridTemplateColumn Header="Customer ID">
                  <sdk:DataGridTemplateColumn.CellTemplate>
                        <
DataTemplate>
                              <
HyperlinkButton x:Name="CustomerIDHyperlink" Content="{Binding Path=CustomerID, Mode=OneWay}" Click="EditHyperlink_Click"></HyperlinkButton>
                        </DataTemplate>
                  </
sdk:DataGridTemplateColumn.CellTemplate>
            </
sdk:DataGridTemplateColumn>
            <
sdk:DataGridTextColumn x:Name="companyNameColumn" Binding="{Binding Path=CompanyName}" Header="Company Name" Width="SizeToHeader" />
            <sdk:DataGridTextColumn x:Name="contactNameColumn" Binding="{Binding Path=ContactName}" Header="Contact Name" Width="SizeToHeader" />
            <sdk:DataGridTextColumn x:Name="contactTitleColumn" Binding="{Binding Path=ContactTitle}" Header="Contact Title" Width="SizeToHeader" />
            <sdk:DataGridTextColumn x:Name="countryColumn" Binding="{Binding Path=Country}" Header="Country" Width="SizeToHeader" />
            <sdk:DataGridTextColumn x:Name="postalCodeColumn" Binding="{Binding Path=PostalCode}" Header="Postal Code" Width="SizeToHeader" />
      </sdk:DataGrid.Columns>
      </
sdk:DataGrid>
      <sdk:DataPager Height="26"HorizontalAlignment="right" Name="dataPager1"PageSize="10"VerticalAlignment="Top" Width="Auto" Source="{BindingElementName=customerDomainDataSource, Path=Data}" />

DataForm

<dataform:DataForm x:Name="dataForm1" Header="Customer Information"AutoGenerateFields="False"AutoEdit="False"AutoCommit="False"CurrentItem="{BindingSelectedItem,ElementName=customerDataGrid}" Margin="0,12,0,0"DeletingItem="dataForm1_DeletingItem">
      <dataform:DataForm.EditTemplate>
            <
DataTemplate>
                  <
StackPanel>
                        <
dataform:DataField Label="Customer ID">
                              <TextBoxIsReadOnly
                              Text="{BindingCustomerID, Mode=OneWay}" />
                        </dataform:DataField>
                        <
dataform:DataField Label="Company Name">
                              <TextBox Text="{BindingCompanyName, Mode=TwoWay}" />
                        </dataform:DataField>
                        <
dataform:DataField Label="Contact Name">
                              <TextBox Text="{BindingContactName, Mode=TwoWay}" />
                        </dataform:DataField>
                        <
dataform:DataField Label="Contact Title">
                              <TextBox Text="{BindingContactTitle, Mode=TwoWay}" />
                        </dataform:DataField>
                        <
dataform:DataField Label="Country">
                              <TextBox Text="{Binding Country, Mode=TwoWay,NotifyOnValidationError=True,ValidatesOnExceptions=True }" />
                        </dataform:DataField>
                        <
dataform:DataField Label="PostalCode">
                              <TextBox Text="{BindingPostalCode, Mode=TwoWay,NotifyOnValidationError=True,ValidatesOnExceptions=True }" />
                        </dataform:DataField>
                  </
StackPanel>
            </
DataTemplate>
      </
dataform:DataForm.EditTemplate>
</
dataform:DataForm>
<
StackPanel Orientation="Horizontal"HorizontalAlignment="Right" Margin="0,12,0,0">
      <Button x:Name="submitButton" Width="75" Height="23" Content="Submit" Margin="4,0,0,0" Click="submitButton_Click"/>
</StackPanel>

Now run the application, result should be like this.

image6.gif
Image6.

Now if you enter some text in text box then result should be filter automatically.

image7.gif
Image7.

Now if you click on customer id and you want sent that id t another page then u have to write code like this.

<sdk:DataGridTemplateColumn Header="Customer ID">
      <sdk:DataGridTemplateColumn.CellTemplate>
            <
DataTemplate>
                  <
HyperlinkButton x:Name="CustomerIDHyperlink" Content="{Binding Path=CustomerID, Mode=OneWay}" Click="EditHyperlink_Click"></HyperlinkButton>
            </DataTemplate>
      </
sdk:DataGridTemplateColumn.CellTemplate>
</
sdk:DataGridTemplateColumn>

private void EditHyperlink_Click(object sender, System.Windows.RoutedEventArgs e)
{
Customer currentEvent = customerDataGrid.SelectedItemasCustomer;
if (currentEvent != null)
{
NavigateToEditEvent(currentEvent.CustomerID);
}
}

private void NavigateToEditEvent(stringeventId)
{
NavigationService.Navigate(newUri("/Detail?CustomerID=" +
eventId, UriKind.Relative));
}


Detail page should be like this.

image8.gif


Image8.

Hope this will help. If you have any questions then download attached project or drop me a comment in www.c-sharcorner.com  comment section.

Login to add your contents and source code to this article
share this article :
post comment
 

I have an iissue that is caused after  update a model from the database.

Here are the sequence of events.

1)      Open the *.edmx file (e.g. Model1.edmx) from the Web project

2)      Right-Click and slect “Update Model from the Database”

3)      Follow the wizard and updated the changes I’ve made to the database  (for example , adding a new column)

4)At this point I see the tables my Model1.edmx are successfully updated (I could see the extra column I’ve added)

Also, I can retrieve (read) data and display it on my DataGrid

5) BUT, could not update, delete or add records back to the database.

Please note  that before I have execute the “Update Model from the Database” (#2), I was able to do update , delete and remove.

Posted by hab Jun 15, 2010

excellent raj, thanks again.

Posted by m l Jun 03, 2010

It's uploaded mick, yo can download it now.

Posted by Raj Kumar Jun 03, 2010

Thanks Raj,
I cannot find the attached project mate. Has it been uploaded?
Thanks
Mick

Posted by m l Jun 03, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor