C# Tutorial MSM_Murach chapter-18-slides

of 39 /39
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 1 C hapter18 How to w ork w ith data sources and datasets

Transcript of C# Tutorial MSM_Murach chapter-18-slides

Page 1: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 1

Chapter 18

How to work with data sources and datasets

Page 2: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 2

Objectives Applied 1. Use a data source to get the data that an application requires. 2. Use a DataGridView control to present the data that’s retrieved

by a data source. 3. Use other controls like text boxes to present the data that’s

retrieved by a data source. 4. Write the code for handling any data errors that result from the

use of the data source or the controls that are bound to it. 5. Use the Dataset Designer to (1) view the schema for the dataset

of a data source, (2) modify a query using the Query Builder, (3) preview the data for a query, or (4) review the SQL statements that are generated for a data source.

Page 3: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 3

Objectives (continued) Knowledge 1. Describe the use of a connection string in an app.config file. 2. Describe the use of the Fill method of the TableAdapter object and

the UpdateAll method of the TableAdapterManager object. 3. Describe the use of the EndEdit method of the BindingSource

object. 4. Describe the two categories of data errors that can occur when you

run an application that uses a data source. 5. Describe the use of the DataError event for a DataGridView control. 6. In general terms, describe the way the SQL statements that are

generated for a data source (1) prevent concurrency errors and (2) refresh a dataset when the database generates the keys for new rows.

Page 4: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 4

An empty Data Sources window

Page 5: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 5

A Data Sources window after a data source has been added

Page 6: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 6

The first step of the Data Source Wizard

Page 7: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 7

The second step of the Wizard

Page 8: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 8

The third step of the Wizard

Page 9: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 9

The Add Connection dialog box

Page 10: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 10

The Change Data Source dialog box

Page 11: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 11

The fourth step of the Wizard

Page 12: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 12

The information that’s stored in the app.config file <connectionStrings> <add name="ProductMaintenance.Properties.Settings. MMABooksConnectionString" connectionString="Data Source=localhost\sqlexpress; Initial Catalog=MMABooks; Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>

Page 13: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 13

The last step of the Wizard

Page 14: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 14

How to work with columns that have default values Omit columns with default values from the dataset unless they’re

needed by the application. Provide values for those columns whenever a row is added to the

dataset.

Page 15: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 15

A project with a dataset defined by a data source

Page 16: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 16

A form with the Products table dragged onto it

Page 17: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 17

The controls and objects that are created when you drag a data source to a form DataGridView control BindingNavigator control BindingSource object DataSet object TableAdapter object TableAdapterManager object

Page 18: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 18

The user interface for the Product Maintenance application

Page 19: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 19

The code that’s generated by Visual Studio private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the // 'mMABooksDataSet.Products' table. // You can move, or remove it, as needed. this.productsTableAdapter.Fill( this.mMABooksDataSet.Products); } private void productsBindingNavigatorSaveItem_Click( object sender, EventArgs e) { this.Validate(); this.productsBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll( this.mMABooksDataSet); }

Page 20: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 20

The syntax of the Fill method tableAdapter.Fill(dataSet.TableName)

The syntax of the UpdateAll method tableAdapterManager.UpdateAll(dataSet)

Page 21: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 21

How to change the default control for a data table

Page 22: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 22

How to change the default control for a column in a data table

Page 23: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 23

A form with the Customers table dragged onto it

Page 24: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 24

The user interface for the Customer Maintenance application

Page 25: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 25

The code for the application private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the // 'mMABooksDataSet.Customers' table. // You can move, or remove it, as needed. this.customersTableAdapter.Fill( this.mMABooksDataSet.Customers); } private void customersBindingNavigatorSaveItem_Click( object sender, EventArgs e) { this.Validate(); this.customersBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll( this.mMABooksDataSet); }

Page 26: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 26

.NET data provider exception classes SqlException OracleException OdbcException OleDbException

Page 27: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 27

Common members of the .NET data provider exception classes Number Message Source Errors GetType()

Page 28: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 28

Code that catches a SQL exception private void Form1_Load(object sender, EventArgs e) { try { this.customersTableAdapter.Fill( this.mMABooksDataSet.Customers); } catch (SqlException ex) { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } }

Page 29: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 29

Common ADO.NET exception classes DBConcurrencyException DataException ConstraintException NoNullAllowedException

Common members of the ADO.NET classes Message GetType()

Page 30: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 30

Code that handles ADO.NET errors try { this.customersBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.mMABooksDataSet); } catch (DBConcurrencyException) { MessageBox.Show("A concurrency error occurred. " + "Some rows were not updated.", "Concurrency Exception"); this.customersTableAdapter.Fill( this.mMABooksDataSet.Customers); } catch (DataException ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); customersBindingSource.CancelEdit(); }

Page 31: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 31

Code that handles ADO.NET errors (cont.) catch (SqlException ex) { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); }

Page 32: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 32

An event of the DataGridView control DataError

Three properties of the DataGridViewDataErrorEventArgs class Exception RowIndex ColumnIndex

Page 33: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 33

Code that handles a data error for a DataGridView control

private void productsDataGridView_DataError( object sender, DataGridViewDataErrorEventArgs e) { int row = e.RowIndex + 1; string errorMessage = "A data error occurred.\n" + "Row: " + row + "\n" + "Error: " + e.Exception.Message; MessageBox.Show(errorMessage, "Data Error"); }

Page 34: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 34

The schema displayed in the Dataset Designer

Page 35: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 35

The Query Builder

Page 36: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 36

The Preview Data dialog box

Page 37: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 37

SQL that retrieves customer rows SELECT CustomerID, Name, Address, City, State, ZipCode FROM Customers

SQL that inserts a customer row and refreshes the dataset INSERT INTO Customers (Name, Address, City, State, ZipCode) VALUES (@Name, @Address, @City, @State, @ZipCode); SELECT CustomerID, Name, Address, City, State, ZipCode FROM Customers WHERE (CustomerID = SCOPE_IDENTITY())

Page 38: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 38

SQL that updates a customer row and refreshes the dataset UPDATE Customers SET Name = @Name, Address = @Address, City = @City, State = @State, ZipCode = @ZipCode WHERE ( (CustomerID = @Original_CustomerID) AND (Name = @Original_Name) AND (Address = @Original_Address) AND (City = @Original_City) AND (State = @Original_State) AND (ZipCode = @Original_ZipCode) ); SELECT CustomerID, Name, Address, City, State, ZipCode FROM Customers WHERE (CustomerID = @CustomerID)

Page 39: C# Tutorial MSM_Murach chapter-18-slides

Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 39

SQL that deletes a customer row DELETE FROM Customers WHERE (CustomerID = @Original_CustomerID) AND (Name = @Original_Name) AND (Address = @Original_Address) AND (City = @Original_City) AND (State = @Original_State) AND (ZipCode = @Original_ZipCode)