Data-Aware Controls in ASP.NET ® Applications ASP.NET.

Slides:



Advertisements
Similar presentations
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

Chapter 9 Customizing Data with Web Controls. ASP.NET 2.0, Third Edition2.
ASP.NET Data Binding. Slide 2 Lecture Overview Understanding the ASP.NET data binding model.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Introduction to ASP.NET, Second Edition2 Chapter Objectives.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
1 Pertemuan 09 Database Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Database-Driven Web Sites, Second Edition1 Chapter 2 INTRODUCTION TO HTML.
Introduction To Form Builder
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Creating Web Page Forms
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
8 Copyright © 2004, Oracle. All rights reserved. Creating LOVs and Editors.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Chapter 9 Using the SqlDataSource Control. References aspx.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Databases and LINQ Visual Basic 2010 How to Program 1.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
Chapter 10 Managing Data with ASP.NET. ASP.NET 2.0, Third Edition2.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
MSIS 5133 Advanced MIS - E-Commerce Spring 2003 Lecture 4: DotNet Technologies - Part 4 Using Controls with Databases Dr. Rathindra Sarathy.
Chapter 81 Building Data-Driven Web Applications Introduction to ASP.NET By Kathleen Kalata.
Chapter 71 Building Data-Driven ASP.NET Applications Introduction to ASP.NET By Kathleen Kalata.
Web Development Using ASP.NET CA – 240 Kashif Jalal Welcome to week – 4-1 of…
ASP.NET Web Server Controls Basic Web Server Controls.
1 ASP.NET ASP.NET Rina Zviel-Girshin Lecture 4. 2 Overview Data Binding Data Providers Data Connection Data Manipulations.
CIS 451: Using ASP.NET Objects with SQL Dr. Ralph D. Westfall February, 2009.
LiveCycle Data Services Introduction Part 2. Part 2? This is the second in our series on LiveCycle Data Services. If you missed our first presentation,
Tutorial 121 Creating a New Web Forms Page You will find that creating Web Forms is similar to creating traditional Windows applications in Visual Basic.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Tutorial 7 Creating Forms. Objectives Session 7.1 – Create an HTML form – Insert fields for text – Add labels for form elements – Create radio buttons.
Delivering Excellence in Software Engineering ® EPAM Systems. All rights reserved. Data Binding in ASP.NET.
ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
1 Chapter 12 – Web Applications 12.1 Programming for the Web, Part I 12.2 Programming for the Web, Part II 12.3 Using Databases in Web Programs.
Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET.
Chapter 5: Windows and Frames
4 Chapter Four Introduction to HTML. 4 Chapter Objectives Learn basic HTML commands Discover how to display graphic image objects in Web pages Create.
Chapter 8 Working With Databases in ASP.NET. Listing 8.1 – ShowListControls Uses The SqlDataSource control for estabishing database connectivity and.
ASP.NET OPTIMIZATION. Why Optimize? $$$ Whether you build applications for customers or not, enhanced applications save money.
Tutorial 91 Databases A database is an organized collection of related information stored in a file on a disk A database allows companies to store information.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
TRAINING SESSIONS.NET Controls.  Standard Controls  Label  Textbox  Checkbox  Button, Image Button, Image control  Radio Button  Literal  Hyperlink.
Reference: “ASP.NET 2.0 Illustrated” by Alex Homer and Dave Sussman. -ch3 illustrated book
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
JavaScript, Fourth Edition
BlackBerry Applications using Microsoft Visual Studio and Database Handling.
Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.
Chapter 5 Introduction To Form Builder. Lesson A Objectives  Display Forms Builder forms in a Web browser  Use a data block form to view, insert, update,
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
CHAPTER 7 LESSON C Creating Database Reports. Lesson C Objectives  Display image data in a report  Manually create queries and data links  Create summary.
Visual Basic 2010 How to Program
Unit 9.1 Learning Objectives Data Access in Code
ASP.NET Web Controls.
Using List Controls with SQL Server
PROG Advanced Web Apps 2/24/2019 Session 9.2 Data Binding Wendi Jollymore, ACES.
Chapter 10 Accessing Database Files
Presentation transcript:

Data-Aware Controls in ASP.NET ® Applications ASP.NET

Data Binding Techniques A data source represents data – A simple hash table, an array, or a database (or any other collection) – Web controls are used to display the data – Sometimes called Data controls because their primary function is to display data – Displayed in a simple list, drop-down list, or table Data binding is the process of assigning a data source to a Web control – Data can be bound to a variety of Web controls

Data Binding Techniques

SingleBind.aspx

Single-Expression Binding (Page 1) Binds a single value or expression to a control/tag Enclosed within inline server tags, i.e. ( ) Pound/digit symbol (#) before the expression name – Expression is a call to some named procedure, either programmer-defined or built-it (i.e. method or function) – The return value is inserted into the control Example: " runat="server" />

Single-Expression Binding (Page 2) The server tag and expression may be entered into for many property lines in the Properties window for Web Server controls (not HTML controls) When the (DataBindings) property is selected, the "DataBindings" dialog window is displayed – Enter the server tag expression into the Custom Binding Expression: window which names the command to execute The radio button must be selected to enter an expression Call DataBind () method usually from Page_Load event to bind the expression to each object

Assign Property Value Directly

Assign as a Data Binding

Key Entry into HTML View

Repeated-Expression Binding (Page 1) Repeated expressions are bound to data controls (controls that have a DataSource property) – Could be a collection such as a HashTable, or an ArrayList (i.e. Chapter 4) – DataSet, DataTable, DataView, or DataReader from a database also are repeated expressions CheckBoxList, RadioButtonList, DropDownList and ListBox inherit from ListControls class and may have data from any of the datasources above bound to them

Repeated-Expression Binding (Page 2) Data sources also may to bound to the DataGrid and DataList controls which are data controls that inherit from class BaseDataList Repeater inherits from System.Web.UI.Controls class directly

Repeated-Expression Binding

ASP.NET Data Objects DataSet – A cached (off-line) set of records from a database – Contains one or more DataTables DataTable and DataView – Subset of rows of data from a DataSet DataReader – Read-only, forward-only on-line stream of data from a database

Data Binding Web Controls DropDownList—displays one value at a time using the tag; drops-down to display values ListBox—displays values in a box several lines at a time using tag RadioButtonList and CheckBoxList—groups multiple controls Repeater—small, lightweight control that displays data; repeats defined HTML content DataList—displays data from each data row in a list DataGrid—repeats content once for each data row; places the data in a table of rows and columns

The DataSource Property For data-aware controls, names the set of data to be displayed by the control Format: controlName.DataSource = arrayList/hashTable/DataSet/DataView Example: ddProducts.DataSource = arrayList1 If a HashTable is assigned to DataSource property: – "Key" is the first element from the hash table – "Value" is the second element from the hash table

The DataBind Method Binds one or more data-aware controls so that data is linked to and displayed in the control Format: Page/controlName.DataBind – The Page object may be used to bind all objects when there are more than one on the Web Form Examples: ddProducts.DataBind() Page.DataBind() DataBind()

DropDownList.aspx

The DataTextFormatString Property (Page 1) Provides formatting for elements in DataBound controls Format string is in two parts, separated by a colon: {"A:Bxx"} – A specifies the index in a zero-based list of parameters (always 0 because there is only one value per element) – B specifies the format to display (C is currency, D is decimal, F is fixed, etc.) – xx specifies the number of decimal places to display

The DataTextFormatString Property (Page 2) Example: – {0:F2} – Formats element to display a fixed point number with two decimal places – Advisable to always specify decimal positions to make it easier for other programmers to read your code

Other Properties of Bound Controls DataTextField property—text displayed between or associated with the tag block or element DataValueField property—used by the control as the value attribute

Binding Data to DropDownList Control Creates an HTML … block with tags Sets the size attribute (height of the object) in the HTML code sent to the browser as equal to 1 – The value 1 is the default for a dropdown list and will not actually be specified in the HTML Example:

ListBox.aspx

Binding Data to a ListBox Control Creates an HTML … block with tags Sets the size attribute (height of the object) in the HTML code sent to browser to a value greater than 1 Example:

CheckBoxRadio.aspx

Binding Data to the CheckBoxList and RadioButtonList Controls RadioButtonList and CheckBoxList also are data- aware controls – Each item in series (of CheckBoxes or RadioButtons) is created is an HTML tag with the type attribute equal either "checkbox" or "radio" Example:

Binding to a DataGrid Control Creates a grid of rows and columns to display two- dimensional data arrays, i.e. – It might display separate key and value items of a hash table in two columns, each element in its own row – Ideal for displaying records and fields from a database table, view or stored procedure Once the data set exists, set the DataSource property of the DataGrid and call the DataBind () method Example:

DataGridSimple.aspx

The AutoGenerateColumns Property of the DataGrid Control Specifies whether of not columns in the grid are automatically created from the DataSource when the page loads – By default all columns are displayed in a DataGrid since the AutoGenerateColumns properties is set to True – If set to False, the … block must be used with tags to name fields to display from DataSource Required when using a HashTable since DataGrid does not have DataTextField and DataValueField properties

The Control When configuring a data source (i.e. HashTable, DataSet, DataView) programmatically, the DataGrid columns are not created until the code executes The … control block can be added manually to the HTML view to build columns Format: columnElements

The Control (Page 1) Displays only selected items from data source in the columns of a DataGrid or other object One or more controls are embedded in the … block Properties: – DataField—names field for display from data source – HeaderText—text displayed at top of column – DataFormatString—uses the same format syntax as the DataTextFormatString

The Control (Page 2) Example: <asp:BoundColumn DataField="Value" HeaderText="Price" DataFormatString="{0:C}"> <asp:BoundColumn …

Setting DataGrid Formatting Found in the Properties window, separate formatting is available for: – HeaderStyle – ItemStyle – FooterStyle Also may be keyed manually in the HTML view – Be careful to not enter the tag inside a … block Such an error would cause the style of just that one column to be formatted

Binding to a DataList Control Used to create a simple, one-dimensional list An control is required in DataList to identify the columns to display and to format data – Like a loop that formats every line in the DataList or other object – Templates are used to store data binding instructions Example: ItemTemplateInformation

DataList.aspx

Templates (Page 1) Templates – Binds data to individual areas within the control providing the ability to format the control – Combines content, HTML, styles, return values of methods and formatting elements into a single column May be used in a DataGrid to create an HTML stream of characters An ItemTemplate is required for the DataList and Repeater controls – Information will be displayed one time for each row/element in the DataSource, like a loop)

Templates (Page 2) Types: – HeaderTemplate—modifies content and appearance of data in header section (first row) – ItemTemplate—configures data rows; and many other HTML elements may be included as well as data source items – FooterTemplate—configures data in footer section (last row) – SeparatorTemplate—configures elements that appear between each of the items – EditItemTemplate—modifies configuration of columns or rows that currently are being edited

Templates (Page 3) Example: :

The Container.DataItem Function A server expression used in a template to identify the columns to bind to a control Retrieves and stores values, and places them into the HTML output stream Format: Container.DataItem("fieldNameString") Examples: – In the second example, the key (first) element from a hash table is the bound data item

The DataBinder.Eval Function A server expression used in a template that, in a single statement, identifies the columns to bind to control along with the formatting instructions Like Container.DataItem function, retrieves values and places them into the HTML output stream Format for data-aware controls: DataBinder.Eval(Container.DataItem, "fieldName", "dataFormatString") Example:

Binding Data to a Repeater Control (Page 1) Used to create tables, comma-delimited lists, bulleted lists, and numbered lists Has no default appearance so it must be configured in HTML view Data can be inserted with: – An HTML tag – ASP.NET templates include header, footer, alternating, item and separator templates To position a Repeater control, use the HTML tag or an ASP.NET Panel control

Repeater.aspx

Binding Data to a Repeater Control (Page 2) Example: ItemTemplateInformation

The ADO Objects There are five (5) objects that must be declared: 1. Connection (OleDbConnection) 2. DataAdaptor (OleDbDataAdaptor) 3. Command (OleDbCommand) 4. DataSet 5. DataTable or DataView

A programmer-defined identifier which defines the connection to a database file Stores connection information (ConnectionString property) including the DBMS type, as well as the path and filename of database Format: Dim objectName As New OleDbConnection Example: Dim conTaraStore As New OleDbConnection – Prefix for OleDbConnection objects is "con" The OleDbConnection Object

A programmer-defined identifier (in this case a reference variable/object) Performs the operations required to communicate between an ASP.NET application and a database file Format: Dim objectName As New OleDbDataAdaptor Example: Dim daProducts As New OleDbDataAdaptor – Prefix for OleDbDataAdaptor objects is "da" The OleDbDataAdaptor Object

The OleDbCommand Object A programmer-defined identifier which stores the SQL command information that must execute to establish the database connectivity Stores a command (CommandText property) that may returns specific records – Or store commands to insert, update or delete rows Format: Dim objectName As New OleDbCommand Example: Dim cmdProducts As New OleDbCommand – Prefix for OleDbCommand objects is "cmd"

The DataSet Object Programmer-defined identifier (reference variable) which stores the collection of tables … – The object may store more than one table returned by the OleDbDataAdaptor object Tables literally are stored off-line in RAM Format: Dim/Private objectName As New DataSet Example: Dim dsProducts As New DataSet – Prefix for DataSet objects is "ds"

Programmer-defined identifier which stores a reference to a single table from a DataSet that is stored in RAM The data then is manipulated off-line – The physical database on disk may be updated after the processing of the data is completed The DataTable object points to one of the tables from the DataSet object The DataTable Object (Page 1)

The DataTable Object (Page 2) Format: Dim/Private objectName As DataTable Example : Dim dtProducts As DataTable – The reserved word New is not used because the object variable is a pointer (points to an address in RAM) – Prefix for DataTable objects is "dt"

Importing a Namespace (Page 1) Use of the keyword Imports lets developer reference a namespace element from the.NET framework without fully qualifying the element Statement must appear at the top of a module before the class header ("Public Class … ") usually after the Option Explicit and Option Strict statements

Importing a Namespace (Page 2) Format: Imports Namespace.Namespace.etc. If "OleDbConnection" class is in System.Data.OleDb namespace the following Imports statement lets the class be called directly: Imports System.Data.OleDb … Dim conTaraStore As New OleDbConnection Instead of: Dim conTaraStore as New System.Data.OleDb.OleDbConnection

Linking the Objects (Page 1) There are six (6) steps necessary to create the links between the objects and access the database: 1. Assign a string to the ConnectionString property of the OleDbConnection object: 2.Assign a SQL string to the CommandText property of the OleDbCommand object 3. Assign the Connection object as the Connection property of the Command object 4. Assign Command object as the SelectCommand property of the DataAdaptor object

Linking the Objects (Page 2) The six (6) steps necessary to create links between the objects and access the database (con.): 5. Call the Fill method of the DataAdaptor object with the DataSet object as its argument 6. Assign the Tables property of the DataSet object to the DataTable object with the first returned table

The ConnectionString Property (Page 1) For the OleDbConnection object, builds a String which establishes the filename and other connection arguments to the data source The information items is in the form of key=value pairs each separated by a semicolon (;) The Provider string specifies the DBMS type, i.e. "Provider=Microsoft.Jet.OLEDB.4.0" The Source string specifies path (location) to the database and its filename, i.e. "Data Source=c:\TaraStore.mdb"

Format: ConnectionObjectName.ConnectionString = "ProviderString;SourceString" Example: mconPayables.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\TaraStore.mdb" The ConnectionString Property (Page 2)

The CommandText Property For the OleDbCommand object, a String that defines the SQL SELECT, INSERT, UPDATE, or DELETE command Format: CommandObjectName.CommandText = "SQLCommandString"/StringVariable Example: pcmdVendor.CommandText = "SELECT * FROM Products"

The Connection Property For the OleDbCommand object, stores information about the DBMS to which the command references Effectively creates the link between the Command object and the Connection object Format: CommandObjectName.Connection = ConnectionObjectName Example: cmdProducts.Connection = conTaraStore

The SelectCommand Property For the OleDbDataAdaptor object, stores SQL SELECT command information to be executed by the DBMS Effectively creates a link between the DataAdaptor object and the Command object Format: DataAdaptorObjectName.SelectCommand = CommandObjectName Example: daProducts.SelectCommand = cmdProducts

The Fill Method A method of OleDbDataAdapter objects which: – Opens the connection to the data provider (DBMS) – Sends the SQL SELECT command – After the data provider executes the command, populates DataSet with table data from database Format: DataAdapterObjectName.Fill (DataSetObjectName) Example: daProducts.Fill(dsProducts)

The Tables Collection (Page 1) A member of the DataSet object representing the one or more tables assigned to it A collection is an array of properties for an individual object The Tables collection is the set of tables that is stored in the DataSet object and may include one or more tables

The Tables Collection (Page 2) Format: DataSetObjectName.Tables(index) Example: mdtVendor = mdsVendor.Tables(0) – Here one table from the DataSet collection is assigned to a DataTable object – The index "0" is the first table from the DataSet (points to a table)

TaraStore.mdb—The Products Table

DataGridDisplay.aspx

Common Database Error Messages ASP.NET Machine account – MachineName\ASPNET – Used by the Web server to run ASP.NET pages – Does not have NTFS permissions to manage the database – Go to Windows Explorer to the file – Set NTFS permissions to Modify

Common Database Error Messages (continued)

Microsoft Access Database Connection Error Messages Permission to the directory that contains the file – Creates a Microsoft Access LDB file, i.e. Databasename.ldb Record locking and other settings can be set within Access

Microsoft Access Database Connection Error Messages (continued)

Modifying Data Columns in the DataGrid Control (Page 1) By default all columns are displayed in a DataGrid since the AutoGenerateColumns properties is set to True When configuring a data source (DataSet, DataTable, or DataView) programmatically, the DataGrid columns are not created until the code executes

Modifying Data Columns in the DataGrid Control (Page 2) The … control block can be used to add columns manually to the HTML view – Set DataField property for each to fieldname from the table – Modify HeaderText property for each column to vary it from default string which is fieldname Example: <asp:BoundColumn …

TaraStore.mdb—The Categories Table

DisplayGridColumns.aspx

The Control (Page 1) Columns have a header, footer, and item section When the control is embedded in an, developer may format that column for every row in the table by setting properties and values using IntelliSense Inserted into either: – The … block format all columns in all rows (but not headers and footers) – The … block to format individual columns

The Control (Page 2) Example:

The Property Builder for the DataGrid (Page 1) Style template controls (,, etc.) may be inserted into the HTML code for a DataGrid using the Property Builder If data source configured programmatically, remember to first create a … block with controls in the HTML view

The Property Builder for the DataGrid (Page 2) Right click on the DataGrid and select Property Builder… from the shortcut menu Select the Columns tab to add columns to and/or modify existing columns in the DataGrid control – Modify each column’s HeaderText, DataField, and other attributes here – A control should appear in the Select columns: list for each column

The Property Builder for the DataGrid (Page 3) Select the Format tab to modify the appearance of template items, including: – Header and Footer (top and bottom of grid), and Pager (controls that link to previous and next pages in DataGrid) – Items—adds formatting for rows within the … block including Normal (formats control for all columns) – Columns—drill down clicking the plus (+) in each column to reach its Header and Footer (for each column) or Items (the cells) elements Select the column name to modify its width attribute

DisplayGridColumns.aspx— Property Builder Properties (Page 1) Columns tab – CategoryName—HeaderText:"Name" – CategoryID—HeaderText:"Category ID"

DisplayGridColumns.aspx— Property Builder Properties (Page 2) Format tab – Header BackColor:DarkSlateGrey Bold:True Font name:Trebuchet MS ForeColor:White Horizontal alignment:Center – Items—Normal Items Font name:Trebuchet MS Font size:X-Small – Columns—Columns(0)—Items Horizontal align: Center

Data Columns All of the following are column types that may be inserted within a … block to assign columns to a DataGrid: – BoundColumn—binds the column to one field from a data source – TemplateColumn—customized columns; can be used for formatting how entries appear both while both displaying and editing data in a DataGrid control – HyperLinkColumn—displays and creates a hyperlink (may be configured in the Property Builder)

The HyperLinkColumn In place of NavigateURL, the following attributes may be used to pass a query string to a called page – DataNavigateUrlFormatString attribute —the format of a URL with a query string that may store a variable value – DataNavigateUrlField attribute —stores a value which can be passed along with the key as part of the query string Example:

DataGridDetails.aspx

Column Styles These templates may be modified manually in the HTML view, or by using the Property Builder – HeaderStyle—formats the HeaderTemplate – FooterStyle—formats the FooterTemplate – ItemStyle—formats individual columns in every row – AlternatingItemStyle—formats every other row – SelectedItemStyle—formats currently selected row – EditItemStyle—formats the row in edit mode – PagerStyle—formats the page navigation controls PageSize—stores the number of rows number displayed

RepeaterTemplate.aspx

Additional Properties for the DataList Control RepeatColumns property—an integer which specifies the number of columns that may be displayed in the DataList for each of the elements RepeatDirections property—direction of repetition for the RepeatColumns property in columns within the grid – RepeatDirection.Horizontal – RepeatDirection.Vertical

DataListTemplate.aspx