Tutorial4 - Wizard - Multiple Tables and Parameterized Queries - 2022
Tutorial4 - Wizard - Multiple Tables and Parameterized Queries - 2022
.NET Programming
3
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
What is Parameterized Query?
4
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Reason for Parameterized Queries
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:
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
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
@NameofParameter
?
7
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
The User Interface
Open newForm5
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.
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.
We can add a new ToolStripButton by clicking on the arrow at the right end of
the new Toolstrip
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.
15
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Parameterized Queries - Code
The generated code for a parameterized query
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.
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.
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.
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
30 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Viewing Specific Call Information
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
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.
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”
39 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Write the Query Text– MS Access
40
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2022 Delivery
Write the Query Text– SQL Server
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
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.
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
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
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
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
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()
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