Work with data in ASP.NET

3,031 views

Published on

Goal: Bringing data to the web and back :-)
Old Style
New Style
DataBinding
DataGrid
Input Validation

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,031
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
65
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Click to add notesPeter Gfader Developing Windows and Web applications
  • Request ResponseOpen browser and browse around
  • Goal: Show View data to users on the webLet users enter and modify data
  • Show how easy it isHow many code lines did i write?Show them the old wayHuge difference!
  • Changes to Datasource get forwarded to databaseChanges from database get forwarded to data grid
  • Property DataSource and DataMember on grid
  • Show controls in VSDataList = More for List layout (http://msdn.microsoft.com/en-us/magazine/cc301328.aspx )DetailsView = mainly for editing (row/col switched) 1 record at a timeFormView = like detailsview but more control over html rendered code (layout) that’s goodListView = like datalist but more control again, see EA emp Repeater = same as Listview, Listview replaces repeaterDropDownListListBox = simple list of itemsCheckBoxListRadioButtonList
  • Go to website: telerikTelerik RAD ControlsComponent ArtInfragisticsDevexpressJanus
  • Suggest or common ones are querystring, hidden fieldCookies saving bits of info for all pagesE.g. Select a row from the grid and show the details in a DetailsViewPass the selected row’s ID (e.g. ContactID) as a query string parameterE.g. http://localhost:1234/MyWebApp/Details.aspx?ContactID=1
  • Show url with IDPostback http://www.search-cube.com/
  • Problem Exists Between Chair and ComputerClient database SWIFT project
  • ASP.NET existing Validation controls create client side script for us
  • CustomValidator Method Validate write your own code there (SERVER SIDE)
  • Work with data in ASP.NET

    1. 1. Developing Windows and Web Applications using Visual Studio.NET<br />Eric Phan<br />Solution Architect<br />
    2. 2. Homework?<br /><ul><li>Address Control
    3. 3. Credit Card Validation</li></li></ul><li>http://sharepoint.ssw.com.au/Training/UTSNET/<br />Part 2: .NET Webforms<br />Overview of .NET Webforms<br />Data in Webforms TODAY<br />Usability<br />Rich Web Forms and <br />Other ASP.NET Features <br />Web Security <br />Advanced Topics & Future Technology (Silverlight)<br />Webforms<br />
    4. 4. Recap<br />
    5. 5. 1<br />2<br />
    6. 6.
    7. 7. Session 7: Data in the Web<br /><ul><li>Goal: Bringing data to the weband back :-)
    8. 8. Old Style
    9. 9. New Style
    10. 10. DataBinding
    11. 11. DataGrid
    12. 12. Input Validation</li></li></ul><li>Data in our database<br />
    13. 13. Data<br />
    14. 14. Bring database to the web<br />Show/view<br />Data<br />Enter<br />
    15. 15. OLD way<br />
    16. 16. What is ADO.NET? <br />Set of classes that expose data access services to the .NET programmer<br />ADO.NET provides a rich set of components for creating distributed, data-sharing applications<br />It is an integral part of the .NET Framework, providing access to relational data, XML, and application data<br />
    17. 17. WebForm Apps<br />WinForm Apps<br />ADO.NET<br />DataSets<br />SQLConnection<br />SQLCommand<br />SQLDataAdapter<br />SQLDataReader<br />SQL Data Providers<br />SQL Data<br />XML Data<br />Oracle Data<br />…<br />
    18. 18. Objects<br />Instead of DataSets we use classes<br />Extensible<br />Can embed logic and validation<br />A more OO approach<br />
    19. 19. WebForm Apps<br />WinForm Apps<br />Objects/Entities<br />Data Sources<br />SQL Data<br />XML Data<br />Oracle Data<br />…<br />
    20. 20. DataSets or Business Objects<br />Why use DataSets<br />A highly functional container for data that can be strongly typed<br />Automatic change tracking for contained data<br />Seamless data-binding support<br />Why Use Objects<br />Business Objects (NHibernate, NTiers, LINQ to SQL, LINQ to Entities)<br />Light weight<br />Good data-binding support<br />Extensible and OO<br />Very easy to use<br />No SQL command Strings<br />
    21. 21. The new way!LINQ 2 EntitiesLINQ 2 SQL<br />
    22. 22. LINQ to SQL/Entities<br />Brand new in the .NET 3.5 Framework<br />Easily access and query your database through classes<br />Logical access to class hierarchies<br />Supports Inheritance<br />Intuitive to use<br />
    23. 23. DBML File<br />Database Mapping Language<br />Contains a mapping of classes to database tables<br />Has built in logic to handle selection, updates and deletes<br />Can be customized<br />www.hookedonlinq.com<br />
    24. 24. var customers = from c in db.Customers<br /> where c.FirstName.StartsWith(“A”) select c<br />SQL Syntax<br />
    25. 25. var customers = db.Customers.Where(c => c.FirstName.StartsWith(“A”);<br />Lambda Syntax<br />
    26. 26. Challenge, use this instead of SQL Server Management Studio for a month<br />LINQPad<br />
    27. 27.
    28. 28. See our Rules<br />http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterLINQ.aspx<br />
    29. 29. Databinding<br />
    30. 30. Bind the data<br />Data<br />Data source<br />
    31. 31. Data Binding<br />Data<br />Data source<br />From MSDN: Data binding is the process that establishes connection between the application UI and business logic<br />Binding controls to data from database<br />Bind a grid on Webform to a database table<br />Simple, convenient, and powerful way to create a read/write link between the controls on a form and the data in their application<br />
    32. 32. Databinding the GridView (code)<br />C#<br />NorthwindDataContext db = newNorthwindDataContext();<br />GridView1.DataSource = db.Customers;<br />GridView1.DataBind();<br />English <br />Instantiate our data access class<br />Make the datasource of GridView1 all the customers from our database<br />Bind the Grid<br />
    33. 33. Databinding the GridView (designer)<br />
    34. 34. Databinding the GridView (designer)<br />
    35. 35. Databinding the GridView (designer)<br />
    36. 36. Databinding the GridView (designer)<br />
    37. 37. Databinding the GridView (designer)<br />
    38. 38. Databinding the GridView (running)<br />
    39. 39. GridView<br /><ul><li>Select sort and edit
    40. 40. Display a Table from a Database
    41. 41. Easy to configure
    42. 42. Without writing any code at all</li></li></ul><li>GridView<br />Lots of built in features like:<br />Sorting<br />Paging<br />Inserting<br />Updating <br />Deleting<br />Selecting<br />
    43. 43. Enabling these features<br />Enable Delete, Insert, Update on the DataSource<br />
    44. 44. Enabling these features<br />Enable them on the GridView<br />
    45. 45. Easy!<br />
    46. 46. Other Data Bindable Web Controls<br />Bindable to Lists <br />DataList<br />DetailsView<br />FormView<br />Repeater/ListView<br />DropDownList<br />ListBox<br />CheckBoxList<br />RadioButtonList<br />Bindable to Properties/Columns<br />Most other controls<br />
    47. 47. Data-Bound Controls<br />Most controls in .NET Framework support data binding<br />Controls inherit from the WebControl base classwhich implements IDataBindingsAccessorinterface<br />Important Data-binding properties <br />DataSource<br />DataMember<br />DisplayMember<br />ValueMember<br />Design your own controls or purchase 3rd party controls<br />
    48. 48. Passing Parameters<br />
    49. 49. Passing Parameters between pages<br />Pass parameters to pages or controls<br />Pass parameters to our data sources<br />Parameters can be obtained from:<br />Query String<br />Session Variable<br />Control Property<br />Hidden Field<br />Cookie<br />
    50. 50. 1. Create a CustomerDetails.aspx <br />Add a Details View and create a new Datasource<br />
    51. 51. 2. Create a LINQ Data Source<br />
    52. 52. 3. Specify the Where clause<br />
    53. 53. 4. Make it read from the Query String<br />
    54. 54. 5. Change the Select on the GridView to Link to the CustomerDetails Page<br />
    55. 55. 6. Edit the templates<br />
    56. 56. 7. Delete Select and Add a Hyperlink<br />
    57. 57. 8. Configure the Hyperlink<br />CustomerDetails.aspx?CustomerID={0}<br />
    58. 58. Run it<br />
    59. 59. Validation – Don’t trust anyone<br />
    60. 60. Validation - Bring database to the web<br />Show/view<br />Data<br />Enter<br />
    61. 61. Validation<br />Why do we need it?<br />PEBCAC<br />Bad Data<br />
    62. 62. Input Validation<br />
    63. 63. Why use Validations?<br />Seamless verification of data fields<br />Check for mandatory fields<br />Check for correct data formats<br />Check for data ranges<br />Reduces incorrect data entered into your database<br />Prevent possible runtime exceptions<br />
    64. 64. Validation Types<br />Client<br />Show/view<br />Data<br />Enter<br />Client<br />Server<br />
    65. 65. Validation Types<br />Client-Side Validation<br />Validations are performed before the form is posted back<br />Quick and responsive for the end user<br />Insecure<br />Server-Side Validation<br />Validation checks are performed on the server<br />Secure because these checks cannot be easily by passed<br />Sometimes can be slow<br />Recommendation<br />Use both the types<br />This approach provides the best of both worlds<br />Client<br />Show/view<br />Data<br />Enter<br />Client<br />Server<br />
    66. 66. ASP.NET Validation Server Controls<br />RequiredFieldValidator<br />CompareValidator<br />RangeValidator<br />RegularExpressionValidator<br />CustomValidator<br />ValidationSummary<br />
    67. 67. To put the Northwind database up on the web?<br />CRUD for all tables<br />Pages for all tables<br />Validation <br />So how long do you think it would take...<br />
    68. 68. 1 line of code<br />5 Minutes<br />
    69. 69. Awesome starting point<br />Learn more from http://www.asp.net/dynamicdata/<br />ASP.NET Dynamic Data<br />
    70. 70. Extend what’s given to you and you can make really great websites<br />You can use it as a framework<br />
    71. 71.
    72. 72. Any issues ... questions<br />PeterGfader@ssw.com.au<br />EricPhan@ssw.com.au<br />
    73. 73. Thank You!<br />Gateway Court Suite 10 81 - 91 Military Road Neutral Bay, Sydney NSW 2089 AUSTRALIA <br />ABN: 21 069 371 900 <br />Phone: + 61 2 9953 3000 Fax: + 61 2 9953 3105 <br />info@ssw.com.auwww.ssw.com.au <br />
    74. 74. Resources<br />www.hookedonlinq.com<br />http://www.asp.net/get-started/<br />http://weblogs.asp.net/scottgu/<br />http://www.asp.net/dynamicdata/<br />If you have the next awesome web ideahttp://www.microsoft.com/BizSpark/<br />http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterLINQ.aspx<br />

    ×