SlideShare a Scribd company logo
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=localhostsqlexpress;
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)

More Related Content

PPT
C# Tutorial MSM_Murach chapter-24-slides
PPT
C# Tutorial MSM_Murach chapter-20-slides
PPT
C# Tutorial MSM_Murach chapter-14-slides
PPT
C# Tutorial MSM_Murach chapter-05-slides
PPT
C# Tutorial MSM_Murach chapter-01-slides
PPT
C# Tutorial MSM_Murach chapter-25-slides
PPT
C# Tutorial MSM_Murach chapter-07-slides
PPT
C# Tutorial MSM_Murach chapter-06-slides
C# Tutorial MSM_Murach chapter-24-slides
C# Tutorial MSM_Murach chapter-20-slides
C# Tutorial MSM_Murach chapter-14-slides
C# Tutorial MSM_Murach chapter-05-slides
C# Tutorial MSM_Murach chapter-01-slides
C# Tutorial MSM_Murach chapter-25-slides
C# Tutorial MSM_Murach chapter-07-slides
C# Tutorial MSM_Murach chapter-06-slides

What's hot (20)

PPT
C# Tutorial MSM_Murach chapter-12-slides
PPT
C# Tutorial MSM_Murach chapter-21-slides
PPT
C# Tutorial MSM_Murach chapter-17-slides
PPT
C# Tutorial MSM_Murach chapter-10-slides
PPT
C# Tutorial MSM_Murach chapter-13-slides
PPT
C# Tutorial MSM_Murach chapter-03-slides
PPT
C# Tutorial MSM_Murach chapter-19-slides
PPT
C# Tutorial MSM_Murach chapter-02-slides
PPT
C# Tutorial MSM_Murach chapter-09-slides
PPT
C# Tutorial MSM_Murach chapter-15-slides
PPT
C# Tutorial MSM_Murach chapter-16-slides
PPT
C# Tutorial MSM_Murach chapter-11-slides
PPT
C# Tutorial MSM_Murach chapter-22-slides
PPT
C# Tutorial MSM_Murach chapter-08-slides
PDF
Intake 38 8
PDF
Intake 38 9
PDF
Intake 37 9
PPTX
Chapter 2 — Program and Graphical User Interface Design
PPTX
Chapter 3 — Program Design and Coding
PPTX
Chapter 04
C# Tutorial MSM_Murach chapter-12-slides
C# Tutorial MSM_Murach chapter-21-slides
C# Tutorial MSM_Murach chapter-17-slides
C# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-13-slides
C# Tutorial MSM_Murach chapter-03-slides
C# Tutorial MSM_Murach chapter-19-slides
C# Tutorial MSM_Murach chapter-02-slides
C# Tutorial MSM_Murach chapter-09-slides
C# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-16-slides
C# Tutorial MSM_Murach chapter-11-slides
C# Tutorial MSM_Murach chapter-22-slides
C# Tutorial MSM_Murach chapter-08-slides
Intake 38 8
Intake 38 9
Intake 37 9
Chapter 2 — Program and Graphical User Interface Design
Chapter 3 — Program Design and Coding
Chapter 04
Ad

Similar to C# Tutorial MSM_Murach chapter-18-slides (20)

PDF
R Tanenbaum .Net Portfolio
DOCX
SetFocus Portfolio
PPT
Database Connection
PPTX
Datasource in asp.net
PPT
C# Tutorial MSM_Murach chapter-04-slides
PPTX
Portfolio - Operations by Sinisa Maricic.pptx
PPT
the .NET Framework. It provides the claf
PPT
C# Tutorial MSM_Murach chapter-23-slides
PDF
C# .NET Developer Portfolio
PDF
Advanced Programming Using Visual Basic 2008 4th Edition Julia Case Bradley
PPTX
Mi 09 N4 Theodore Columbus Portfolio
PPTX
PATTERNS08 - Strong Typing and Data Validation in .NET
PDF
JDE & Peoplesoft 2 _ Sam Sampathnathan _ Best Practices for Managing Your JD ...
PPTX
Work with data in ASP.NET
PPTX
Data Access Tech Ed India
DOCX
Framework 4
PPT
Windows Mobile 5.0 Data Access And Storage Webcast
PPTX
Mis04
R Tanenbaum .Net Portfolio
SetFocus Portfolio
Database Connection
Datasource in asp.net
C# Tutorial MSM_Murach chapter-04-slides
Portfolio - Operations by Sinisa Maricic.pptx
the .NET Framework. It provides the claf
C# Tutorial MSM_Murach chapter-23-slides
C# .NET Developer Portfolio
Advanced Programming Using Visual Basic 2008 4th Edition Julia Case Bradley
Mi 09 N4 Theodore Columbus Portfolio
PATTERNS08 - Strong Typing and Data Validation in .NET
JDE & Peoplesoft 2 _ Sam Sampathnathan _ Best Practices for Managing Your JD ...
Work with data in ASP.NET
Data Access Tech Ed India
Framework 4
Windows Mobile 5.0 Data Access And Storage Webcast
Mis04
Ad

More from Sami Mut (6)

DOCX
MSM_Time
PPT
chapter 5 Java at rupp cambodia
PPT
chapter 2 Java at rupp cambodia
PPT
chapter 3 Java at rupp cambodia
PPT
chapter 2 Java at rupp cambodia
PPT
chapter 1 Java at rupp cambodia
MSM_Time
chapter 5 Java at rupp cambodia
chapter 2 Java at rupp cambodia
chapter 3 Java at rupp cambodia
chapter 2 Java at rupp cambodia
chapter 1 Java at rupp cambodia

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Spectral efficient network and resource selection model in 5G networks
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation theory and applications.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPT
Teaching material agriculture food technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
Advanced methodologies resolving dimensionality complications for autism neur...
Digital-Transformation-Roadmap-for-Companies.pptx
sap open course for s4hana steps from ECC to s4
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
Building Integrated photovoltaic BIPV_UPV.pdf
Network Security Unit 5.pdf for BCA BBA.
Spectral efficient network and resource selection model in 5G networks
The AUB Centre for AI in Media Proposal.docx
Chapter 3 Spatial Domain Image Processing.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Unlocking AI with Model Context Protocol (MCP)
Encapsulation theory and applications.pdf
Machine learning based COVID-19 study performance prediction
Dropbox Q2 2025 Financial Results & Investor Presentation
The Rise and Fall of 3GPP – Time for a Sabbatical?
NewMind AI Weekly Chronicles - August'25-Week II
Teaching material agriculture food technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
MIND Revenue Release Quarter 2 2025 Press Release

C# Tutorial MSM_Murach chapter-18-slides

  • 1. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 1 Chapter 18 How to work with data sources and datasets
  • 2. 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.
  • 3. 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.
  • 4. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 4 An empty Data Sources window
  • 5. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 5 A Data Sources window after a data source has been added
  • 6. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 6 The first step of the Data Source Wizard
  • 7. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 7 The second step of the Wizard
  • 8. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 8 The third step of the Wizard
  • 9. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 9 The Add Connection dialog box
  • 10. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 10 The Change Data Source dialog box
  • 11. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 11 The fourth step of the Wizard
  • 12. 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=localhostsqlexpress; Initial Catalog=MMABooks; Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
  • 13. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 13 The last step of the Wizard
  • 14. 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.
  • 15. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 15 A project with a dataset defined by a data source
  • 16. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 16 A form with the Products table dragged onto it
  • 17. 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
  • 18. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 18 The user interface for the Product Maintenance application
  • 19. 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); }
  • 20. 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)
  • 21. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 21 How to change the default control for a data table
  • 22. 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
  • 23. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 23 A form with the Customers table dragged onto it
  • 24. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 24 The user interface for the Customer Maintenance application
  • 25. 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); }
  • 26. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 26 .NET data provider exception classes • SqlException • OracleException • OdbcException • OleDbException
  • 27. 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()
  • 28. 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()); } }
  • 29. 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()
  • 30. 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(); }
  • 31. 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()); }
  • 32. 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
  • 33. 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"); }
  • 34. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 34 The schema displayed in the Dataset Designer
  • 35. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 35 The Query Builder
  • 36. Murach’s C# 2010, C1 © 2010, Mike Murach & Associates, Inc. Slide 36 The Preview Data dialog box
  • 37. 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())
  • 38. 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)
  • 39. 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)