SlideShare a Scribd company logo
1 of 39
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

What's hot

C# Tutorial MSM_Murach chapter-12-slides
C# Tutorial MSM_Murach chapter-12-slidesC# Tutorial MSM_Murach chapter-12-slides
C# Tutorial MSM_Murach chapter-12-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-21-slides
C# Tutorial MSM_Murach chapter-21-slidesC# Tutorial MSM_Murach chapter-21-slides
C# Tutorial MSM_Murach chapter-21-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-17-slides
C# Tutorial MSM_Murach chapter-17-slidesC# Tutorial MSM_Murach chapter-17-slides
C# Tutorial MSM_Murach chapter-17-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slidesC# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-13-slides
C# Tutorial MSM_Murach chapter-13-slidesC# Tutorial MSM_Murach chapter-13-slides
C# Tutorial MSM_Murach chapter-13-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-03-slides
C# Tutorial MSM_Murach chapter-03-slidesC# Tutorial MSM_Murach chapter-03-slides
C# Tutorial MSM_Murach chapter-03-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-19-slides
C# Tutorial MSM_Murach chapter-19-slidesC# Tutorial MSM_Murach chapter-19-slides
C# Tutorial MSM_Murach chapter-19-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-02-slides
C# Tutorial MSM_Murach chapter-02-slidesC# Tutorial MSM_Murach chapter-02-slides
C# Tutorial MSM_Murach chapter-02-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-09-slides
C# Tutorial MSM_Murach chapter-09-slidesC# Tutorial MSM_Murach chapter-09-slides
C# Tutorial MSM_Murach chapter-09-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesC# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-16-slides
C# Tutorial MSM_Murach chapter-16-slidesC# Tutorial MSM_Murach chapter-16-slides
C# Tutorial MSM_Murach chapter-16-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-11-slides
C# Tutorial MSM_Murach chapter-11-slidesC# Tutorial MSM_Murach chapter-11-slides
C# Tutorial MSM_Murach chapter-11-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-22-slides
C# Tutorial MSM_Murach chapter-22-slidesC# Tutorial MSM_Murach chapter-22-slides
C# Tutorial MSM_Murach chapter-22-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-08-slides
C# Tutorial MSM_Murach chapter-08-slidesC# Tutorial MSM_Murach chapter-08-slides
C# Tutorial MSM_Murach chapter-08-slidesSami Mut
 
Chapter 2 — Program and Graphical User Interface Design
Chapter 2 — Program and Graphical User Interface DesignChapter 2 — Program and Graphical User Interface Design
Chapter 2 — Program and Graphical User Interface Designfrancopw
 
Chapter 3 — Program Design and Coding
Chapter 3 — Program Design and Coding Chapter 3 — Program Design and Coding
Chapter 3 — Program Design and Coding francopw
 
Chapter 04
Chapter 04Chapter 04
Chapter 04llmeade
 

What's hot (20)

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

Similar to C# Tutorial MSM_Murach chapter-18-slides

C# Tutorial MSM_Murach chapter-23-slides
C# Tutorial MSM_Murach chapter-23-slidesC# Tutorial MSM_Murach chapter-23-slides
C# Tutorial MSM_Murach chapter-23-slidesSami Mut
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionlorinerba
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versiontrishaavenell
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionlorinerba
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionzacherynicoloro
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionzacherynicoloro
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionlorinerba
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionlorinerba
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionzacherynicoloro
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionlorinerba
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionzacherynicoloro
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionzacherynicoloro
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionzacherynicoloro
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionzacherynicoloro
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionzacherynicoloro
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionzacherynicoloro
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versiontrishaavenell
 

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

C# Tutorial MSM_Murach chapter-23-slides
C# Tutorial MSM_Murach chapter-23-slidesC# Tutorial MSM_Murach chapter-23-slides
C# Tutorial MSM_Murach chapter-23-slides
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 
BIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 versionBIS 220 Final Exam 2015 version
BIS 220 Final Exam 2015 version
 

More from Sami Mut

C# Tutorial MSM_Murach chapter-04-slides
C# Tutorial MSM_Murach chapter-04-slidesC# Tutorial MSM_Murach chapter-04-slides
C# Tutorial MSM_Murach chapter-04-slidesSami Mut
 
chapter 5 Java at rupp cambodia
chapter 5 Java at rupp cambodiachapter 5 Java at rupp cambodia
chapter 5 Java at rupp cambodiaSami Mut
 
chapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodiachapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodiaSami Mut
 
chapter 3 Java at rupp cambodia
chapter 3 Java at rupp cambodiachapter 3 Java at rupp cambodia
chapter 3 Java at rupp cambodiaSami Mut
 
chapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodiachapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodiaSami Mut
 
chapter 1 Java at rupp cambodia
chapter 1 Java at rupp cambodiachapter 1 Java at rupp cambodia
chapter 1 Java at rupp cambodiaSami Mut
 

More from Sami Mut (7)

C# Tutorial MSM_Murach chapter-04-slides
C# Tutorial MSM_Murach chapter-04-slidesC# Tutorial MSM_Murach chapter-04-slides
C# Tutorial MSM_Murach chapter-04-slides
 
MSM_Time
MSM_TimeMSM_Time
MSM_Time
 
chapter 5 Java at rupp cambodia
chapter 5 Java at rupp cambodiachapter 5 Java at rupp cambodia
chapter 5 Java at rupp cambodia
 
chapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodiachapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodia
 
chapter 3 Java at rupp cambodia
chapter 3 Java at rupp cambodiachapter 3 Java at rupp cambodia
chapter 3 Java at rupp cambodia
 
chapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodiachapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodia
 
chapter 1 Java at rupp cambodia
chapter 1 Java at rupp cambodiachapter 1 Java at rupp cambodia
chapter 1 Java at rupp cambodia
 

Recently uploaded

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 

Recently uploaded (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

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)