Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm.

Similar presentations


Presentation on theme: "Introduction to Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm."— Presentation transcript:

1 Introduction to Data Adapter

2 A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm WebForm

3 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.

4 Data Adapter Wizard Configure Data Adapter and generating a dataset: –From the Data tab of the ToolBox, Drag OledbDataAdapter to the form. (If you don’t see it, right-click the Data tab and select Choose Item. Then select it from the.Net components) –Use the Data Adapter Wizard to configure the Adapter. –Right Click the Adapter to preview data and create dataset. Bind the dataset to controls. In the Form Load event, use Adapter’s Fill method to load the dataset: OleDbDataAdapter1.Fill(DataSet11)

5 Creating Bound DataGridView DataGridView control: –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: OleDbDataAdapter1.Fill(DataSet11) Note: A BindingSource object is created after choosing data source.

6 BindingSource Object 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 –RemoveCurrent

7 Adding a Save Button Save button: Use BindingSource EndEdit method and Adapter’s Update method: Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click DataSet11BindingSource.EndEdit() OleDbDataAdapter1.Update(DataSet11.CUSTOMER) End Sub

8 BindingSource ’ s Position Property If controls are bound to a BindingSource object, to move the current record we change the Position property of the BindingSource object: –To move to the next record: Me.EmpBindingSource.Position += 1 Or Me.EmpBindingSource.MoveNext –To move to the previous record: Me.EmpBindingSource.Position -= 1 Or Me.EmpBindingSource.MovePrevious

9 Binding Text Box Select Data Bindings property: –Text: choose field Add navigation buttons: –MoveNext: CUSTOMERBindingSource.Position += 1 Or CUSTOMERBindingSource.MoveNext –MovePrevious: CUSTOMERBindingSource.Position -= 1 Or CUSTOMERBindingSource.MovePrevious

10 Adding AddNew, Delete and SaveChange buttons. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click CUSTOMERBindingSource.AddNew() End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click CUSTOMERBindingSource.EndEdit() OleDbDataAdapter1.Update(DataSet11.CUSTOMER) End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click CUSTOMERBindingSource.RemoveCurrent() End Sub

11 BindingNavigator Object This object automatically adds navigation buttons to the form. Property: –BindingSource property Specify a BindingSource object (Must first define a BindingSource object) Note: It does not create the SaveChange button.

12 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.

13 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. Unbound textbox –To display the ValueMember Textbox1.text = ListBox1.SelectedValue –To display other fields: Textbox1.text = ListBox1.SelectedItem(“Cname”) Can we use TextBox1.text=ListBox1.SelectedItem? No!

14 ListBox SelectedItem Property How to display the selected record in unbound textbox? After binding to a data source, this property return a DataRowView object. What is DataRowView? –Object Browser: System.Data –DataRowView: Item property is the default property To retrieve a column from a DataRowView object (use 0-based index to identity a column): ListBox1.SelectedItem.Item(1) Or: ListBox1.SelectedItem(1) Or: ListBox1.SelectedItem(“Cname”)

15 Using Object Browser View/Object Browser DataSet object model: System.Data –DataSet Relations Tables –Rows –Columns Use Object Browser to study object’s properties, methods.

16 Working with SQL Server Data Adapter From the Data tab: –Choose: SQLDataAdapter


Download ppt "Introduction to Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm."

Similar presentations


Ads by Google