560 likes | 849 Views
? K.M. 06/03/2012. University of Greenwich. 2. Data Binding. Data binding in .NET is much improved on previous Microsoft implementationsUse data binding to make a link between the user interface and the data modelCreate a C
E N D
1. © K.M. 06/03/2012 University of Greenwich 1 More Visual StudioData Binding and Web Services Kevin McManus
2. © K.M. 06/03/2012 University of Greenwich 2 Data Binding Data binding in .NET is much improved on previous Microsoft implementations
Use data binding to make a link between the user interface and the data model
Create a C# Web Site project
Bind a property and a method to GUI components
DataBind()
3. © K.M. 06/03/2012 University of Greenwich 3 Data Binding
4. © K.M. 06/03/2012 University of Greenwich 4 Data Binding
5. © K.M. 06/03/2012 University of Greenwich 5 Data Binding
6. © K.M. 06/03/2012 University of Greenwich 6 Data Binding
7. © K.M. 06/03/2012 University of Greenwich 7 Data Binding
8. © K.M. 06/03/2012 University of Greenwich 8 Data Binding
9. © K.M. 06/03/2012 University of Greenwich 9 Connecting to Databases Visual Studio integrates with SQL Server
edit SQL Server data directly in VS.NET
execute SQL statements
VS Server Explorer provides easy connection to databases
View -> Server Explorer
Data binding links controls on the user interface to data stored in SQL Server
now largely automated in VS05
10. © K.M. 06/03/2012 University of Greenwich 10 Binding to a Datagrid Create a new C# Web Application project
give it a meaningful name
give the web form a meaningful name
Use the Server Explorer to locate your SQL Server database
sql-server.cms.gre.ac.uk
11. © K.M. 06/03/2012 University of Greenwich 11 Binding to a Datagrid Create a new C# Web Application project
give it a meaningful name
give the web form a meaningful name
Use the Server Explorer to locate your SQL Server database
sql-server.cms.gre.ac.uk
12. © K.M. 06/03/2012 University of Greenwich 12 Binding to a Datagrid
13. © K.M. 06/03/2012 University of Greenwich 13 Binding to a Datagrid
14. © K.M. 06/03/2012 University of Greenwich 14 Telephone Directory In ye olden days you had to do a lot of this stuff manually
dive in and data bind stuff
All now tooled up and automated
Repeat the exercise as a Web Service
Right click the project in the solution explorer and add a web service
rename it phoneBook.asmx
15. © K.M. 06/03/2012 University of Greenwich 15 Telephone Directory Web Service
16. © K.M. 06/03/2012 University of Greenwich 16 Telephone Directory Web Service
17. © K.M. 06/03/2012 University of Greenwich 17
18. © K.M. 06/03/2012 University of Greenwich 18 .NET XML Containers Three XML document stores each with different features
XmlDocument
the primary document store
supports W3C standards for XML document (DOM), plus useful additional features that make it easier to access the contents
XmlDataDocument
provides a bridge between XML and relational data by allowing the content to be accessed as a DataSet instance as well as a standard XML document
XPathDocument.
fast and efficient document store designed to be accessed only using XPath methods and the XPathNavigator
XPathNavigator can be used over any of the three document stores
19. © K.M. 06/03/2012 University of Greenwich 19 Telephone Directory Web Service Re-write the web service to return an XmlDocument
instead of a data set
Convert the data set read from SQL Server into a string
Load the string into an XmlDocument
20. Telephone Directory Web Service
21. © K.M. 06/03/2012 University of Greenwich 21
22. © K.M. 06/03/2012 University of Greenwich 22 Telephone Directory Web Service Microsoft Access database uses an OLEDB JET connection string
System.Data.OleDb
old fashioned
insecure
popular
Next example uses DOM to read data from Access in XML format
23. © K.M. 06/03/2012 University of Greenwich 23 Telephone Directory Web Service
25. © K.M. 06/03/2012 University of Greenwich 25
26. © K.M. 06/03/2012 University of Greenwich 26 Telephone Directory Web Service Create a .NET web service that accesses XML from a URL
That URL could itself be a web service
getDirectory.php reads a staff telephone directory from a MySQL database
returns a DOM object
is a RESTful web service
uses the PHP5 DOM model
stuweb.cms.gre.ac.uk
28. © K.M. 06/03/2012 University of Greenwich 28
29. © K.M. 06/03/2012 University of Greenwich 29 Telephone Directory Web Service
30. © K.M. 06/03/2012 University of Greenwich 30 Telephone Directory Web Service
31. © K.M. 06/03/2012 University of Greenwich 31 Telephone Directory Web Service
32. © K.M. 06/03/2012 University of Greenwich 32 XML Aggregation Web services are used to create interfaces to data resources
Using DOM structures we can operate on the data
Data from many sources can be aggregated into a single DOM object
.NET provides alternative programmatic approaches to XML
DataSet
XmlNavigator
XSLT
33. XML Aggregation
34. © K.M. 06/03/2012 University of Greenwich 34 XML Aggregation
35. © K.M. 06/03/2012 University of Greenwich 35 XML Aggregation This does the trick
Although we could always do it the hard way
which would allow us to adjust the DOM structure as we migrate nodes
36. © K.M. 06/03/2012 University of Greenwich 36 XML Aggregation
38. © K.M. 06/03/2012 University of Greenwich 38 XML Aggregation Of course we could try a different approach using data sets instead of XML documents
Data sets provide a useful merge method
although this will complain if data types do not match
e.g. are telephone numbers strings?
converting everything to strings is one work around
39. © K.M. 06/03/2012 University of Greenwich 39
40. © K.M. 06/03/2012 University of Greenwich 40
41. © K.M. 06/03/2012 University of Greenwich 41
42. © K.M. 06/03/2012 University of Greenwich 42
43. © K.M. 06/03/2012 University of Greenwich 43 Adding A Processing Instruction
44. © K.M. 06/03/2012 University of Greenwich 44
45. © K.M. 06/03/2012 University of Greenwich 45 Adding Processing Instruction It is not entirely surprising that the processing instruction is filtered out from a web service
Add a Generic Handler to the project
.ashx
Add code to aggregate XML and add a processing instruction into the page load event
Return all of the XML using the Response object
Don’t forget to also use the Response object to send out the correct mime type
text/xml
46. © K.M. 06/03/2012 University of Greenwich 46 Adding Processing Instruction
47. © K.M. 06/03/2012 University of Greenwich 47 Adding Processing Instruction
48. © K.M. 06/03/2012 University of Greenwich 48 Adding Processing Instruction
49. © K.M. 06/03/2012 University of Greenwich 49 Adding Processing Instruction Try the same trick in PHP…
Add one extra line to getDirectory.php
The nice thing about DOM is the way the code works pretty much the same in both PHP and C#
Add the same DOM code into PHP and aggregate some XML
50. © K.M. 06/03/2012 University of Greenwich 50 Adding Processing Instruction
51. © K.M. 06/03/2012 University of Greenwich 51 Server Side XSLT Processing Not all browsers support XSLT processing
Perhaps the XSLT processing is better handled at the server
add another Generic Handler
52. © K.M. 06/03/2012 University of Greenwich 52 Server Side XSLT Processing
53. © K.M. 06/03/2012 University of Greenwich 53 SQL Insert Web Service So far we have been retrieving information from various databases – often we want to insert or modify.
Visual Studio makes it easy to create a suitable web service
54. © K.M. 06/03/2012 University of Greenwich 54 SQL Insert Web Service
55. © K.M. 06/03/2012 University of Greenwich 55 SQL Insert Web Service
56. © K.M. 06/03/2012 University of Greenwich 56 Conclusions It's all so easy
especially when it's RESTful
Visual Studio does most of the work
wish we had VS for PHP
PHPeclipse, Netbeans
These examples show main points
coding would benefit from try catch constructs
57. © K.M. 06/03/2012 University of Greenwich 57 Questions?