76
1 Web-Enabled Decision Support Systems Database Connectivity with ADO .NET Prof. Name [email protected] Position (123) 456-7890 University Name

1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name [email protected] Position (123) 456-7890 University Name

Embed Size (px)

Citation preview

Page 1: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

1

Web-Enabled Decision Support Systems

Database Connectivity with ADO .NET

Prof. Name [email protected] (123) 456-7890University Name

Page 2: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

2

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 3: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

3

Introduction

A database application is a computer program that allow users to manipulate data in a DBMS through a user-friendly interface

Examples:– Amazon.com

Online shopping

– Mapquest.com Driving directions

– University portals Transcripts and tuition payment information

Page 4: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

4

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 5: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

5

Database Application Types

We classify database applications into the following three categories:– Display-oriented applications

Display data retrieved from a relational database on forms or web pages Examples: Online newspapers, portal to view student transcript and grades,

mapping websites

– Transaction-oriented applications Involve frequent transfer of data to and from a database Examples: Online shopping, portal for university faculty to enter student grades

– Communication-oriented applications Communicate with other applications or processes Examples: Application with underlying optimization software, Crystal Reports,

Map Point

Page 6: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

6

Database Application Overview

A VB .NET database application involves connectivity between a database and a graphical user interface (GUI)

Achieved through ActiveX Data Objects (ADO) .NET– A collection of objects (classes) designed to support data access and

manipulation

– The ADO .NET architecture forms the basis of VB .NET database applications

– ADO .NET object examples: Connection object TableAdapter object DataSet object

Page 7: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

7

Database Application Processes

1. Connecting to the database– Establish a two-way communication channel via Connection object

2. Fetching data using database queries– Bring data into an application in the desired format (filtered, sorted, etc.) via

TableAdapter object

3. Temporarily storing the result somewhere– Via DataSet object

4. Displaying data on Windows forms– Data binding

5. Editing data in the application

6. Saving updated data back in the database

Page 8: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

8

Database Application Data Cycle

Database Application Data Cycle

Page 9: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

9

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 10: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

10

Create the Project

How-to: Create a Simple Database Application1. In Visual Studio, choose the File | New Project option from the Main menu to

open the New Project dialog box.

2. Make sure to select the Windows Application icon in the Templates area.

3. Name the project DatabaseConnectivity and click OK.

Page 11: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

11

Create a Data Source

4. Choose the Data | Show Data Sources option from the Main menu.

5. In the Data Sources Window, click Add New Data Source to start the Data Source Configuration Wizard.

Opening the Data Sources Window

Invoking the Data Sources Configuration Wizard

Page 12: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

12

Create a Data Source (cont.)

6. Select the Database icon on the Choose a Data Source Type page of the wizard. Click Next.

Choosing a Data Source Type

Page 13: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

13

Create a Data Source (cont.)

7. On the Choose Your Data Connection page, click New Connection to open the Choose Data Source dialog box.

Specifying the Data Connection

Page 14: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

14

Create a Data Source (cont.)

8. Select the Microsoft Access Database File in the Data Source list box. Click on Continue.

9. In the Add Connection dialog box that opens, select the University database file. Click Test Connection to verify the connection. Click OK.

Creating the Database Connection

Page 15: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

15

Create a Data Source (cont.)

10.Click Yes to copy the database to the current project folder. Click Next.

11. On the Save the Connection String to the Application Configuration File page, save the connection string with its default name. Click Next.

Saving the Connection String

Page 16: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

16

Create a Data Source (cont.)

12.Expand the Tables node on the Choose your Database Objects page, and select the student, department, transcript, and faculty tables. Click Finish.

Selecting Database Tables for a Data Source

Page 17: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

17

Using a DataGridView Control

13. In the Data Sources Window, drag the student table onto Form1 to create the DataGridView control along with a ToolStrip at the top of the form. The ToolStrip provides controls for navigation, adding/deleting records, and

saving data

Drag-and-Drop to Create DataGridView Control

Snapshot of the Component Tray

Page 18: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

18

Test the Application

14.Press Ctrl + F5 to run the application.

15.Test the ToolStrip functionality by navigating through the student records.

16.Alter values in the DataGridView control, and click on the Save button in the ToolStrip to modify the data.

Application Output

Page 19: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

19

Property Window Review

17.Click the smart tag on the DataGridView control to view its Tasks list. Note that the DataSource property is set to the TblStudentBindingSource. Also note the automatic enabling of various options.

Tasks List of a DataGridView Control

Page 20: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

20

Code Review

18.Double click anywhere on the form to open the code behind window. Review the auto-generated code that was created when we dragged the student

table from the Data Sources Window.

Form1_Load Event Populating the Student DataTable

Page 21: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

21

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 22: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

22

Auto-Created Objects in the Component Tray

Four objects were added to the Component tray when we dragged the student table node from the Data Sources Window onto Form1:1. TableAdapter object

Host for SQL queries Corresponding to each query, has a Fill method that executes a SQL query

2. DataSet object Acts as an in-memory data repository Can have any number of DataTables DataTables are populated using a TableAdapter, by means of results of SQL

queries

Page 23: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

23

Auto-Created Objects in the Component Tray (cont.)

3. BindingSource object Mediator between a Windows control and a DataSet

4. BindingNavigator object Enables users to navigate through and manipulate data on Windows forms with

the help of a ToolStrip control

BindingSource Object Work Diagram

Page 24: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

24

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 25: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

25

Setting the Controls to be Created

How-to: Display Data in Individual Windows Controls1. Add a new form, Form2, to the DatabaseConnectivity application.

2. In the Data Sources Window, select the student table node, and click the drop-down arrow to select the Details option. The default is the DataGridView control.

Selecting the Details Option for a Table Display

Page 26: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

26

Setting the Controls to be Created (cont.)

3. Expand the tblStudent node in the Data Sources Window.

4. Choose the controls for all the columns as shown below.

Individual Displaying Controls for the Columns of the Student Table

Page 27: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

27

Setting the Controls to be Created (cont.)

5. Drag the tblStudent node from the Data Sources Window onto Form2 to create individual data-bind controls for each selected column. These controls are accompanied by appropriately titled Label controls.

Individual Controls Shown in Form’s Design View

Page 28: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

28

Test the Application

6. Set Form2 as the start-up form. Press Ctrl + F5 to run the application. The first record displayed is shown below.

7. Use the ToolStrip on top of the form to navigate through the records. Edit the value of any field, and click Save to test its functionality.

Student Table Displayed One Record at a Time

Page 29: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

29

Property Window Review

8. Select the TextBox control for the Name field, and navigate to its DataBindings property in the Property Window. Note that the Text property of the Name TextBox is associated with the Name

column from the TblStudentBindingSource.

Data Binding Properties for the Name Column’s TextBox Control

Page 30: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

30

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 31: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

31

Adding a PictureBox Control

How-to: Bind Data to Existing Controls1. Continue with the Windows form created in the previous hands-on tutorial.

Drag-and-drop a PictureBox control onto the right of the existing controls.

2. Verify that the Image property of the PictureBox control has the default value None.

Running Application with Data-bind PictureBox Control

Page 32: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

32

Associating the Picture Column and Testing

3. Drag-and-drop the Picture column under the student table node from the Data Sources Window onto the PictureBox control.

4. Press Ctrl + F5 to run the application. Test the data binding for PictureBox by navigating through student records using the ToolStrip.

Running Application with Data-bind PictureBox Control

Page 33: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

33

Property Window Review

5. Re-open the Property Window for the PictureBox control and review its Image property. It is now associated with the Picture column from the TblStudentBindingSource.

Data-bind Image Property for the PictureBox Control

Page 34: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

34

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 35: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

35

Adding the Related Transcript Table

How-to: Display Related Data on a Windows Form1. In the Data Sources Window, expand the student table node and select the

transcript table node. Drag-and-drop it onto the bottom of Form2.

Adding Related Tables from the Data Sources Window

Component Tray

Page 36: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

36

Editing the Transcript DataGridView

2. Select the transcript DataGridView and click its smart tag to view the Tasks list. Select the Edit Column option from the list to open the Edit Columns dialog box.

Invoking DataGridView’s Edit Columns Dialog Box

Page 37: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

37

Editing the Transcript DataGridView (cont.)

3. Select the StudentID column and click Remove to delete the column. In general, we can use the Edit Columns dialog box to edit properties of existing

columns, such as column heading, width, and more.

Removing StudentID Column from the DataGridView Control

Page 38: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

38

Editing the Transcript DataGridView (cont.)

4. Select the DataGridView and navigate to the Property Window.

5. Select the AlternatingRowsDefaultCellStyle property and click the Build button (…) to edit the alternating cell style.

Styling Alternate Rows of a DataGridView Control

Page 39: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

39

Editing the Transcript DataGridView (cont.)

6. Choose the BackColor and ForeColor properties in the CellStyle Builder dialog box as shown below.

Setting BackColor and ForeColor Properties

Page 40: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

40

Testing the Application

7. Press Ctrl + F5 to run the application. Test the parent-child relationship by navigating through student records using the ToolStrip.

Running Application with Parent-Child Relationship

Page 41: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

41

Property Window Review

8. Select the TblStudentBindingSource object (parent) from the Component tray and open the Property Window. Note that the DataSource property of this object is set to the UniversityDataSet. Also, the DataMember property is set to the tblStudent data table.

DataSource Property of Parent BindingSource

Page 42: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

42

Property Window Review (cont.)

9. Now select the TblTranscriptBindingSource object (child) from the Component tray and open the Property Window. Note that the DataSource property of this object is set to the

TblStudentBindingSource. Also, the DataMember property is set to tblStudenttblTranscript, which is the

name of the Data Relation object that relates parent and child tables.

DataSource Property of Child BindingSource

Page 43: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

43

Code Review

Since we added one more table on the form, Visual Studio added one more line of code. – Executes the appropriate SQL query to fill the tblTranscript data table.

Form2_Load Event Populating the Transcript and Student Tables

Page 44: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

44

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 45: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

45

DataGridView Control

The DataGridView control is one of the most used data controls – We can use it to:

Display data Format data Sort data Update and delete data Select and navigate through data records

DataGridView Tasks:– We can use the smart tag to:

Access the DataSource property of the control Add a new column to the column collection Enable or disable edit, delete, and insertion operations

Page 46: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

46

Sorting with DataGridView

DataGridView controls may be sorted by clicking any column heading in the grid– Click repeatedly to toggle sorting order (ascending/descending)

– A small triangular icon appears in the column when sorted

Sorting Data by DeptID Column in Descending Order

Page 47: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

47

Data Binding

Binding data to the DataGridView control is intuitive and straightforward– Specify data source by setting the DataSource property to a BindingSource

– Set DataMember property for specific tables/queries

DataSource and DataMember Properties of a DataGridView Control

Page 48: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

48

DataGridView: In Depth

Additional properties:– BackColor, BackgroundColor, ColumnCount, Font, ForeColor, Name,

ReadOnly, RowCount, Visible, Width

Name Description

Columns Gets a collection that contains all the columns in the control.

CurrentCell Gets or sets the currently active cell.

CurrentRow Gets the row containing the current cell.

DataMember Gets or sets the name of the table in the data source for which the grid is

displaying data.

DataSource Gets or sets the data source that the grid is displaying data for.

Item Gets or sets the cell located at the intersection of the specified row and column.

Rows Gets a collection that contains all the rows in the grid.

Page 49: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

49

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 50: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

50

Hands-On Tutorial: Creating a Search Form

Often we are not interested in viewing an entire table of data– Prefer to display only the data that satisfies some specified criteria

– We can do this using search forms in a database application

A parameterized query takes in some criteria as parameters and retrieves records that satisfy the criteria – Improve the efficiency of database applications

– Fetch only the portion of the data that interests us

Page 51: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

51

Adding a Parameterized Query

How-to: Create a Search Form1. Add a form, Form3, to the DatabaseConnectivity application.

2. Drag and drop tblStudent from the DataSources Window to the form. This creates a DataGridView control named TblStudentDataGridView.

3. Choose the Add Query option from the Tasks list of the DataGridView.

Adding a Query to a TableAdapter Through DataGridView

Page 52: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

52

Adding a Parameterized Query (cont.)

4. This opens the Search Criteria Builder dialog box. Name the new parameter query FillByDept. Enter the SQL query as shown below in the Query Text area to construct a parameterized query.

Adding Queries in Search Criteria Builder Dialog Box

Page 53: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

53

Adding a Parameterized Query (cont.)

5. Click the Query Builder button in the Search Criteria Builder dialog box.

6. Click the Execute Query button to test the query.

Working with the Query Builder to Design SQL Queries

Page 54: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

54

Adding a Parameterized Query (cont.)

7. A Query Parameters dialog box opens. Test the query with the example “CISE” for the DeptID parameter value and click OK.

8. Preview the results of the query in the Query Builder itself. Note that all the records that are displayed have CISE as the DeptID. Click OK.

Assigning a Sample Value to a Query Parameter

Page 55: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

55

Adding a Parameterized Query (cont.)

9. We have now added a parameterized query, FillByDept, to the TblStudentTableAdapter. This automatically creates the ToolStrip control for DeptID input at the top of Form3. We can also see that a ToolStrip control has been added to the Component tray.

A ToolStrip Control for a Parameterized Query

Page 56: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

56

Preview Data for Newly Added Query

10.Use the DataGridView’s smart tag to view its Tasks list, and choose the Preview Data option to open a dialog box.

11. Choose the query FillByDept from the drop-down list as shown below.

Selecting a Query in the Preview Data Dialog Box

Page 57: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

57

Preview Data for Newly Added Query (cont.)

12.Provide the testing parameter for the query as “CISE” and click Preview. The dialog box resembles below.

The Preview Data Dialog Box in Action

Page 58: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

58

Testing the Application

13.Set Form3 as the start-up form of the application, and press Ctrl + F5 to run the application. Test the search or filtering functionality by entering different DeptIDs in the

FillByDept ToolStrip.

Running Form3 with the Search Functionality

Page 59: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

59

Code Review

14.Open the Code Window for Form3 by double-clicking on the form. Note the Visual Studio auto-generated the Click event. Executes the FillByDept query of the TblStudentTableAdapter and fills the

tblStudent data table of the UniversityDataSet.

Executing Parameterized SQL Statements

Page 60: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

60

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 61: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

61

The Query Builder

The Query Builder dialog box helps us build SQL queries – Divided into four horizontal segments (listed from top to bottom):

Table Pane: Displays and allows the addition or deletion of DataTables and their relationships

Design Grid: Specifies the sorting order, filter criteria, group by, and output value for the fields being displayed

SQL View: Displays the SQL statement as we build the query in the Design Grid

Preview: Displays a preview of the query output

Page 62: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

62

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 63: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

63

Linking the Department Table

How-to: Create a Look-up Table1. Drag tblDepartment from the Data Sources Window directly onto the DeptID

ComboBox control on Form2.

2. Note that the DataSource property of the DeptID ComboBox control is set to TblDepartmentBindingSource.

(a)

(b)

Creating a Department Look-up Table

Component Tray

Page 64: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

64

Linking the Department Table (cont.)

3. Set the DisplayMember property of the DeptID ComboBox control to Name and verify that the ValueMemeber property is set to the DeptID column. The new Component tray objects will query and bring the names of the

departments as a list into the DeptID ComboBox control.

Page 65: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

65

Testing the Application

4. Set Form2 as the start-up form, and press Ctrl + F5 to run the application.

5. Navigate through the student records. Note that the ComboBox displays the department to which the student belongs. Use the drop-down arrow on the ComboBox to see the list of all the departments.

Running Application with a Look-up Table

Page 66: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

66

Property Window Review

6. Open the tasks list for the DeptID ComboBox control. Note the values for all properties of the control.

Tasks for ComboBox with a Lookup Table

Page 67: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

67

Property Window Review (cont.)

7. Open the Property Window for DeptID ComboBox control and locate its Data Binding section. Note the SelectedValue property value.

Property Window for DeptID ComboBox Control

Page 68: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

68

Code Review

The auto generated code for the lookup table is very simple. – The only addition to the code is a call to the Fill method of

TblDepartmentTableAdapter (lines 25-26).

Form2_Load Event Showing Fill Method for Look-up Table

Page 69: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

69

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 70: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

70

Data Binding Properties of a ComboBox Control

There are four main data binding properties of a ComboBox:

Data Source Set it to the BindingSource object that will provide data for the ComboBox.

Unlike DataGridView, BindingSource for ComboBox should contain a single

table (simple binding).

Display Member Set it to the field from the data source of a ComboBox that should be

displayed to the user.

Value Member Set it to the field from the data source from which the ComboBox should get

its value. This value can be accessed in code through the property

ComboBox.SelectedValue.

Selected Value The value of a selection in the ComboBox. This property can be bound to any

field in the project’s DataSet as long as its value matches one or more values

from the ComboBox’s ValueMember. For example, we have the department

table as the data source of the ComboBox with DeptID as the ValueMember.

The SelectedValue property is set to the DeptID from the student table.

Page 71: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

71

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 72: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

72

In-Class Assignment

Develop an application that displays the list of faculty from the University database– Display on both:

DataGridView control Individual controls

– Show a faculty picture in the PictureBox control

– Add a look-up table for the DeptID in an individual control on the form

– Add search functionality to the form to display only those faculty records for which:

Query: “The salary is greater than the user-entered salary value and the department is the department specified by the user.”

Hint: Add a parameter query to the TableAdapter with two parameters

Page 73: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

73

Overview

14.1 Introduction 14.2 Database Applications Overview 14.3 Hands-On Tutorial: Creating a Simple Database Application 14.4 Auto-Created Objects in the Component Tray 14.5 Hands-On Tutorial: Displaying Data in Individual Windows Controls 14.6 Hands-On Tutorial: Binding Data to Existing Controls 14.7 Hands-On Tutorial: Displaying Related Data on a Windows Form 14.8 DataGridView Control 14.9 Hands-On Tutorial: Creating a Search Form 14.10 The Query Builder 14.11 Hands-On Tutorial: Creating a Look-up Table 14.12 Data Binding Properties of a ComboBox Control 14.13 In-Class Assignment 14.14 Summary

Page 74: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

74

Summary

Database applications can be roughly divided into three types: – Display-oriented

– Transaction-oriented

– Communication-oriented

The database application development process can be summarized into the following steps: 1. Connect to the database using a Connection object

2. Fetch data using database queries in the TableAdapter object

3. Temporarily store the results in a DataSet object

4. Display data on a Windows form by binding Windows controls to a DataSet

5. Edit data in the application

6. Save data back in the database

Page 75: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

75

Summary (cont.)

We walked through the process of building a simple database application displaying the student table fields on the DataGridView control. – We reviewed the auto-generated code and auto-set properties by Visual

Studio.

We also showed how the same data and columns can be displayed in individual controls on a form. – We then extended this application to bind the PictureBox to the picture

column of the student table.

We further enhanced the application by adding a related transcript table to the form.– Used to display the courses and grades for each student record as we

navigate through them.

Page 76: 1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name name@email.com Position (123) 456-7890 University Name

76

Summary (cont.)

We have added a search or filter function to the form. – Displays only those student records belonging to the department specified by

us in the ToolStrip.

Finally, we have added a look-up table for the department field of the student table.– Allows us to list all the department names via a drop-down list.