C# Tutorial MSM_Murach chapter-18-slides
-
Upload
sami-mut -
Category
Technology
-
view
24 -
download
2
Transcript of 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
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.
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.
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 4
An empty Data Sources window
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 5
A Data Sources window after a data source has been added
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 6
The first step of the Data Source Wizard
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 7
The second step of the Wizard
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 8
The third step of the Wizard
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 9
The Add Connection dialog box
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 10
The Change Data Source dialog box
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 11
The fourth step of the Wizard
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>
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 13
The last step of the Wizard
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.
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 15
A project with a dataset defined by a data source
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 16
A form with the Products table dragged onto it
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
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 18
The user interface for the Product Maintenance application
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); }
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)
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 21
How to change the default control for a data table
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
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 23
A form with the Customers table dragged onto it
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 24
The user interface for the Customer Maintenance application
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); }
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 26
.NET data provider exception classes SqlException OracleException OdbcException OleDbException
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()
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()); } }
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()
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(); }
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()); }
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
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"); }
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 34
The schema displayed in the Dataset Designer
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 35
The Query Builder
Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 36
The Preview Data dialog box
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())
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)
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)