Tutorial5 - Wizard - Data Access With ADO - NET - 2021
Tutorial5 - Wizard - Data Access With ADO - NET - 2021
.NET Programming
2
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Objectives (continued)
Knowledge
Describe the use of a connection string in an app.config file.
Describe the use of the Fill method of the TableAdapter object.
Describe the use of the UpdateAll method of the
TableAdapterManager object.
Describe the use of the EndEdit method of the BindingSource
object.
Describe the two categories of data errors that can occur when
you run an application that uses a data source.
In general terms, describe the way the SQL statements that are
generated for a data source
(1) prevent concurrency errors
(2) refresh a dataset when the database generates the keys for new
rows.
3
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Getting Started
4
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
The User Interface
5 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Creating a Data Source (1/2)
After adding the form, the
next thing to do is to create
a connection to the
database using the
Connection wizard
6 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Creating a Data Source (2/2)
Menu->View->Other
Windows-> Data
Sources
7 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
The Data Sources Window
8 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Choose a Data Source Type Window
The first step of the
connection wizard is the
“Choose a Data Source
Type” Window.
9 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Choose a Database Model Window
The next step of the connection
wizard is the “Choose a
Database Model” Window.
10 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Choose your Data Connection Window
The next step of the
connection wizard is the
“Choose your Data
Connection” Window.
12 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Change Data Source Window
The Next step of setting the
connection string is the “Change Data
Source ” Window.
13 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Add Connection Window – OLEDB
For OLEDB Clicking of OK on the Previous
“Change Data Source ” Window will take
you back to the same “Add Connection”
Window in slide 12
14 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Add Connection Window – OLEDB
Next select Test Connection
to be sure everything is
correctly set up.
If an error message is
shown, then you need to
repeat the process and be
sure the user name and
passwords, if any, are
correct
15 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Change Data Source - MS SQL SERVER
Users of MS Access
Database must select
“Microsoft Access
Database File” and Click
on OK to proceed
16 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Add Connection – MS SQL SERVER
For MS SQL Server Database Clicking on OK on the
Previous “Change Data Source ” Window will take you to
the following “Add Connection” Window
17 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Add Connection – MS SQL SERVER
Next select Test Connection to be
sure everything is correctly set up.
18 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
View your Connection String
The connection string is
ready.
19 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
View your Connection String
Clicking the Next button
on the “Choose your
Data Connection”
Window will generate the
following dialog.
20 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Save Connection String Window
Selecting Yes from the
dialog will open the “Save
Connection String” Window
21 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Choose your Data Objects Window
On the “Choose your Data
Objects” window you are asked to
select which tables and views you
want to include in the dataset.
22 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
The Data Source Window
After exiting the
Connection wizard, the
Data Sources Window
will now contain your
database and the
tables you selected
Namely:
The dataset file
The data source file
The app.config file The dataset
24 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
The App. Configuration File – SQL Server
<connectionStrings>
<add name=
" AdvanceVBNET_Totorial.My.MySettings.Contact_DBConnectionString"
connectionString="Data Source=localhost\sqlexpress;
Initial Catalog=Contact_DB;
Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
25
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
The App. Configuration File – OLEDB
<connectionStrings>
<add
name="AdvanceVBNET_Totorial.My.MySettings.Contact_DBConnectionSt
ring"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=|DataDirectory|\Contact_DB.mdb"
providerName="System.Data.OleDb" />
</connectionStrings>
26
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Handling Columns With Default Values
28
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Auto-Added Controls
After drag-and-droping the table on the form different
controls were automatically added to the form
The controls that are created when you drag a data source
onto a form are:
Control Description
DataGridView control Displays the data from the data
source in a grid.
29
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Auto-Added Controls
The objects that are created when you drag a data source
to a form
Object Description
BindingSource Identifies the data source that the controls on
the form are bound to and provides
functionality for working with the data source.
DataSet Provides access to all of the tables, views,
stored procedures, and functions that are
available to the project.
TableAdapter Provides the commands that read and write
data to and from a table.
31
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
BindingNavigator - The ToolStrip
To First Total To Last
Delete
Record To records Record
Record
Previous
Record
33
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Data NOT Saved Problem
TableAdapter.Update(DataSet.TableName)
36
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Changing the Default Control (1/2)
By default when tables are dragged onto a
form the DataGridView Control is added to
display the data.
But this can be changed to make other
controls such as textboxes, labels,
comboboxes, etc. display various fields of
our tables.
To change the default controls of a table do
the following:
Open the Data sources window and select
the Table whose controls you wish to
change
Click on the arrow to the right of the table
and select Details
This will change the controls from
Datagridview to the appropriate default
controls for the specific data types.
Eg. For a column with a text data type, a textbox
will be used,
for a column with Date datatype Datetimepicker
will be used, etc.
38
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Changing the Default Control (2/2)
To change the default controls of each
field/Column do the following:
Open the Data sources window and
select the Table whose controls you wish
to change
Click on the arrow to the right of the table
and select Details
40 CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Creating the User Interface
Open newForm6
Open the Data sources window and change the default view of Person_TB from
DataGridView to Detail
Go through the attributes of the Person_TB table and change them to appropriate
controls
From the Data Sources window drag-and-drop the Person_TB table onto newForm6
Expand the form and reorganize the controls to get a good design
41
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Test The Application
42
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery
Code for the Application
Public Class Form1
45
CICS 314: Advanced Visual Basic .NET Programming - GTUC 2021 Delivery