I do Academic and Windows Phone stuff at Microsoft Greece. Plus, I co-develop the First Words series for Windows Phone 7. Feel free to contact me on Twitter or Facebook
I had a request to create a proof of concept app on how to connect a Windows Phone 7 Silverlight application to a SQL Server database hosted on a PC. Since the Windows Phone API does not allow the developer to connect directly to the SQL Server, one has to create a WCF (Windows Communication Foundation) service that will expose operations to interact with the database. Let’s do this! Source: http://sdrv.ms/XUd0CN
1. I’ll be using Northwind database for the sample, and I’m including it as part of the files for download. You can use any database you want, of course. First, let’s create a web service application. We’ll be using the ASP.NET empty website template for this matter.
2. Let’s create a new ADO.NET Entity Model, in order to use the powerful Entity Framework to do our job. We call it NorthwindModel
3. We generate it from database, choose to include sensitive data in our connection string, check the table “Customers” and check “Pluralize or singularize generated object names”.
4.We create a new *Silverlight-enabled” web service in our project. We call it “NorthwindService”.
5. We write the code for the web service, that queries the database (via the Entity Framework mechanisms) and returns all Customer objects
6. We press F5 and our service is ready! Don’t close the browser or stop the project, as we need it to be alive so that we can reference it via our Windows Phone project.
7. Let’s create our mobile application. We create a new Windows Phone Silverlight application
8. We create a ListBox (giving it the name “lst”) to display ContactName and City of the customers that we will fetch from the WCF service
9. We’ll use the “Add Service Reference dialog” so that our application can “see” the web service. We copy the url of the service from the browser, and enter it into the dialog.
10. We select to “use” the service reference via C#’s using statement. We also “use” the System.ServiceModel namespace.
11. We write the code for the service operation call and the Completed event handler
12. We press F5 and we are ready!!!!
Download the source here: http://sdrv.ms/XUd0CN
Nice!Let me add that for users of the express version of Visual Studio,since you can't add service references from the IDE,you will have to use the svcutil tool.How?The screenshot showing the service page explains that ;) .
oreos filaraki!
Can anybody help me please....i am stuck with this......getting this error:
"There was no endpoint listening at http://localhost:50717/MyService.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details." when i run my phone client.
Try to use your PC's name instead of localhost
Το αρχειο δεν υπάρχει στο skydrive, υπάρχει κάπου αλλού?
μόλις διόρθωσα το link ;)
Δημήτρη, αν θες στη λίστα που θα εμφανίσεις, να κάνεις tap και να πας σε μια detail page στην οποία υποτίθεται θα βλέπεις περισσότερα πεδία, πως μεταφερεις τα πεδία στην άλλη σελίδα?
θα πρέπει να κάνεις
1. handle το selectedItemChanged στο listbox
2. να πάρεις το ID του selected item
3. να κάνεις navigate σε μια detail σελίδα, περνώντας το ID
4. από την detail σελίδα, να πάρεις το ID και να το στείλεις σε ένα νέο service που θα φτιάξεις (ονόματι π.χ. GetCustomer) ώστε να πάρεις πίσω τον επιλεγμένο πελάτη και να τον εμφανίσεις
To listbox εχει μεσα 3 textblocks. Ενα για το customerid (κρυφό) ένα για το όνομα και ένα για την διεύθυνση.
Πως θα πάρω το customerid για να το στείλω στην δευτερη σελίδα?
Do I have to keep service running always on, when I want get values from database? Just did this and it works, so thank you :)
Can't see any of the images :S can you fix this asap?
Does anyone know about the freehosting?
I've tried doing your tutorial for my windows phone app and have it all working except the last stage just returns
"Timesheet_System.Servicereference.TimeData"
<The phone project>< service reference> <SQL table name>
Any ideas?
Very Helpfull!
Sky Drive is saying the source files are no longer available? Anyone else have them?