ISYS 512 Data Adapter Wizard 2 nd Level of Using ADONet Configure Data Adapter and generating a dataset From the Data tab of the ToolBox Drag SqlDataAdapter to the form Use the Data Adapter Wizard to configure the Adapter ID: 458749
Download Presentation The PPT/PDF document "Using Adapter Wizard" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Using Adapter Wizard
ISYS 512Slide2
Data Adapter Wizard – 2
nd
Level of Using ADO.Net
Configure Data Adapter and generating a dataset:
From the Data tab of the
ToolBox
,
Drag
SqlDataAdapter
to the form.
Use the Data Adapter Wizard to configure the Adapter.
Right Click the Adapter to preview data and creat
e
dataset.
Note: If
SqlDataAdapter
is not listed in Toolbox’s Data tab , right click
ToolBox
Data Tab and select Choose Item; then select
SqlDataAdapter
from
.Net
Framework components.Slide3
Creating a Form with Bound
DataGridView
Configure Adapter and generate dataset.
Bind
DataGrid
View
control to the table:
Data Source property
:
DataSet
Data Member property
A table in the dataset
In the Form Load event, use Adapter’s Fill method to load the dataset:
SqlDataAdapter1.Fill(DataSet11
);
Note 1: No navigation capability
Note 2: View adapter’s properties and see the SQL commands.Slide4
Bind Controls to
BindingSource
Object
for one-record-at-a-time navigation
Configure Adapter and generate dataset.
Drag the
BindingSource
object
from Toolbox Data tab to
the
form.
Use
BindingSource
object’s property window to set its
DataSource
and Data Member properties to the dataset object and table object.
Set control’s
DataBinding
property to the
BindingSource
object.Slide5
Creating a Form with Bound
Textboxes
Configure Adapter and generate dataset.
Add and define a
bindingSource
object
Select textbox’s
Data Bindings property:
Text: choose field from
bindingSOurce
object
In the Form Load event, use Adapter’s Fill method to load the dataset:
SqlDataAdapter1.Fill(DataSet11
)
Note: No navigation capabilitySlide6
Using BindingSource Object to Do the Binding and Navigation
It is an object that keeps track of position (the current row) of a data source.
Useful properties:
DataSource
DataMember
Position property: is the index of the current row. The index is a 0-based index, the first record has a position of 0.
Methods:
MoveFirst
,
MoveLast
,
MoveNext
,
MovePrevious
AddNew
AllowEdit
EndEdit
Current
RemoveCurrentSlide7
Use BindingSource Object’s MoveNext, MovePrevious
Methods
Add a MoveNext button
bindingSource1.MoveNext();
Add a MovePrevious button:
bindingSource1.MovePrevious(); Slide8
Adding AddNew and Save Button
AddNew button: Use BindingSource AddNew Method
:
private void button3_Click(object sender, EventArgs e)
{
bindingSource1.AddNew();
}
Save button: Use BindingSource EndEdit method and Adapter’s Update method:
private void button4_Click(object sender, EventArgs e)
{
bindingSource1.EndEdit();
SqlDataAdapter1.Update(dataSet2.CUSTOMER
);
}Slide9
Binding ListBox
Example: Bind Customer Table’s CID field to a
listbox
.
Create a Adapter
for
Customer table
, and generate the dataset.
Add
ListBox
and set binding properties:
Data Source
: Customer table
Display Member
: Field to display in the listbox.Value Member: the actual values for items in the list box. Slide10
Display Selected Record
Bound textbox
(same data source as the
listbox
)
:
If the
Listbox
and the textbox are bound to the same
BindingSource
object, the textbox will automatically displays the record of the selected
listbox
item. Slide11
Working with
OleDB
Data
Adapter
Use
OleDB
provider to work with another databases such as Oracle, MS Access, etc.
From the Data tab:
Choose:
OleDBDataAdapter
(If you don’t see it, right-click the Data tab and select Choose Item. Then select it from the
.Net
components)Slide12
DataSet and Data Adapter
DataSet
Object: A
DataSet
object can hold several tables and relationships between tables.
DataAdapter
: This the object used to pass data between the database and the dataset.
It is an object with SQL Select, Insert, Update and Delete commands.Slide13
Creating Parent/Child Form Using DataAdapter Wizard
Add two
DataAdapters
to the form: one for the parent table and one for the child table.
Use the parent table’s
DataAdapter
to generate a dataset; then use the child table’s
DataAdapter
to generate the child table into the same dataset.
Open dataset’s designer and right-click the parent table and choose Add/Relation to add a relation.
Add and bind textboxes to the parent table; add
dataGridView
and bind its
datasource
property to the relation.
Add navigation button’s using parent table’s BindingSource object.