More Related Content
Similar to Vb.net session 06
Similar to Vb.net session 06 (20)
Vb.net session 06
- 1. Accessing and Manipulating Data
Pre-Assessment Questions
1. Which of the following is the fundamental format for data transfer in
ADO.NET. ?
• HTML
• .doc
• .xls
• XML
• A data provider, which is used to work specifically with Microsoft SQL Server.
a. OLE DB data provider
b. SQL Server data provider
c. Oracle data provider
d. ODBC data provider
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 1 of 24
ADO.NET
- 2. Accessing and Manipulating Data
Pre-Assessment Questions (Contd.)
1. Which of the following retrieves data from a database into a dataset?
• Data Adapter
• Data Reader
• Data Command
• Data Connection
2. Which of the following is not a component of DataTable?
• DataRowCollection
• DataView
• DataRelation
• DataColumnCollection
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 2 of 24
ADO.NET
- 3. Accessing and Manipulating Data
Pre-Assessment Questions (Contd.)
1. Which of the following is used to navigate to the next row of data in case the
command execution returns multiple rows of data?
a. Read method
b. Close method
c. Open method
d. NextResult method
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 3 of 24
ADO.NET
- 4. Accessing and Manipulating Data
Objectives
In this lesson, you will learn to:
• Implement simple data binding
• Implement complex data binding
• Filter and sort Data
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 4 of 24
ADO.NET
- 5. Accessing and Manipulating Data
Solutions to Pre-Assessment
Questions
1. d. XML
2. b. SQL Server data provider
3. a. Data Adapter
4. c. DataRelation
5. d. NextResult method
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 5 of 24
ADO.NET
- 6. Accessing and Manipulating Data
Introducing Data Binding
• Data binding is binding data to a control on a Windows Form to be displayed in
a customized format.
• Data binding is of two types :
• Simple data binding
• Simple data binding is process of binding a control, such as a text
box or a label, to a value in a dataset.
• The dataset value can be bound to the control by using the properties
of the control.
• Complex data binding
• Complex binding is the process of binding a component, such as a
DataGrid or a ListBox control, to display multiple values for a
column from the dataset rows.
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 6 of 24
ADO.NET
- 7. Accessing and Manipulating Data
Complex Data Binding Control
Control Description
ListBox This control is used to display data for a column from
multiple records of a dataset. The DataSource
property of the ListBox control is used to set the
dataset, and the DisplayMember property of the
ListBox control is used to bind the control to a
specific column of a table in the dataset.
ComboBox This control has two parts, a text box for entering
data and a drop-down list for displaying the data. The
DataSource property of the ComboBox control is
used to set the dataset, and the DisplayMember
property of the ComboBox control is used to bind the
control to a specific column of a table in the dataset.
DataGrid This control can display data from multiple records as
well as multiple columns. The DataSource property
of the DataGrid control is used to set the dataset,
and the DataMember property of the DataGrid
control is used to bind the control to a specific table
in the dataset.
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 7 of 24
ADO.NET
- 8. Accessing and Manipulating Data
Navigating between Records
• For every data source that is bound to a Windows Form control, there
exists a CurrencyManager object.
• The CurrencyManager object handles the binding to the data source by
keeping a pointer to the current item in the record list.
• The CurrencyManager class is derived from the BindingManagerBase class.
• The figure given below illustrates the relationship between the
BindingContext class, CurrencyManager class, and a Windows Form:
Windows Form
Control 1
Control 2
Control 3
CurrencyManager 1 DATA SOURCE 1
BindingContext
object CurrencyManager 2 DATA SOURCE 2
CurrencyManager 3 DATA SOURCE 3
The BindingContext Class, CurrencyManager Class, and Windows Form
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 8 of 24
ADO.NET
- 9. Accessing and Manipulating Data
Demo
Implementing Simple Data Binding
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 9 of 24
ADO.NET
- 10. Accessing and Manipulating Data
Problem Statement
• An application needs to be created that allows the Marketing Manager of Diaz
Telecommunications to view customer details since the new product details are
to be sent to all the customers. In addition, the Marketing Manager should be
able to navigate through the customer details. The customer details are
located in the database at a remote call center.
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 10 of 24
ADO.NET
- 11. Accessing and Manipulating Data
Solution
• To design the application, you need to perform the following steps:
1. Design a Windows Form to display the data
2. Connect to the database
3. Write the code for navigating through the data
4. Bind the data to a Windows Form control
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 11 of 24
ADO.NET
- 12. Accessing and Manipulating Data
Demo
Implementing Complex Data Binding
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 12 of 24
ADO.NET
- 13. Accessing and Manipulating Data
Problem Statement
• An application needs to be created that allows the Marketing Manager of Diaz
Telecommunications to view the customer details in a customized format
since the new product details are to be sent to all the customers. The
customer details are located in the database at a remote call center.
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 13 of 24
ADO.NET
- 14. Accessing and Manipulating Data
Solution
• To design the application, you need to perform the following steps:
1. Design a Windows Form to display the data
2. Connect to the database
3. Bind the data to a Windows Form control
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 14 of 24
ADO.NET
- 15. Accessing and Manipulating Data
Filtering and Sorting Data
• You filter the data so as to display only the desired records.
• You can sort the data to display it in ascending or descending order of a
particular column.
• There are two methods for displaying filtered data:
• Creating parameterized queries
• Filtering a dataset
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 15 of 24
ADO.NET
- 16. Accessing and Manipulating Data
Creating Parameterized Queries
• By using parameterized queries, data can be filtered based on the criterion
entered by a user at run time.
• Parameterized queries are created while configuring data adapters.
• For example, when configuring an OLEDB data adapter through the wizard,
you can specify the parameterized query for displaying data based on the
value stored in the state column, as follows:
SELECT id, lname, state
FROM authordetails
WHERE (state =?)
• For an SQL data adapter, the syntax for the above given parameterized
query will be as follows:
SELECT id, lname, state
FROM authordetails
WHERE (state =@param)
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 16 of 24
ADO.NET
- 17. Accessing and Manipulating Data
Filtering a Dataset
• There are two options for filtering the data in a dataset:
• Using the Select() method
• Filtering a data view
• Using the Select() Method
• When the Select() method is used to filter data, the order of the
records in the table does not change based on the parameter passed.
Only the data is presented in a filtered format.
• To use the Select() method, an object of the DataTable class has to be
created that will call the Select() method.
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 17 of 24
ADO.NET
- 18. Accessing and Manipulating Data
Filtering a Data View
• A DataView object creates a fixed customized view of a given DataTable
object.
• By default, every table in a dataset has a data view attached to it.
• You can also explicitly create the DataView objects in applications.
• You can bind the controls to the created data view for displaying data
according to your needs.
• A parameterized class is a collection of classes that have common parameters
and is represented by a template.
FILTER2
DataView1
Windows Data Table
Application
DataView2
DataView3
Filter1
Displaying Customized Data Through Data Views
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 18 of 24
ADO.NET
- 19. Accessing and Manipulating Data
Filtering a Data View (Contd.)
• The generic steps to add a data view to a Windows Form are as follows:
• Drag the DataView control from the Data tab of the Toolbox to the
Windows Form.
• Set the properties for the data view by using the Properties window. All
the properties can be set at design time as well as at run time.
• Data view is preferable since you can bind controls to the DataView
object in the Form Designer.
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 19 of 24
ADO.NET
- 20. Accessing and Manipulating Data
Demo
Implementing Filtering and Sorting
Data
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 20 of 24
ADO.NET
- 21. Accessing and Manipulating Data
Problem Statement
• An application needs to be created for the Marketing Manager of Diaz
Telecommunications to view the order details for a specific invoice to verify
the monthly sales report.
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 21 of 24
ADO.NET
- 22. Accessing and Manipulating Data
Solution
• To design the application, you need to perform the following steps:
1. Design a Windows Form to display the data
2. Connect to the database, and filter and sort the data
3. Bind the data to a Windows Form control
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 22 of 24
ADO.NET
- 23. Accessing and Manipulating Data
Summary
In this lesson, you learned that:
• After the data is retrieved from the database, it has to be bound to a
Windows Form control to be displayed in a customized format.
• Data binding is of two types:
• Simple data binding
• Complex data binding
• Using parameterized queries, the data can be filtered based on the criterion
entered by a user.
• After the data is retrieved in a dataset, you can filter the data stored in the
dataset by:
• Using the Select() method
• Using DataView objects
• A DataView object allows a fixed customized view of a given DataTable
object.
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 23 of 24
ADO.NET
- 24. Accessing and Manipulating Data
Summary (Contd.)
• A data view manager is a collection of data views and is useful while working
with related tables.
• A CurrencyManager object handles the binding to the data source by keeping
a pointer to the current item in the record list.
• A BindingContext object is used to keep track of the existing
CurrencyManager objects on a form.
©NIIT Creating Data Centric Applications using Lesson 1B / Slide 24 of 24
ADO.NET