Posted by Joe on May 3, 2008
I tried out the step-by-step example by Michael Sync for getting data into a datagrid in Silverlight via webservices. I skipped some steps as I already had a database set up, and I adapted the code to my specific needs. I’m very, very glad to say, though, with very little difficulties, it worked. I did have to surmise a few things, so I’ll jot them down here.
- You’ll need to create the ListingControl.xaml file. The easiest way would be to rename the default Page.xaml that’s created in the Silverlight project.
- In the ListingControl.xaml file, rename the default <Grid> from “LayoutRoot” to “ListingControl”
- Add a “Loaded” event to the <Grid> and choose the default name (ListingControl_Loaded)
- Add a DataGrid to the ListingControl.xaml file.
Here’s the code for the XAML file:
<UserControl xmlns:my=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data” x:Class=”SLTwo.Page”
xmlns=”http://schemas.microsoft.com/client/2007“
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml“
Width=”400″ Height=”300″>
<Grid x:Name=”ListingControl” Background=”White” Loaded=”ListingControl_Loaded”>
<my:DataGrid x:Name=”CustomersDataGrid” Grid.Row=”1″ Margin=”5″ AutoGenerateColumns=”True” />
</Grid>
</UserControl>
- Note that you may have a different solution name than the example (S2WebSrv). Mine was named SLTwo, so I had to change that in the <UserControl> tag.
- Be careful with your XML. My data had ampersands in various places, so I had to change the line of code in the RetrieveProduct method of the web service that adds the product name to the XML so it replaces “&” with “&”. The XDocument.Parse method choked on the XML it was receiving, but didn’t say why. Ugh.
Phew! What a long road just to get data flowing into a Silverlight app. Glad to say, with Michael Sync’s help, that obstacle has been overcome. Now, on to better things. Maybe I’ll try hooking up NHibernate to the ASP.NET project and play around with some more interactive content.
‘Til next time!
Posted in Silverlight | Tagged: Silverlight, web services, xml | 1 Comment »
Posted by Joe on May 2, 2008
I won’t rant too much here. Just updating on my latest endeavors with Silverlight. I was trying to wire up my test app to a MySQL database, but discovered you can’t easily do that because you can’t use a .dll (e.g. the MySql.Data.dll) in a Silverlight Application. That’s kind of a bummer. It turns out the Silverlight way of connecting to any data source is through a web service.
I have no real issues with this, except I wonder how performance will be on larger datasets. I’ll have to run some tests once I get it working . . . IF I get it working. For the life of me, I can’t get web services to work. I’ve seen a few dozen examples, but when trying to implement them myself, I run into blocks all the way. Hooking up to a standard .NET web service (.asmx) is painful, but doable. Looks like Silverlight prefers the WCF-style web service and Linq.
I coded up a WCF web service, then used Linq in Silverlight to read the data returned from the web service. Note, however, that I made an assumption that I would create one true .NET solution (non-Silverlight) that handled the data access and web service portion, and consume that web service in Silverlight. Alas, no . . . at least not by adding a service reference to my web service in the Silverlight app. I get a message stating the service needs to be in the same context. I don’t know what this means, and by this point, I’m quite frustrated with the whole thing, so I’ve taken a bit of a sabbatical from it.
I have a strong suspicion that I’m missing something important. Even trying to adapt the Digg example from Scott Guthrie left me a little bewildered that my own code was having so many troubles. That said, I just found another example from Michael Sync that I’m going to take a look at. It looks a lot more thorough at first glance. I’ll blog about my results with this soon.
Posted in Silverlight | Tagged: Silverlight, web services | 1 Comment »