0% found this document useful (0 votes)
110 views

Lab Answer Key Module 7 - Accessing A Database

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Lab Answer Key Module 7 - Accessing A Database

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

23/12/2020 Lab Answer Key: Module 7: Accessing a Database

Lab Answer Key: Module 7: Accessing a Database

Lab: Retrieving and Modifying Grade Data

Exercise 1: Creating an Entity Data Model from The School of Fine Arts Database
Es
te
do
Task 1: Build and cgenerate
um an EDM by using a table from the SchoolGradesDB database
No en
to
es pe
tán r
pe ait tene
rm urra ce a
1. Start the MSL-TMG1 itid virtual
@ machine if it is not already running.
as se An
las rnat drés
co u Itu
2. Start the 20483A-SEA-DEV11-07 pia r.cvirtual
l rmachine.
ra
ss Ur
in qu
au iet
3. Log on to Windows® 8 as Student with tor the password a. Pa$$w0rd.
iza
ció
n.

Note: If necessary, click Switch User to display a list of users in the Windows 8 logon screen.

Es
4. Switch totethe
do Windows 8 Start window.
cu
me
5. No e Studionto2012.
Click Visual pe
stá r
np ait tene
6. erm urr ce
In Visual Studio, onitidthe aFile
@ menu,a A point to Open, and then click Project/Solution.
as s ern ndré
las a sI
7. In the Open Project dialog cobox,tubrowse tuto
rra E:\Labfiles\Starter\Exercise 1, click GradesPrototype.sln, and then
pia r.cl
click Open. s sin Ur
qu
au iet
tor a.
iza
8. On the File menu, point to Add, and thencclick ión New Project.
.
9. In the Add New Project dialog box, in the templates list, click Class Library, in the Name box, type
Grades.DataModel, and then click OK.

10. On the Project menu, click Add New Item.


Es
te
do
11. In the Add New cu Item – Grades.DataModel dialog box, in the templates list, click ADO.NET Entity Data
me
No in the Name nto
Model, es pbox,
ert
type GradesModel, and then click Add.
tán en
pe aitu ec
12. In the Entity Data rm Model e a on the Choose Model Contents page, click Generate from database, and
r Wizard,
itid ra@
as se And
then click Next. las rnat rés
co u Itu
pia r.cl rra
s U
13. On the Choose Your Data Connection sin page, rclick
qu New Connection.
iet
au a.
tor
iza
14. If the Choose Data Source dialog box appears, ció in the Data source list, click Microsoft SQL Server, and
n.
then click Continue.

15. In the Connection Properties dialog box, in the Server name box, type (localdb)\v11.0, in the Select or enter
a database name list, click SchoolGradesDB, and then click OK.
Es
te Data Model Wizard, on the Choose Your Data Connection page, click Next.
16. In the Entitydo
cu
me
N nto
17. On theoChoose
es Your pDatabase
ert Objects and Settings page, expand Tables, expand dbo, select the
tán e
following tables, and then click Finish:
https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 1/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database

• Grades

• Students

• Subjects

• Teachers Es
te
do
cu
• Users me
No nto
e stá pe
r
np ait tene
erm urr ce
itid a@ aA
a
18. If the Security Warning s ern box
s la dialog nd appears, click Do not show this message again, and then click OK.
sc atu rés I
op r. tur
ias cl ra
Ur
19. On the Build menu, click Build Solution.
sin qu
au iet
tor a.
iza
ció
n.

Task 2: Review the generated code

1. Es
In the designer
te window, review the entities that have been generated.
do
cu
me
2. Review nto and navigation properties of the Grade entity.
No the properties
es pe
tán r
p ait tene
3. Right-click theeheading
rm u ce Grade entity, and then click Table Mapping.
r of the
itid ra@ aA
as s e n
4. las rnat drés
In the Mapping Details – Grade co pane,
u Ireview
tur the mappings between the columns in the database table and
pia r.cl ra
the properties of the entity. s s Ur
in qu
au iet
tor a.
5. In Solution Explorer, expand GradesModel.edmx, i za expand GradesModel.Context.tt, and then double-click
ció
n.
GradesModel.Context.cs.

6. In the code window, note that the wizard has created a DbContext object named SchoolGradesDBEntities.

7. In Solution Explorer, expand GradesModel.tt, and then double-click Grade.cs.


Es
te
8. Note that the do wizard has created one property for each column in the Grades database table.
cu
me
No nto
e
9. On the File stámenu, clickpe Save All.
r
np ait tene
erm urr ce
ida aClose
10. On the File menu,itclick @ aA
Solution.
s la sern ndré
sc atu sI
op r. tur
ias cl ra
Ur
sin qu
au iet
tor a.
Results: After completing this exercise, theizprototype
ac application should include an EDM that you can use to
access The School of Fine Arts database. ión
.

Exercise 2: Updating Student and Grade Data by Using the Entity Framework
Es
te
do
cu
me
No
Task 1: Display gradesntfor
o p the current student
es ert
tán e

https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 2/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database

1. In Visual Studio, on the File menu, point to Open, and then click Project/Solution.

2. In the Open Project dialog box, browse to E:\Labfiles\Starter\Exercise 2, click GradesPrototype.sln, and then
click Open.

3. In Solution Explorer, right-click GradesPrototype, and then click Set as StartUp Project.

4. On the View menu, click Task List.


Es
te
5. In the Task dList
oc window, in the Categories List, click Comments.
um
No en
6. to
Double-click
es the TODO: pe Exercise 2: Task 1a: Find all the grades for the student. task.
tán r
pe ait tene
7. rm urra ce a
In the code editor,iticlick
da in @ the blank
A line below the comment, and then type the following code:
s la sern ndré
sc a t s
op ur.cl Iturra
ias
List<Grades.DataModel.Grade> sin gradesUr=qu new List<Grades.DataModel.Grade>();
au iet
tor grade in a.
foreach (Grades.DataModel.Grade iza SessionContext.DBContext.Grades)
ció
{ n.
if (grade.StudentUserId == SessionContext.CurrentStudent.UserId)
{
grades.Add(grade);
} Es
te
} do
cu
me
No nto
e stá pe
r
np ait tene
erm urr ce
8. In the Task List window,
itid a@ double-click
aA the TODO: Exercise 2: Task 1b: Display the grades in the
as s e nd
studentGrades ItemsControl las rnby rés databinding. task.
atuusing
co Itu
pia r.cl rra
9. ss Ur
In the code editor, click in the blank in line below quthe comment, and then type the following code:
iet
au a.
tor
iza
ció
n
studentGrades.ItemsSource = grades; .

10. On the Build menu, click Build Solution.


Es
te
11. On the Debug do menu, click Start Without Debugging.
cu
me
No nto
12. When the esapplication ploads,
ert in the Username box, type vallee, and in the Password box, type
tán en
password99,pand a ec Log on.
erm then
itu click
r ea
itid ra@
as se And
13. In the Class 3C view, click rna Liu.
las Kevin ré
co tur s Itu
pia . c l rra
ss Ur
14. Verify that Kevin Liu’s grades areinlisted. qu
au iet
tor a.
iza
15. Note that the subject column uses the subject c ión ID rather than the subject name, and then close the
.
application.

Es
Task 2: Display
te the subject name in the UI
do
cu
me
No nto
es pe
tán rte
1. In Visual Studio, in the Task List window, double-click the TODO: Exercise 2: Task 2a: Convert the subject ID
https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 3/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database

provided in the value parameter. task.

2. In the code editor, click in the blank line below the comment, and then type the following code:

int subjectId = (int)value;


var subject = SessionContext.DBContext.Subjects.FirstOrDefault(s => s.Id ==
subjectId);
Es
te
do
cu
me
3. N nto
In the Task
o e List window, pe double-click the TODO: Exercise 2: Task 2b: Return the subject name or the string
stá r
“N/A”. task.n pe ait tene
rm urra ce a
itid @
as se And
4. In the code editor, delete las thernfollowing
atu rés I line of code:
co tur
pia r.cl ra
ss Ur
in qu
au iet
return value; tor a.
iza
ció
n.

5. In the code editor, click in the blank line below the comment, and then type the following code:

return
E subject.Name != string.Empty ? subject.Name : "N/A";
ste
do
cu
me
No nto
e stá pe
r
np ait tene
erm urr ce
itid a@ aA
as s e nd
las rnview
Task 3: Display the GradeDialog rés use the input to add a new grade
atu and
co Itu
pia r.cl rra
ss Ur
in qu
au iet
tor a.
1. In the Task List window, double-click the i zaTODO: Exercise 2: Task 3a: Use the GradeDialog to get the details
ció
of the new grade. task. n.

2. In the code editor, click in the blank line below the comment, and then type the following code:

Es
GradeDialog gd = new GradeDialog();
t ed
oc
um
No en
to
es pe
In the TasktáList r
3. np ait tedouble-click
window, n the TODO: Exercise 2: Task 3b: Display the form and get the details of
erm urr ece
the new grade. task.itid a@ a
as se An
las rnat drés
4. In the code editor, click incthe ur.
op blank Itu
ias cl line rbelow
ra
Ur
the comment, and then type the following code:
sin qu
au iet
tor a.
iza
if (gd.ShowDialog().Value) ció
n.
{

5. Click in the blank line below the final TODO comment in this try block, and then type the following code:
Es
te
do
cu
} me
No nto
es pe
tán rte

https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 4/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database

6. In the Task List window, double-click the TODO: Exercise 2: Task 3c: When the user closes the form, retrieve
the details of the assessment grade from the form and use them to create a new Grade object. task.

7. In the code editor, click in the blank line below the comment, and then type the following code:

Grades.DataModel.Grade newGrade = new Grades.DataModel.Grade();


newGrade.AssessmentDate = gd.assessmentDate.SelectedDate.Value;
E
ste
newGrade.SubjectId = gd.subject.SelectedIndex;
do
cu
newGrade.Assessment = gd.assessmentGrade.Text;
m en
N
oe t
o p = gd.comments.Text;
newGrade.Comments
s e
tán rte
pe ait
newGrade.StudentUserId
n
ec = SessionContext.CurrentStudent.UserId;
r urmi e
ra@
tid a
as se And
las r na ré
co tur s Itu
pia . cl rra
8. ss
In the Task List window, double-click the TODO: Ur
in qu Exercise 2: Task 3d: Save the grade. task.
au iet
tor a.
9. In the code editor, click in the blank line belowi z ac the comment, and then type the following code:
ión
.

SessionContext.DBContext.Grades.Add(newGrade);
SessionContext.Save();

Es
te
do
c
10. In the Task Listumwindow,
en double-click the TODO: Exercise 2: Task 3e: Refresh the display so that the new
No to
es
grade appears. task. pe
tán r
pe ait tene
rm u rra ce a
11. In the code editor,iticlick
da at @ the endA of the comment, press Enter, and then type the following code:
s la sern ndré
sc at s
op ur.cl Iturra
ias Ur
Refresh(); sin qu
au iet
tor a.
iza
ció
n.

Task 4: Run the application and test the grade-adding functionality


Es
te
do
cu
me
1. On theNoBuild menu,ntoclick Build Solution.
es pe
tán rte
a ne
2. On the Debugpemenu, i
rm tuclick
rra Startce Without Debugging.
itid @ aA
as se n
3. las rin na dUsername

When the application loads, co the tur s Itu box, type vallee, and in the Password box, type
pia . c l rra
password99, and then click Log s s on. Ur
in qu
au iet
tor a.
4. In the Class 3C view, click Kevin Liu. izac
ión
.
5. Verify that the list of grades now displays the subject name, not the subject ID.

6. In the Report Card view, click Add Grade.

7. In the New
Es Grade Details dialog box, in the Subject list, click Geography, in the Assessment box, type
te
do
A+, in the Comments box, type Well done!, and then click OK.
cu
me
No nto
8. Verify that pe is added to the list, and then close the application.
es the new grade
tán rte

https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 5/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database

9. In Visual Studio, on the File menu, click Close Solution.

Results: After completing this exercise, users will be see the grades for the current student and add new
grades.

Es
te
oc d
Exercise 3: Extending
um the Entity Data Model to Validate Data
No en
to
e stá pe
r
np ait tene
erm urr ce
Task 1: Throw the ClassFullException exception
itid a@ a
as se And
las rnat rés
co u Itu
pia r.cl rra
1. In Visual Studio, on the File menu,ss Ur
in point to Open, qu
iet
and then click Project/Solution.
au a.
tor
iza
2. In the Open Project dialog box, browse to ciE:\Labfiles\Starter\Exercise
ón 3, click GradesPrototype.sln, and then
.
click Open.

3. In Solution Explorer, right-click GradesPrototype, and then click Set as StartUp Project.

4.
Es Explorer, right-click Grades.DataModel, point to Add, and then click Class.
In Solution
te
do
5. In the Add New cu Item – Grades.DataModel dialog box, in the Name box, type customTeacher.cs, and then
me
No nto
click Add.
es pe
tán r
pe ait tene
u
rm modify r ce
6. In the code editor, itid ra@the aclass An declaration as shown in the following code:
as s e
las rnat drés
co u Itu
pia r.cl rra
public partial class Teacher s sin Ur
qu
au iet
tor a.
iza
ció
n.
7. In the code editor, in the Teacher class, type the following code:

private const int MAX_CLASS_SIZE = 8;


Es
te
do
cu
me
8. In theNcode editor, ntin the Teacher class, type the following code:
oe op
stá ert
np a en
erm iturr ece
itid a@ a
public void EnrollInClass(Student
as se An student)
las rnat drés
{ co u Itu
pia r.cl rra
// Verify that this teacher's s Ur
class
sin qu is not already full.
au iet
// Determine how many students t ori a.
are currently in the class.
za
ció
int numStudents = (from s in Students n.
where s.TeacherUserId == UserId
select s).Count();
// If the class is already full, another student cannot be enrolled.
if
E (numStudents >= MAX_CLASS_SIZE)
ste
{ do
cu
m
No // Soenthrow
to a ClassFullException and specify the class that is full.
es pe
tthrow
án r te
new ClassFullException("Class full: Unable to enroll student", Class);

https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 6/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database

}
// Verify that the student is not already enrolled in another class.
if (student.TeacherUserId == null)
{
// Set the TeacherID property of the student.
student.TeacherUserId = UserId;
}
Este
else
do
cu
{ me
No nto
es // If the pestudent is already assigned to a class, throw an ArgumentException.
tán r
p ait tene
throw ce
erm newurrArgumentException("Student", "Student is already assigned to a
itid a@ aA
class"); a s
s la ern n d
sc at rés
} op ur.cl Iturra
ias Ur
} sin qu
au iet
tor a.
iza
ció
n.
9. In the Task List window, double-click the TODO: Exercise 3: Task 1a: Call the EnrollInClass method to assign
the student to this teacher’s class. task.

10. In the code editor, click in the blank line below the comment, and then type the following code:
Es
te
do
cu
me
No nto
SessionContext.CurrentTeacher.EnrollInClass(student);
es pe
tán r
pe ait tene
rm u r ce
itid ra@ aA
s
as double-click
e n
11. In the Task List window, las rnat drés the TODO: Exercise 3: Task 1b: Save the updated student/class
co u Itu
information back to the database. pia r.ctask.
l rra
ss Ur
in qu
au iet
12. In the code editor, click in the blank line tor below the a. comment, and then type the following code:
iza
ció
n.

SessionContext.Save();

Es
te
do
cu
me
Novalidation nto
Task 2: Add es logic
pe for the Assessment and AssessmentDate properties
tán r
pe ai tene
rm turra ce a
itid @
as se And
l as rnatGrades.DataModel,
1. In Solution Explorer, right-click ré point to Add, and then click Class.
co ur. s Itu
pia c l rra
ss Ur
2. In the Add New Item – Grades.DataModel in qu
au iet box, in the Name box, type customGrade.cs, and then
dialog
tor a.
click Add. iza
ció
n.
3. In the code editor, modify the class declaration as shown in the following code:

public partial class Grade


Es
te
do
cu
me
4. In theNcode editor,
ntin
o pthe Grade class, type the following code:
oe
stá ert
n e

https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 7/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database

public bool ValidateAssessmentDate(DateTime assessmentDate)


{
// Verify that the user has provided a valid date.
// Check that the date is no later than the current date.
if (assessmentDate > DateTime.Now)
{

Es // Throw an ArgumentOutOfRangeException if the date is after the current


t
date. e do
cu
m
No throwennew to ArgumentOutOfRangeException("Assessment Date", "Assessment date
es pe
must be ton
án or before r the current date");
pe ait tene
r mi u rr ce
} tid a@s a An
as e
else las rnat drés
co u Itu
{ pia r.cl rra
ss Ur
in qu
return true; au iet
tor a.
iza
} ció
n.
}

5. In the code editor, below the existing using directives, type the following code:
Es
te
do
cu
using me
System.Text.RegularExpressions;
No nto
es pe
tán r
pe ait tene
rm u r ce
itid ra@ aA
6. In the code editor, inathe s
s la Grade nd
ern class, type the following code:
sc atu rés I
op r. t urr
ias cl aU
sin rqu
public bool ValidateAssessmentGrade(string au iet
tor a. assessment)
iza
{ ció
n.
// Verify that the grade is in the range A+ to E-.
// Use a regular expression: A single character in the range A-E at the start of
the string followed by an optional + or - at the end of the string.
Match matchGrade = Regex.Match(assessment, @"^[A-E][+-]?$");
E
ifste(!matchGrade.Success)
doc
{ um
No en
to
e
st// If thepegrade
r is not valid, throw an ArgumentOutOfRangeException.
án
p ait tene
e
throw u c
rm new rrArgumentOutOfRangeException("Assessment",
ea "Assessment grade must be
itid a@ An
in the range A+ ato s la s
E-"); e d
r r
s c natu és It
} op r.c urr
ias l aU
s in rqu
else au iet
tor a.
{ iza
ció
return true;
n.

}
}

Es
te
7. In the Task dList
oc window, double-click the TODO: Exercise 3: Task 2a: Create a Grade object. task.
um
No en
to
8. es editor, click
In the code pe in the blank line below the comment, and then type the following code:
tán rte

https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 8/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database

Grades.DataModel.Grade testGrade = new Grades.DataModel.Grade();

9. In the Task List window, double-click the TODO: Exercise 3: Task 2b: Call the ValidateAssessmentDate
method. task.

10. In the code editor, click in the blank line below the comment, and then type the following code:
Es
te
do
um c
testGrade.ValidateAssessmentDate(assessmentDate.SelectedDate.Value);
e
No nto
e stá pe
r
np ait tene
erm urr ce
itid a@
11. In the Task List window, a
as double-click
se And the TODO: Exercise 3: Task 2c: Call the ValidateAssessmentGrade.
r ré
las na
task. co tur s Itu
pia . cl rra
ss Ur
i n qu
12. In the code editor, click in the blankauline below the
iet comment, and then type the following code:
tor a.
iza
ció
n.
testGrade.ValidateAssessmentGrade(assessmentGrade.Text);

Es
te
do
cu
Task 3: Run the me
application and test the validation logic
No nto
es pe
tán r
pe ait tene
rm u r ce
itid ra@ aA
1. On the Build menu, aclick s
s la Build nd
ern Solution.
sc atu rés I
op r.c tur
l
ias Without ra
2. On the Debug menu, click Start sin Debugging.
Ur
qu
au iet
tor a.
3. When the application loads, in the Username i za
ció box, type vallee, and in the Password box, type
password99, and then click Log on. n.

4. When the application loads, click Enroll Student.

5. In the Assign Student dialog box, click Eric Gruber, in the Confirm message box, click Yes, and then in the
Es
Error enrolling
te student message box, click OK.
do
cu
me
6. In theNAssign
oe nto dialog box, click Close.
Student
stá pe
r
np ait tene
7. In the Class 3C erm
view,urclick
r ce
Kevin Liu, and then click Add Grade.
itid a@ a
as se And
8. In the New Grade Details las rnat rés
codialogu box,Itin
pia r.cl urrthe Date box, type tomorrow’s date, and then click OK.
ss aU
i n rqu
9. In the Error creating assessment amessage uto iet click OK.
box, a.
riz
ac
10. In the New Grade Details dialog box, in the iónDate box, type 8/19/2012, in the Assessment box, type F+,
.
and then click OK.

11. In the Error creating assessment message box, click OK.

12. In the New


Es Grade Details dialog box, in the Assessment box, type A+, in the Comments box, type Well
te
done!, anddthen
oc click OK.
um
No en
to
es the new grade
13. Verify that pe is added to the list, and then close the application.
tán rte

https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 9/10
23/12/2020 Lab Answer Key: Module 7: Accessing a Database

14. In Visual Studio, on the File menu, click Close Solution.

Results: After completing this exercise, the application will raise and handle exceptions when invalid data is
entered.

Es
te
do
cu
me
No nto
e stá pe
r
np ait tene
erm urr ce
itid a@ a
as se And
las r na ré
co tur s Itu
pia . cl rra
ss Ur
in qu
au iet
tor a.
iza
ció
n.

Es
te
do
cu
me
No nto
e stá pe
r
np ait tene
erm urr ce
itid a@ aA
as s e n
las rnat drés
co u Itu
pia r.cl rra
ss Ur
in qu
au iet
tor a.
iza
ció
n.

Es
te
do
cu
me
No nto
e stá pe
r
np ait tene
erm urr ce
itid a@ a
as se An
las rnat drés
co u Itu
pia r.cl rra
ss Ur
in qu
au iet
tor a.
iza
ció
n.

Es
te
do
cu
me
No nto
e stá pe
n rte

https://www.skillpipe.com/#/reader/book/b449cdd9-afbb-4104-8a9b-297d55063872 10/10

You might also like