CICS 314: Advanced Visual Basic
.NET Programming
Multiple Tables and Parameterized Queries
- Tutorial 4 -
Master/Detail Relationships Using a Wizard for OLE DB and SQL Data Sources
Ghana Communication Technology University
Lecturer – Dr. Forgor Lempogo
2022
Objectives
Applied
Use the properties and methods of the BindingSource
class to navigate and modify the rows in a dataset.
Create and use a parameterized query with a data
source.
Customize a ToolStrip control by adding controls to it,
deleting controls from it, and formatting the controls that
are on it. Then, code the event handlers for making
these controls work.
Use a DataGridView control as part of a Master/Detail
form.
Work with master/detail tables using dataset designer
properties and methods of the DataGridView
2
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Objectives (2/2)
Knowledge
Explain why you might want to use code to work directly
with the binding source object for a data source.
Describe the use of a parameterized query and the
ToolStrip control that gets generated for the query.
3
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
What is Parameterized Query?
A parameterized query is a type of SQL query that
requires at least one parameter for execution.
A placeholder is normally substituted for the
parameter in the SQL query.
The parameter is then passed to the query in a
separate statement.
4
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Reason for Parameterized Queries
The major reasons for using parameterized
queries are:
They make queries more readable.
They help to protect the database from SQL
injection attacks.
5
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Parameterized Queries - Examples
The following is an example of an ADO.NET
parameterized query:
SELECT LastName FROM Person_TB WHERE P_ID =
@PersonID
@ PersonID is the parameter for this query, which
might be defined in a subsequent statement similar
to the following:
command.Parameters.Add(new SqlParameter("@PersonID",
thePersonID))
6
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Creating a Search Functionality
In this example we will use a parameterized query to create a search
functionality in our application
We will implement the search functionality on newForm5 using a
“Search Criteria Builder”
When a user enters a Person’s ID, the App will search and display the
Person’s information in the DataGridView.
The user can then click on another Button to return to all the records in
the DatagridView
Parameters for SQL are denoted as
@NameofParameter
For OLEDB they are denoted as
?
7
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The User Interface
Open newForm5
To lunch the “Search Criteria Builder”, do the following:
Select the DataGridView
On the top-right-corner of the DataGridview click on the little arrow
From the dropdown select “Add Query” to lunch the “Search Criteria
Builder” Window
8
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The Search Criteria Builder – MS Access
The “Search Criteria Builder”
already contains an SQL
Query in the “Query Text”
Field.
What is needed is for us to
add the WHERE clause:
WHERE P_ID =?
Next you must indicate a
Name for the Query in the
“New Query Name” Textbox
I chose the name:
FillByP_ID
Click Ok to Complete the
process
9
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The Search Criteria Builder – SQL Server
The “Search Criteria Builder”
already contains an SQL
Query in the “Query Text”
Field.
What is needed is for us to
add the WHERE clause:
WHERE P_ID =@P_ID
Next you must indicate a
Name for the Query in the
“New Query Name” Textbox
I chose the name:
FillByP_ID
Click Ok to Complete the
process
10
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The User Interface
Clicking on the OK button on the “Search Criteria Builder” will close it and
return to the newForm5
A new Toolstrip has been automatically added to newForm5
The new Toolstrip has three controls: a Label, Textbox and another
Label.
Change the text properties of the labels to anything appropriate
Change the text property of the P_ID Label to “Enter ID to Search” and
the text property of the FillByP_ID Labe to “Search”
11
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Adding a ToolStripButton
The next thing is to add a button that a user can click on to close the search
results and return to the main table.
To do this lets add a new ToolStripButton to the Toolstrip
We can add a new ToolStripButton by clicking on the arrow at the right end of
the new Toolstrip
Select Button from the dopdown to add the button.
12
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Adding a ToolStripButton
The next thing is to
set the properties of
the new
ToolStripButton.
Make sure the
ToolStripButton is
selected then set the
properties in the
Properties window
There are two very
important properties
you must set:
Change DispayStyle
to Text
Set the Text Property
to “View All”
13
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Programming the ToolStripButton
The next thing is to program/Code the
ToolStripButton.
Double-click on the ToolStripButton to open
the code editor
In the click even of the ToolStripButton we will
write the following code:
Me.Person_TBTableAdapter.Fill(Me.Contact_DBDataSet.Person_TB)
This code is the same as what is in the Load even
of newForm5
It fetches and displays all the data from the
Person_TB
14
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Test the App
Run the App and Enter any valid Person ID to search
and display his/her record
After searching Click on View All to return to the initial
data.
15
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Parameterized Queries - Code
The generated code for a parameterized query
Private Sub FillByP_IDToolStripButton_Click
(sender As Object, e As EventArgs)
Handles FillByP_IDToolStripButton.Click
Try
Me.Person_TBTableAdapter.FillByP_ID
(Me.Contact_DBDataSet.Person_TB,
CType(P_IDToolStripTextBox.Text, Integer))
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
16
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Parameterized Queries - Code
To return all data after search refill the dataset.Table with
the original Select statement
TableAdapter.QueryName(DataSet.TableName,
param1 [,param2]...)
18
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
More on the ToolStrip Control
The Items Collection Editor for a ToolStrip control
19
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The ToolStrip Control - Properties
Common properties of ToolStrip items
Property Description
DisplayStyle Indicates whether a button displays an
image, text, or both an image and text.
Image The image that’s displayed on a button if
you select Image or ImageAndText for the
DisplayStyle property.
Text The text that’s displayed on a button if you
select Text or ImageAndText for the
DisplayStyle property.
Width The width of the item.
20
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Show Whole Table After a Search - Code
The syntax for filling a DataGridView with the entire table
Private Sub ShowAllToolStripButton_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles FillByVendorIDToolStripButton.Click
Try
Me.Person_TBTableAdapter.Fill(Me.Contact_DBDataSet.Person
_TB)
Catch ex As SqlException
MessageBox.Show("SQL Server error # " & ex.Number &
": " & ex.Message, ex.GetType.ToString)
End Try
End Sub
21
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Working with Master/Detail Tables
Here we examine a DataSet that contains two
tables related through a common field.
A master/detail DataSet is a dataset with more
than one table.
Each master record is related to zero or more
records from the second table.
Parent and child terms may refer to master and
detail records.
23
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Setup the Dataset
The first thing to do when
working with multiple
tables in a wizard
connection is to setup
the dataset using the
dataset designer.
To lunch the dataset
designer, go to the
solutions explorer double
click on the file with
extension .xsd.
In this instance the file is
The dataset
called
Contact_DBDataSet.xsd
24
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Setup the Dataset
Lunching the Dataset Designer
will open all containing tables in
the dataset.
If you have followed the tutorials
from the beginning of the
semester you will have three
tables in the dataset
In the dataset designer we can
pretty much do most of the
things we do in our DBMS. i.e.
Add and delete tables, create
relationships between tables, etc.
You can also preview the data in
each table by right-clicking on a
table and selecting preview data,
then select preview from the pop
up window.
We can also create relationships
that same way we do in MS
Access: Select a primary key
field, drag it onto the foreign key
field to lunch the relationship
window.
25
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Create a Relationship
We can create relationships
that same way we do in MS
Access: Select a primary
key field, drag it onto the
foreign key field.
Lets create a Relationship
between the Person_TB
and Call_TB using the
Caller_ID(FK) and P_ID(PK)
from the Call_TB and
Person_TB respectively.
Be sure to select
“Relationship Only” Radio
button
Then click ok to return to
the dataset.
26
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Create a Relationship
There is now a
relationship between the
Person_TB
(Parent/Master) and the
Call_TB(Child/Detail)
27
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The Data Sources Window with
Relationship
After a relationship is
created between the
Person_TB
(Parent/Master) and the
Call_TB(Child/Detail)
Expanding the
Person_TB table will
reveal a duplicate
Call_TB table inside it,
just like one of the
attributes of the
Person_TB table.
The Duplicate table is the
Child table.
28
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The User Interface – Multi-Tables
Open newForm6
From the Data sources
Window, drag and drop the
child Call_TB table onto
newForm6
You must only use the
Call_TB table which is
inside the Person_TB table
for this exercise
Reorganize the controls to
get a good design. Add
more controls if necessary.
(You may use mine: see
adjacent )
29 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Test the Application
When the application is
run, you will only see the
record of a Person and
only the calls made by
that person
30 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Viewing Specific Call Information
The Datagridview on newForm6 displays a list of calls
made by one particular caller/Person.
In the next example, we want to add buttons to each
row of the Datagridview.
When a user clicks on a button in the datagridview, a
window will open, displaying information about the
particular call in the row whose button is clicked
31 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Adding a Column to DataGridview (1/3)
Open newForm6
Click on the little arrow on the top-right corner of the
datagridview and choose Add Column
32
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Adding a Column to DataGridview (2/3)
In the Add Column window choose Unbound column
Type an appropriate name in the name field (must be a valid
identifier). Mine is: ViewCall
Select DataGridViewButtonColumn from the type dropdown
Type an appropriate Text in the Header text field. Mine is: View
Call
Select Add to add the column (click Add only once and select
cancel to close the window)
33
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Adding a Column to DataGridview (3/3)
Go back to the DataGridView and Click on the little arrow on the
top-Right corner and choose Edit Column
Selecting the new column in the Edit Column window you will
notice properties on the right side.
Change the TEXT property to something appropriate (eg. View
Call)
Change the UseColumnTextForButtonValue property to TRUE
The click OK to finish.
34
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
View Call – CellContentClick
Double-click anywhere on the dataGridView on newForm6
to enter the CellContentClick and type the ff. code.
If e.ColumnIndex = 5 Then
' View Call button clicked
' Get the ID of the selected Message
Dim i As Integer = e.RowIndex
Dim row As DataGridViewRow =
DataGridView2.Rows(i)
Dim cell As DataGridViewCell = row.Cells(1)
Dim Call_ID As String = cell.Value
' Display the newForm7 form
Dim callForm As New newForm7
callForm.Tag = Call_ID
callForm.ShowDialog()
End If
35
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The User Interface – View Call Info (1/2)
Add a new form to your
project and give it an
appropriate name
(mine is newForm7)
We will now create a
parameterized query to
help display the
specific message we
will select from the
DataGridView in
newForm6
To create a query
lunch the Dataset
designer from the
solution explorer
window
36 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Create Parameterized Query
Lunching the Dataset
Designer will open all
containing tables in the
dataset.
To create a query, right-
click on the bottom half
of the Call_TB Table,
beneath the gray colour.
Select Add Query to
lunch the “Table Adapter
Query configuration
Wizard”
37
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Table Adapter Query configuration Wizard
On the “choose
Command type”
window Select
“Use SQL
Statements
And Click on Next
to continue
38 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Choose a Query Type
On the “choose a
query type”
window Select
“Select which
returns rows”
And Click on Next
to continue
39 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Write the Query Text– MS Access
The “Specify a Select
Command” Window already
contains an SQL Query in it.
What is needed is for us to
add the WHERE clause:
WHERE C_ID =?
Click Next to Proceed
40
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Write the Query Text– SQL Server
The “Specify a Select
Command” Window already
contains an SQL Query in it.
What is needed is for us to
add the WHERE clause:
WHERE C_ID =@C_ID
Click Next to Proceed
41
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Save the Methods
Next you must indicate a
Name for the Query in the
“Method Name” Textbox
I chose the name:
FillByC_ID
Click Finish to Complete the
process
42
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The User Interface – View Call info (2/2)
Open newForm7
In the Data sources Window,
change the default view of
Call_TB Table from
DataGridView to Detail.
From the Data sources
Window, drag and drop the
child Call_TB table onto
newForm7
Reorganize the controls to get
a good design. Add more
controls if necessary. (You
may use mine: see adjacent )
43 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Load Event of newForm7
In the Form Load, Replace the auto-generated code with
the following:
Private Sub newForm7_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
Dim Call_ID As String = Me.Tag
Try
Me.Call_TBTableAdapter.FillByC_ID(Me.Contact_DBDataSet.Call_TB,
CType(Call_ID, Integer))
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
44
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Test the Application
When the application is
run, you will only see the
record of a Person and
only the calls made by
that person
Then when you click o the
view call button the view
call window will pop up
with that specific call
information
45 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Individual Assignment
For all the functionalities that have been
implemented for the Call_TB, Add forms
and codes to implement same for the
Message_TB table
Test and upload word report on the course
page
46
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Formatting a Column
Dialogue box for formatting a column
47
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Binding a Combo Box
Binding a Combo box to a Data source
50
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Binding a Combo Box - Properties
Property Description
DataSource The data table that contains the data
displayed in the list.
DisplayMember The data column whose data is
displayed in the list.
ValueMember The data column whose value is
stored in the list.
SelectedValue Gets the value of the currently
selected item
51
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The Binding Source - Properties
Common properties of the BindingSource class
Property Description
Position The index of the current row in the data
source.
Count The number of rows in the data source.
52
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The Binding Source - Methods
Common Methods of the BindingSource class
Property Description
AddNew() Adds a new, blank row to the data source.
EndEdit() Saves changes to the current row.
CancelEdit() Cancels changes to the current row.
RemoveCurrent() Removes the current row from the data
source
MoveFirst() Moves to the first row in the data source.
MovePrevious() Moves to the previous row in the data
source, if there is one.
MoveNext() Moves to the next row in the data source, if
there is one.
MoveLast() Moves to the last row in the data source.
53
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The Binding Source - Examples
A statement that adds a new row to a data source
Me.VendorsBindingSource.AddNew()
A statement that saves the changes to the current row and
ends the edit
Me.VendorsBindingSource.EndEdit()
A statement that cancels the changes to the current row
Me.VendorsBindingSource.CancelEdit()
A statement that removes the current row from a data
source
Me.VendorsBindingSource.RemoveCurrent()
54
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The Binding Source – Example 1
Code that moves to the next row and displays the
position and count
Private Sub btnNext_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles btnNext.Click
Me.VendorsBindingSource.MoveNext()
Dim position As Integer =
VendorsBindingSource.Position + 1
txtPosition.Text = position & " of " &
VendorsBindingSource.Count
End Sub
55
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The DataGridView Control – The Column
The dialog box for editing a DataGridView control
Property Description
HeaderText The text that’s displayed in the column header.
Width The number of pixels that are used for the
width of the column.
DefaultCellStyle The style that’s applied to the cell.
Visible Determines if the column is visible in the
control.
ReadOnly Determines if the data in the column can be
modified.
SortMode Determines if the data in the grid can be
sorted by the values in the column and how
the sorting is performed
56
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The DataGridView Control – The Cell
The CellStyle Builder dialog box
57
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The DataGridView Control – Format string
The Format String dialog box
58
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Questions?
59
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery