CSC412 - Visual Programming
CSC412 - Visual Programming
CSC412 - Visual Programming
LAB
MANUAL
Course: CSC412 – Visual Programming
Learning Procedure
1) Stage J (Journey inside-out the concept)
2) Stage a1 (Apply the learned)
Table of Contents
ii
LAB # 01
Statement Purpose:
This lab will give you an introduction to Visual Studio environment and developing first
Console based application using C# language.
Activity Outcomes:
This lab teaches you the following topics:
Instructor Note:
Download and Install Visual Studio 2013 or above. In this lab, Visual Studio 2013 is used.
1) Stage J (Journey)
Introduction
Visual Studio is a complete set of development tools for building ASP.NET Web
applications, XML Web Services, desktop applications, and mobile applications. Visual
Basic, Visual C#, and Visual C++ all use the same integrated development e nvironment
(IDE), which enables tool sharing and eases the creation of mixed-language solutions. In
addition, these languages use the functionality of the .NET Framework, which provides
access to key technologies that simplify the development of ASP Web applications and
XML Web Services.
Console applications are typically designed without a graphical user interface (GUI) and
are compiled into an executable file. You interact with a console application by typing
instructions at the command prompt.
2) Stage a1 (apply)
Lab Activities:
Activity 1:
Visual Studio environment should be customized for C# Language. By doing this, C#
based tools and options would be more visible. Existing Visual Studio environment
settings can also be exported.
Solution:
Activity 2:
Developing first console based application in Visual Studio using .net framework.
Solution:
To create a new project, Go to File > New > Project or Press Ctrl+Shift+N.
A popup will appear showing multiple options. From left tree of the popup, select
Windows under Templates > Visual C#. On right side of the popup, type of Windows
based projects are shown which can be developed using Visual C#. A .net framework
version can be selected from drop down menu in popup.
Select Console Application, choose name of project, location where project will be
created and project Solution name. Press OK to create the project.
A new console based application will be created from template. Solution Explorer shows
the project file and references structure. App.config file contains the information about
the configuration of the project solution. Program.cs file contains the code which will
be written by developer.
Compile and run application by clicking Start button or Debug > Start Debugging or
press F5.
For now, a blank output screen will appear. In some Visual Studio configurations, output
will stop. If it does not stop then write a code line Console.ReadKey(); inside Main
method. By writing this line of code, console will stop to take key input from keyboard.
Activity 3:
Writing first “Hello World” console based application using Visual C#.
Solution:
The following procedure creates a C# version of the traditional "Hello World!" program.
The program displays the string Hello World!
If Program.cs isn't open in the Code Editor, open the shortcut menu for Program.cs in
Solution Explorer, and then choose View Code.
The first line contains a comment. The characters // convert the rest of the line to a
comment.
Main Method
A C# console application must contain a Main method, in which control starts and ends.
The Main method is where you create objects and execute other methods.
The Main method is a static (C# Reference) method that resides inside a class or a
struct. In the previous "Hello World!" example, it resides in a class named Hello. You can
declare the Main method in one of the following ways:
Or
The parameter of the Main method, args, is a string array that contains the command-line
arguments used to invoke the program. Unlike in C++, the array does not include the
name of the executable (exe) file.
The call to ReadKey at the end of the Main method prevents the console window from
closing before you have a chance to read the output when you run your program in
debug mode, by pressing F5.
C# programs generally use the input/output services provided by the run-time library
of the .NET Framework. The statement System.Console.WriteLine("Hello World!");
uses the WriteLine method. This is one of the output methods of the Console class in the
run-time library. It displays its string parameter on the standard output stream
followed by a new line. Other Console methods are available for different input and
output operations. If you include the using System; directive at the beginning of the
program, you can directly use the System classes and methods without fully qualifying
them. For example, you can call Console.WriteLine instead of System.Console.WriteLine:
using System;
Console.WriteLine("Hello World!");
Activity 4:
The Main method is the entry point of a C# console application or windows application.
(Libraries and services do not require a Main method as an entry point.). When the
application is started, the Main method is the first method that is invoked.
Note: There can only be one entry point in a C# program. If you have more than one
class that has a Main method, you must compile your program with the /main compiler
option to specify which Main method to use as the entry point
Solution:
Following example prints the number of command line arguments on console.
class TestClass
{
static void Main(string[] args)
{
// Display the number of command line arguments:
System.Console.WriteLine(args.Length);
}
}
To pass command line arguments to the program, it can be done while executing the
program from command line. Each argument should be separated by a space.
Command line arguments can also be specified in project properties. To open project
properties, go to Project > ProjectName Properties or right click on Project name in
Solution Explorer and click on Properties or press Alt+Enter
Array indexes can be used to access the contents of command line arguments.
Following example will print the contents of command line arguments on console:
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
Console.WriteLine(args[0]);
Console.WriteLine(args[1]);
}
Console.ReadKey();
}
}
arg1
arg2
Another approach to iterating over the array is to use the foreach statement as shown in
this example. The foreach statement can be used to iterate over an array, a .NET
Framework collection class, or any class or struct that implements the IEnumerable
interface.
class CommandLine2
{
static void Main(string[] args)
{
Console.WriteLine("Number of command line parameters = {0}",
args.Length);
This example displays the command line arguments passed to a command -line
application. The output shown is for the first entry in the table above.
class CommandLine
{
static void Main(string[] args)
{
// The Length property provides the number of array elements
Console.WriteLine("parameter count = {0}", args.Length);
3) Stage v (verify)
Home Activities:
Activity 1:
Develop a console based application using Visual C# to print your name, registration
number and address on console.
Activity 2:
Develop a console based application which takes command-line arguments and print
them on console.
consoleApp.exe a b c "a"
"b"
"c"
Activity 3:
Console.ReadLine method reads the next line of characters from the standard input
stream. Read following article at MSDN and practice using ReadLine method:
https://msdn.microsoft.com/en-us/library/system.console.readline
LAB # 02
Statement Purpose:
This lab will give you an introduction of working with Arrays, Multi-Dimensional Arrays,
Jagged Arrays, Strings, Arrays of type String.
Activity Outcomes:
This lab teaches you the following topics:
Instructor Note:
Download and Install Visual Studio 2013 or above. In this lab, Visual Studio 2013 is used.
1) Stage J (Journey)
Introduction
Arrays
You can store multiple variables of the same type in an array data structure . You declare
an array by specifying the type of its elements.
type[] arrayName;
Strings
CCS412 –Lab Manual 13
2) Stage a1 (apply)
Lab Activities:
Activity 1:
Create single-dimensional, multidimensional, and jagged arrays.
Solution:
class TestArraysClass
{
static void Main()
{
// Declare a single-dimensional array
int[] array1 = new int[5];
// Alternative syntax
int[] array3 = { 1, 2, 3, 4, 5, 6 };
Activity 2:
C# also provides the foreach statement. This statement provides a simple, clean way to
iterate through the elements of an array or any enumerable collection. The foreach
statement processes elements in the order returned by the array or collection type’s
enumerator, which is usually from the 0th element to the last.
CCS412 –Lab Manual 14
Creates an array called numbers and iterates through it with the foreach statement.
Solution:
Single Dimension Array:
Activity 3:
Arrays can be passed as arguments to method parameters. Because arrays are reference
types, the method can change the value of the elements.
Initialize an array of strings which represents days of week (i.e. Sun, Mon, Tue … Sat)
and pass as an argument to a PrintArray. The method displays the elements of the array.
Create methods ChangeArray and ChangeArrayElement change the array elements.
ChangeArray method reverse the array elements so that Sat comes at 0 index.
ChangeArrayElement method changes the first three index of the array so that Sat
comes at 0 index, Fri comes at 1st index and Thu comes at 2nd index.
Solution:
class ArrayClass
{
static void PrintArray(string[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " :
"");
}
Console.WriteLine();
}
PrintArray(weekDays);
}
}
// Output:
// Sun Mon Tue Wed Thu Fri Sat
// arr[0] is Sat in ChangeArray.
// Array weekDays after the call to ChangeArray:
// Sun Mon Tue Wed Thu Fri Sat
//
// arr[0] is Sat in ChangeArrayElements.
// Array weekDays after the call to ChangeArrayElements:
// Sat Fri Thu Wed Thu Fri Sat
Activity 4:
A jagged array is an array whose elements are arrays. The elements of a jagged array
can be of different dimensions and sizes. A jagged array is sometimes called an "array of
arrays." The following examples show how to declare, initialize, and access jagged
arrays.
Before you can use jaggedArray, its elements must be initialized. You can initialize the
elements like this:
It is also possible to use initializers to fill the array elements with values, in which case
you do not need the array size. For example:
You can use the following shorthand form. Notice that you cannot omit the new
operator from the elements initialization because there is no default initialization for
the elements:
int[][] jaggedArray3 =
{
new int[] {1,3,5,7,9},
Activity 5:
Build an array whose elements are themselves arrays (Jagged array). Each one of the
array elements has a different size. After creating jagged array, display array elements.
Solution:
class ArrayTest
{
static void Main()
{
// Declare the array of two elements:
int[][] arr = new int[2][];
Activity 6:
You can instantiate a String object in the following ways:
By assigning a string literal to a String variable. This is the most commonly used method
for creating a string. The following example uses assignment to create several strings.
Note that in C#, because the backslash (\) is an escape character, literal backslashes in a
string must be escaped or the entire string must be @-quoted.
Activity 7:
By retrieving a property or calling a method that returns a string. The following
example uses the methods of the String class to extract a substring from a larger string.
Activity 8:
By calling a formatting method to convert a value or object to its string representation.
The following example uses the composite formatting feature to embed the string
representation of two objects into a string.
3) Stage v (verify)
Home Activities:
Activity 1:
Initialize two-dimensional array of integers and passed to the Print2DArray method.
The method displays the elements of the array.
Activity 2:
Create a method named Merger which takes array of type string as an argument. It
combines all elements of string into one string and returns it.
For example:
string [] s = { "hello ", "and ", "welcome ", "to ", "this ", "demo! " };
Merger(s) returns “hello and welcome to this demo!”
Activity 3:
Write a method which takes string as an argument and extracts all words of length
between 4 to 5 and contains vowels in it. Method returns an array of type string
containing words which satisfied above criteria.
LAB # 03
Statement Purpose:
You will learn exception handling, classes, objects and inheritance in this lab.
The C# language's exception handling features help you deal with any unexpected or
exceptional situations that occur when a program is running.
Inheritance, together with encapsulation and polymorphism, is one of the three primary
characteristics (or pillars) of object-oriented programming.
Activity Outcomes:
This lab teaches you the following topics:
• Exception Handling
• Debugging
• Classes, Objects and Inheritance
1) Stage J (Journey)
Introduction
Exception Handling
Exception handling uses the try, catch, and finally keywords to try actions that may not
succeed, to handle failures when you decide that it is reasonable to do so, and to clean
up resources afterward. Exceptions can be generated by the common language runtime
(CLR), by the .NET Framework or any third-party libraries, or by application code.
Exceptions are created by using the throw keyword.
In many cases, an exception may be thrown not by a method that your code has called
directly, but by another method further down in the call stack. When this happens, the
CLR will unwind the stack, looking for a method with a catch block for the specific
exception type, and it will execute the first such catch block that if finds. If it finds no
appropriate catch block anywhere in the call stack, it will terminate the process and
display a message to the user.
Inheritance
Inheritance enables you to create new classes that reuse, extend, and modify the
behavior that is defined in other classes. The class who se members are inherited is
called the base class, and the class that inherits those members is called the derived
class. A derived class can have only one direct base class. However, inheritance is
transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC
inherits the members declared in ClassB and ClassA.
Conceptually, a derived class is a specialization of the base class. For example, if you
have a base class Animal, you might have one derived class that is named Mammal and
another derived class that is named Reptile. A Mammal is an Animal, and a Reptile is
an Animal, but each derived class represents different specializations of the base class.
When you define a class to derive from another class, the derived class implicitly gains
all the members of the base class, except for its constructors and destructors. The
derived class can thereby reuse the code in the base class without having to re -
implement it. In the derived class, you can add more members. In this manner, the
derived class extends the functionality of the base class.
2) Stage a1 (apply)
Lab Activities:
Activity 1:
In this example, a method tests for division by zero and catches the error. Without the
exception handling, this program would terminate with a DivideByZeroException
was unhandled error.
class ExceptionTest
{
static double SafeDivision(double x, double y)
{
if (y == 0)
throw new System.DivideByZeroException();
return x / y;
}
static void Main()
{
// Input for test purposes. Change the values to see
// exception handling behavior.
double a = 98, b = 0;
double result = 0;
try
{
result = SafeDivision(a, b);
Console.WriteLine("{0} divided by {1} = {2}", a, b,
result);
}
catch (DivideByZeroException e)
{
Console.WriteLine("Attempted divide by zero.");
}
}
}
Activity 2:
CCS412 –Lab Manual 22
After an exception is thrown, the runtime checks the current statement to see whether
it is within a try block. If it is, any catch blocks associated with the try block are checked
to see whether they can catch the exception. Catch blocks typically specify exception
types; if the type of the catch block is the same type as the exception, or a base class of
the exception, the catch block can handle the method. For example:
If the statement that throws an exception is not within a try block or if the try block that
encloses it has no matching catch block, the runtime checks the calling method for a try
statement and catch blocks. The runtime continues up the calling stack, searching for a
compatible catch block. After the catch block is found and executed, control is passed to
the next statement after that catch block.
A try statement can contain more than one catch block. The first catch statement that
can handle the exception is executed; any following catch statements, even if they are
compatible, are ignored. Therefore, catch blocks should always be ordered from most
specific (or most-derived) to least specific. For example:
System.Console.WriteLine("Done");
}
CCS412 –Lab Manual 23
Before the catch block is executed, the runtime checks for finally blocks. Finally blocks
enable the programmer to clean up any ambiguous state that could be left over from an
aborted try block, or to release any external resources (such as graphics handles,
database connections or file streams) without waiting for the garbage collector in the
runtime to finalize the objects
Activity 3:
The following illustration shows a class WorkItem that represents an item of work in
some business process. Like all classes, it derives from System.Object and inherits all its
methods. WorkItem adds five members of its own. These include a constructor, because
constructors are not inherited. Class ChangeRequest inherits from WorkItem and
represents a particular kind of work item. ChangeRequest adds two more members to
the members that it inherits from WorkItem and from Object. It must add its own
constructor, and it also adds originalItemID. Property originalItemID enables
the ChangeRequest instance to be associated with the original WorkItem to which the
change request applies.
Class inheritance
The following example shows how the class relationships demonstrated in the previous
illustration are expressed in C#. The example also shows how WorkItem overrides the
virtual method Object.ToString , and how the ChangeRequest class inherits
the WorkItem implementation of the method.
Solution:
// WorkItem implicitly inherits from the Object class.
public class WorkItem
{
// Static field currentID stores the job ID of the last WorkItem that
//Properties.
protected int ID { get; set; }
protected string Title { get; set; }
protected string Description { get; set; }
protected TimeSpan jobLength { get; set; }
// Method Update enables you to update the title and job length of an
// existing WorkItem object.
public void Update(string title, TimeSpan joblen)
{
this.Title = title;
this.jobLength = joblen;
}
// constructor.
class Program
{
static void Main()
{
// Create an instance of WorkItem by using the constructor in the
// base class that takes three arguments.
WorkItem item = new WorkItem("Fix Bugs",
"Fix all bugs in my code branch",
new TimeSpan(3, 4, 0, 0));
3) Stage v (verify)
Home Activities:
Activity 1:
Write C# code for the given scenario: Consider a class named Person having name (of
type string) as instance variable. Student class is a derived class of Person class having
following instance variables:
Activity 2:
Write C# for the given scenario:
Activity 3:
LAB # 04
Statement Purpose:
You will learn about implementing interfaces, indexers, abstract classes and operator
overloading in this lab.
The C# language's exception handling features help you deal with any unexpected or
exceptional situations that occur when a program is running.
Inheritance, together with encapsulation and polymorphism, is one of the three primary
characteristics (or pillars) of object-oriented programming.
Activity Outcomes:
This lab teaches you the following topics:
• Interfaces
• Indexers
• Abstract Classes
• Operator Overloading
1) Stage J (Journey)
Introduction
An interface contains definitions for a group of related functionalities that a class or a
struct can implement.
By using interfaces, you can, for example, include behavior from multiple sources in a
class. That capability is important in C# because the language doesn't support multiple
inheritance of classes. In addition, you must use an interface if you want to simulate
inheritance for structs, because they can't actually inherit from another struct or class.
Abstract classes are closely related to interfaces. They are classes that cannot be
instantiated, and are frequently either partially implemented, or not at all implemented.
One key difference between abstract classes and interfaces is that a class may
implement an unlimited number of interfaces, but may inherit from only one abstract
(or any other kind of) class. A class that is derived from an abstract class may still
implement interfaces. Abstract classes are useful when creating components because
they allow you specify an invariant level of functionality in some methods, but leave the
implementation of other methods until a specific implementation of that class is needed.
They also version well, because if additional functionality is needed in derived classes, it
can be added to the base class without breaking code.
An indexer allows an object to be indexed such as an array. When you define an indexer
for a class, this class behaves similar to a virtual array. You can then access the instance
of this class using the array access operator ([ ]).
2) Stage a1 (apply)
Lab Activities:
Activity 1:
The abstract keyword enables you to create classes and class members that are
incomplete and must be implemented in a derived class.
Classes can be declared as abstract by putting the keyword abstract before the class
definition. For example:
Abstract classes may also define abstract methods. This is accomplished by adding the
keyword abstract before the return type of the method. For example:
Activity 2:
Abstract methods have no implementation, so the method definition is followed b y a
semicolon instead of a normal method block. Derived classes of the abstract class must
implement all abstract methods. When an abstract class inherits a virtual method from a
base class, the abstract class can override the virtual method with an abstra ct method.
For example:
public class F : E
{
public override void DoWork(int i)
{
// New implementation.
}
}
If a virtual method is declared abstract, it is still virtual to any class inheriting from
the abstract class. A class inheriting an abstract method cannot access the original
implementation of the method—in the previous example, DoWork on class F cannot
call DoWork on class D. In this way, an abstract class can force derived classes to pr ovide
new method implementations for virtual methods.
Activity 3:
The sealed keyword enables you to prevent the inheritance of a class or certain class
members that were previously marked virtual.
Classes can be declared as sealed by putting the keyword sealed before the class
definition. For example:
A sealed class cannot be used as a base class. For this reason, it cannot also be an
abstract class. Sealed classes prevent derivation. Because they can never be used as a
base class, some run-time optimizations can make calling sealed class members slightly
faster.
public class D : C
{
public sealed override void DoWork() { }
Activity 4:
You define an interface by using the interface keyword, as the following example shows.
interface IEquatable<T>
{
bool Equals(T obj);
}
Any class or struct that implements the IEquatable<T> interface must contain a
definition for an Equals method that matches the signature that the interface specifies.
As a result, you can count on a class that implements IEquatable<T> to contain an
Equals method with which an instance of the class can deter mine whether it's equal to
another instance of the same class.
Activity 5:
CCS412 –Lab Manual 32
If a class implements two interfaces that contain a member with the same signature,
then implementing that member on the class will cause both interfaces to use that
member as their implementation. In the following example, all the calls to Paint invoke
the same method.
class Test
{
static void Main()
{
SampleClass sc = new SampleClass();
IControl ctrl = (IControl)sc;
ISurface srfc = (ISurface)sc;
interface IControl
{
void Paint();
}
interface ISurface
{
void Paint();
}
class SampleClass : IControl, ISurface
{
// Both ISurface.Paint and IControl.Paint call this method.
public void Paint()
{
Console.WriteLine("Paint method in SampleClass");
}
}
// Output:
// Paint method in SampleClass
// Paint method in SampleClass
// Paint method in SampleClass
If the two interface members do not perform the same function, however, this can lead
to an incorrect implementation of one or both of the interfaces. It is possible to
implement an interface member explicitly—creating a class member that is only called
through the interface, and is specific to that interface. This is accomplished by naming
the class member with the name of the interface and a period. For example:
The class member IControl.Paint is only available through the IControl interface, and
ISurface.Paint is only available through ISurface. Both method implementations are
separate, and neither is available directly on the class. For example:
IControl c = (IControl)obj;
c.Paint(); // Calls IControl.Paint on SampleClass.
ISurface s = (ISurface)obj;
s.Paint(); // Calls ISurface.Paint on SampleClass.
// Output:
// IControl.Paint
// ISurface.Paint
Activity 6:
Suppose you have a class named TempRecord that represents the temperature in
Farenheit as recorded at 10 different times during a 24 hour period. The class contains
an array named "temps" of type float to represent the temperatures, and a DateTime that
represents the date the temperatures were recorded. By implementing an indexer in
this class, clients can access the temperatures in a TempRecord instance as float temp
= tr[4] instead of as float temp = tr.temps[4] . The indexer notation not only
simplifies the syntax for client applications; it also makes the class and its purpose more
intuitive for other developers to understand.
The following example shows how to declare a private array field, temps, and an
indexer. The indexer enables direct access to the instance tempRecord[i]. The
alternative to using the indexer is to declare the array as a public member and access its
members, tempRecord.temps[i], directly.
class TempRecord
{
// Array of temperature values
private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F,
58.8F, 61.3F, 65.9F, 62.1F, 59.2F, 57.5F };
// Indexer declaration.
// If index is out of range, the temps array will throw the exception.
public float this[int index]
{
get
{
return temps[index];
}
set
{
temps[index] = value;
}
}
}
class MainClass
{
static void Main()
{
TempRecord tempRecord = new TempRecord();
// Use the indexer's set accessor
tempRecord[3] = 58.3F;
tempRecord[5] = 60.1F;
}
}
/* Output:
Element #0 = 56.2
Element #1 = 56.7
Element #2 = 56.5
Element #3 = 58.3
Element #4 = 58.8
Element #5 = 60.1
Element #6 = 65.9
Element #7 = 62.1
Element #8 = 59.2
Element #9 = 57.5
*/
Activity 7:
Indexers allow instances of a class or struct to be indexed just like arrays. Indexers
resemble properties except that their accessors take parameters.
In the following example, a generic class is defined and provided with simple get and set
accessor methods as a means of assigning and retrieving values. The Program class
creates an instance of this class for storing strings.
class SampleCollection<T>
CCS412 –Lab Manual 35
{
// Declare an array to store the data elements.
private T[] arr = new T[100];
Activity 8:
You can redefine or overload most of the built-in operators available in C#. Thus a
programmer can use operators with user-defined types as well. Overloaded operators
are functions with special names the keyword operator followed by the symbol for the
operator being defined. similar to any other function, an overloaded operator has a
return type and a parameter list.
The above function implements the addition operator (+) for a user -defined class Box. It
adds the attributes of two Box objects and returns the resultant Box object.
using System;
namespace OperatorOvlApplication
{
class Box
{
private double length; // Length of a box
private double breadth; // Breadth of a box
private double height; // Height of a box
class Tester
{
static void Main(string[] args)
{
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box(); // Declare Box2 of type Box
Box Box3 = new Box(); // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
CCS412 –Lab Manual 37
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);
// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
// volume of box 3
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result:
3) Stage v (verify)
Home Activities:
Activity 1:
Declare a class named DayCollection that stores the days of the week. Declare a get
accessor that takes a string, the name of a day, and returns the corresponding integer.
For example, Sunday will return 0, Monday will return 1, and so on. Create indexer in
DayCollection. Following is a demo class.
class Program
{
static void Main(string[] args)
{
DayCollection week = new DayCollection();
System.Console.WriteLine(week["Fri"]);
// Raises ArgumentOutOfRangeException
System.Console.WriteLine(week["Made-up Day"]);
}
// Output: 5
Activity 2:
Extend the functionality of Box class to overload some more operators. Assume
following driver class and overload all used operators. Consider that the bo x is greater
or smaller than other box by comparing all of its dimensions (i.e. length, breadth and
height).
class Tester
{
static void Main(string[] args)
{
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box(); // Declare Box2 of type Box
Box Box3 = new Box(); // Declare Box3 of type Box
Box Box4 = new Box();
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);
// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
// volume of box 3
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);
if (Box1 != Box2)
Console.WriteLine("Box1 is not equal to Box2");
else
Console.WriteLine("Box1 is not greater or equal to Box2");
Box4 = Box3;
if (Box3 == Box4)
Console.WriteLine("Box3 is equal to Box4");
else
Console.WriteLine("Box3 is not equal to Box4");
Console.ReadKey();
}
}
}
LAB # 05
Statement Purpose:
Purpose of this lab is to familiarize the students with Window Presentation Foundation
applications and to introduce them to common controls used in WPF applications.
Activity Outcomes:
This lab teaches you the following topics:
2) StageJ(Journey)
Introduction
Extensible Application Markup Language, or XAML (pronounced "zammel"), is an XML-
based markup language developed by Microsoft. XAML is the language behind the visual
presentation of an application that you develop, just as HTML is the language behind the
visual presentation of a Web page. Creating an application means writing XAML code,
either by hand or visually by working in the Design view.
WPF is based on managed code but uses a markup language, Extensible Application
Markup Language (XAML), to make building applications much easier for designers.
XAML-based applications currently support C# and Microsoft Visual Basic .NET. If you
write a WPF application entirely in procedural code, you can use any common language
runtime (CLR) language.
Windows Presentation Foundation (WPF) allows developers to easily build and create
visually enriched UI based applications.
3) Stage a1 (apply)
Lab Activities:
Activity 1:
The first task is the creation of a simple WPF application and familiarize with the
environment. Click on File > New > Project menu option.
• Under Templates, select Visual C# and in the middle panel, select WPF Application.
• Give the project a name. Type HelloWorld in the name field and click the OK
button.
• By default, two files are created, one is the XAML file (mainwindow.xaml) and the
other one is the CS file (mainwindow.cs)
• On mainwindow.xaml, you will see two sub-windows, one is the design window
and the other one is the source (XAML) window.
• In WPF application, there are two ways to design an UI for your application. One
is to simply drag and drop UI elements from the toolbox to the Design Window.
The second way is to design your UI by writing XAML tags for UI elements. Visual
Studio handles XAML tags when drag and drop feature is used for UI designing.
• In mainwindow.xaml file, the following XAML tags are written by default.
• When you look at the source window, you will see that Visual Studio has generated
the XAML code of the TextBlock for you.
• Let’s change the Text property of TextBlock in XAML code from TextBlock to Hello
World.
Now, you will see the change on the Design Window as well.
When the above code is compiled and executed, you will see the following window.
Activity 2:
Radio Button and TextBlock
Drag five radio buttons and four text blocks from the Toolbox and arrange them as shown
in the following XAML code.
The following example shows how to use a RadioButton. We will display two groups of
RadioButton. When a user selects an option, then the program will display the message
on a TextBlock. Here is the XAML code.
<Grid>
<TextBlock x:Name = "textBlock" HorizontalAlignment = "Left"
Margin = "23,68,0,0" TextWrapping = "Wrap" Text = "Gender:” />
namespace WPFRadioButtonControl {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public MainWindow() {
InitializeComponent();
}
}
}
4) Stage v (verify)
Home Activities:
Activity 1:
Write a program that visualize which one of the items collection are checked.
Activity 2:
Write a program that shows a ComboBox with various elements added to its Items. For
example – add text, ellipse and picture.
5) Stagea2(assess)
LAB # 06
Lab Sessional 1
LAB # 07
Statement Purpose:
Purpose of this lab is to familiarize the students with common controls of WPF applications.
Activity Outcomes:
This lab teaches you the following topics:
3) StageJ(Journey)
Introduction
Image controls are used to display external images in WPF Application. Grid is a
powerful designing feature when designing WPF Application. It is visible for designing
purposes and has features such as snap to grid, aligning controls etc.
4) Stage a1 (apply)
Lab Activities:
Activity 1:
Image and Grid:
First divide the screen into two rows by using Grid.RowDefinition. Drag three Image
controls from the Toolbox. The following example shows three images. The first one is a
simple image; in the second image, opacity property is set; and in the third image, one
Eclipse is painted with an ImageBrush.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height = "1*"/>
<RowDefinition Height = "1*"/>
</Grid.RowDefinitions>
When you compile and execute the above code, it will produce the following window.
Activity 2:
CheckBox and TextBox
Drag two checkboxes and two textboxes from a toolbox and set the following properties
in the properties window.Now switch to XAML window in which you will see the XAML
tags for check boxes and text boxes.
Add some more properties and selection event as shown in the following XAML code.
<Grid>
<CheckBox x:Name = "checkBox1" Content = "Two States" HorizontalAlignment = "Left"
Margin = "80,70,0,0" VerticalAlignment = "Top" Checked = "HandleCheck"
Unchecked = "HandleUnchecked" Width = "90"/>
</Window>
using System.Windows;
using System.Windows.Controls;
namespace WPFCheckBoxControl
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public MainWindow() {
InitializeComponent(); }
}
}
Activity 3:
Combo Boxes and Text Boxes
Drag two comboboxes and two textboxes from a toolbox and set the following
properties in the properties window.
<Grid>
<ComboBox x:Name = "comboBox" HorizontalAlignment = "Left"
Margin = "80,53,0,0" VerticalAlignment = "Top" Width = "120"
SelectionChanged = "Combo_SelectionChanged">
<ComboBoxItem Content = "Item #1" />
<ComboBoxItem Content = "Item #2" />
<ComboBoxItem Content = "Item #3" />
</ComboBox>
</Grid>
</Window>
Here is the C# code in which the selection changed events are implemented.
using System.Windows;
using System.Windows.Controls;
namespace WPFComboBoxControl {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent(); }
When you compile and execute the above code, it will produce the following window.
When you select an item, it will be displayed on the textbox.
6) Stage v (verify)
CCS412 –Lab Manual 55
Home Activities:
Activity 1:
Create a new window called BringBackPocoyo, to contain a Border control which in turn
contains a horizontal StackPanel control.
Add border and image controls to this window to get the following:
LAB # 08
Statement Purpose:
Purpose of this lab is to familiarize the students with Dependency and Attached Property in
WPF Applications.
Activity Outcomes:
This lab teaches you the following topics:
• Dependency Property
• Attached Property
4) StageJ(Journey)
Introduction
Windows Presentation Foundation (WPF) provides a set of services that can be used to
extend the functionality of a common language runtime (CLR) property. Collectively,
these services are typically referred to as the WPF property system. A property that is
backed by the WPF property system is known as a dependency property. This overview
describes the WPF property system and the capabilities of a dependency property. This
includes how to use existing dependency properties in XAML and in code. This overview
also introduces specialized aspects of dependency properties, such as dependency
property metadata, and how to create your own dependency property in a custom class.
The main difference is, that the value of a normal .NET property is read directly from a
private member in your class, whereas the value of a DependencyProperty is resolved
dynamically when calling the GetValue() method that is inherited from
DependencyObject.
When you set a value of a dependency property it is not stored in a field of your
object, but in a dictionary of keys and values provided by the base
class DependencyObject. The key of an entry is the name of the property and the value is
the value you want to set.
The advantages of dependency properties are
in the instance. The default values are stored once within the dependency
property.
• Value inheritance
When you access a dependency property the value is resolved by using a value
resolution strategy. If no local value is set, the dependency property navigates up
the logical tree until it finds a value. When you set the FontSize on the root
element it applies to all textblocks below except you override the value.
• Change notification
Dependency properties have a built-in change notification mechanism. By
registering a callback in the property metadata you get notified, when the value
of the property has been changed. This is also used by the databinding.
Every time you access a dependency property, it internally resolves the value by
following the precedence from high to low. It checks if a local value is available, if not if a
custom style trigger is active and continues until it founds a value. At last the default
value is always available.
5) Stage a1 (apply)
Lab Activities:
Activity 1:
To make it accessable as a normal .NET property you need to add a property wrapper.
This wrapper does nothing else than internally getting and setting the value by using
the GetValue() and SetValue() Methods inherited from DependencyObject and passing
the DependencyProperty as key.
Important: Do not add any logic to these properties, because they are only called
when you set the property from code. If you set the property from XAML the
SetValue() method is called directly.
If you are using Visual Studio, you can type propdp and hit 2x tab to create a
dependency property.
// Dependency Property
public static readonly DependencyProperty CurrentTimeProperty =
DependencyProperty.Register( "CurrentTime", typeof(DateTime),
typeof(MyClockControl), new FrameworkPropertyMetadata(DateTime.Now));
Activity 2:
Value Changed Callback
The change notification callback is a static method, that is called everytime when the
value of the TimeProperty changes. The new value is passed in the EventArgs, the object
on which the value changed is passed as the source.
{
data = DateTime.Now;
}
return data;
}
Validation Callback
In the validate callback you check if the set value is valid. If you return false, an
ArgumentException will be thrown. In our example demand, that the data is an instance
of a DateTime.
Activity 3:
Readonly DependencyProperties
Some dependency property of WPF controls are readonly. They are often used to report
the state of a control, like the IsMouseOver property. Is does not make sense to provide
a setter for this value.
Maybe you ask yourself, why not just use a normal .NET property? One important
reason is that you cannot set triggers on normal .NET propeties.
Activity 4:
Attached Properties
Attached properties are a special kind of DependencyProperties. They allow you to
attach a value to an object that does not know anything about this value.
A good example for this concept are layout panels. Each layout panel needs different
data to align its child elements. The Canvas needs Top and Left, The DockPanel needs
Dock, etc. Since you can write your own layout panel, the list is infinite. So you see, it's
not possible to have all those properties on all WPF controls.
The solution are attached properties. They are defined by the control that needs the
data from another control in a specific context. For example an element that is aligned
by a parent layout panel.
To set the value of an attached property, add an attribute in XAML with a prefix of the
element that provides the attached property. To set the the Canvas.Top and Canvas.Left
property of a button aligned within a Canvas panel, you write it like this:
<Canvas>
<Button Canvas.Top="20" Canvas.Left="20" Content="Click me!"/>
</Canvas>
If you want to listen to changes of a dependency property, you can subclass the type that
defines the property and override the property metadata and pass an
PropertyChangedCallback. But an much easier way is to get
the DependencyPropertyDescriptor and hookup a callback by
calling AddValueChanged()
if (textDescr!= null)
{
textDescr.AddValueChanged(myTextBox, delegate
{
// Add your propery changed logic here...
});
}
button1.ClearValue( Button.ContentProperty );
7) Stage v (verify)
Home Activities:
Activity 1:
Create a WPF Application which contain order information of a purchase. User can
choose their existing address from a list. Use dependency and attached property to
show the user defined address in shipping address.
Activity 2:
When user change the shipping address, ask user to change the existing address (source
address) or add new address in the list of addresses.
LAB # 09
Statement Purpose:
Purpose of this lab is to learn Extensible Markup Language (XML) file structure, reading
and writing xml files using C#. XML is the universal format for data on the Web. XML allows
developers to easily describe and deliver rich, structured data from any application in a
standard, consistent way. XML does not replace HTML; rather, it is a complementary
format.
Activity Outcomes:
This lab teaches you the following topics:
1) Stage J (Journey)
Introduction
XmlWriter class represents a writer that provides a fast, non-cached, forward-only way
to generate streams or files that contain XML data.
XmlReader class Represents a reader that provides fast, noncached, forward-only access
to XML data.
2) Stage a1 (apply)
Lab Activities:
Activity 1:
Create following XML file using XmlWriter class:
Solution:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
w.WriteStartDocument();
w.WriteStartElement("catalog");
w.WriteStartElement("book");
w.WriteAttributeString("id", "bk101");
w.WriteElementString("author", "Gambardella, Matthew");
w.WriteElementString("title", "XML Developer's Guide");
w.WriteElementString("genre", "Computer");
w.WriteElementString("price", "44.95");
w.WriteElementString("publish_date", "2000-10-01");
w.WriteEndElement();
w.WriteEndDocument();
w.Close();
Activity 2:
Create following XML file using XmlWriter class:
</SatteliteInfo>
</Position>
<Image Resolution="1024x800">
<Path>\images\1.jpg</Path>
</Image>
</GPS_Log>
Solution:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
w.WriteStartDocument();
w.WriteStartElement("GPS_Log");
w.WriteStartElement("Position");
w.WriteAttributeString("DateTime", DateTime.Now.ToString());
w.WriteElementString("x", "65.8973342");
w.WriteElementString("y", "72.3452346");
w.WriteStartElement("SatteliteInfo");
w.WriteElementString("Speed", "40");
w.WriteElementString("NoSatt", "7");
w.WriteEndElement();
w.WriteEndElement();
w.WriteStartElement("Image");
w.WriteAttributeString("Resolution", "1024x800");
w.WriteElementString("Path", @"\images\1.jpg");
w.WriteEndDocument();
w.Close();
Activity 3:
Read xml file in following format using XmlReader class. Also, print the xml on console.
</title>
<author>
<first-name>
Herman
</first-name>
<last-name>
Melville
</last-name>
</author>
<price>
11.99
</price>
</book>
</bookstore>
Solution:
XmlTextReader reader = new XmlTextReader("books.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
Activity 4:
The following example uses the XmlReader methods to read the content of elements
and attributes.
String xmlString =
@"<bookstore>
<book genre='autobiography' publicationdate='1981-03-22' ISBN='1-
861003-11-0'>
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
</bookstore>";
// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
CCS412 –Lab Manual 68
reader.ReadToFollowing("book");
reader.MoveToFirstAttribute();
string genre = reader.Value;
output.AppendLine("The genre value: " + genre);
reader.ReadToFollowing("title");
output.AppendLine("Content of the title element: " +
reader.ReadElementContentAsString());
}
OutputTextBlock.Text = output.ToString();
3) Stage v (verify)
Home Activities:
Activity 1:
Generate following xml file using XmlWriter. Format the xml using XmlSettings class.
Activity 2:
Read XML file generated in above activity using XmlReader class and display the output
xml on console.
Activity 3:
Generate following xml file using XmlWriter. Format the xml using XmlSettings class.
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
</catalog_item>
<catalog_item gender="Women's">
<item_number>RRX9856</item_number>
<price>42.50</price>
<size description="Small">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Large">
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Extra Large">
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
</catalog_item>
</product>
</catalog>
Activity 4:
Read XML file generated in above activity using XmlReader class and display the output
xml on console.
LAB # 10
Statement Purpose:
LINQ to SQL is a component of .NET Framework version 3.5 that provides a run -time
infrastructure for managing relational data as objects.
Activity Outcomes:
This lab teaches you the following topics:
• LINQ Basics
• LINQ with Relational Database
Instructor Note:
Relational data appears as a collection of two-dimensional tables, where common columns
relate tables to each other. To use LINQ to SQL effectively, you must have some familiarity
with the underlying principles of relational databases.
1) Stage J (Journey)
Introduction
In LINQ to SQL, the data model of a relational database is mapped to an object model
expressed in the programming language of the developer. When the application runs,
LINQ to SQL translates into SQL the language-integrated queries in the object model and
sends them to the database for execution. When the database returns the results, LINQ
to SQL translates them back to objects that you can work with in your own
programming language.
Developers using Visual Studio typically use the Object Relational Designer, wh ich
provides a user interface for implementing many of the features of LINQ to SQL.
2) Stage a1 (apply)
Lab Activities:
Activity 1:
The following example shows the complete query operation. The complete operation
includes creating a data source, defining the query expression, and executing the query
in a foreach statement.
class IntroToLINQ
{
static void Main()
{
CCS412 –Lab Manual 71
// 2. Query creation.
// numQuery is an IEnumerable<int>
var numQuery =
from num in numbers
where (num % 2) == 0
select num;
// 3. Query execution.
foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
}
}
Query variable itself only stores the query commands. The actual execution of the query
is deferred until you iterate over the query variable in a foreach statement. This concept
is referred to as deferred execution and is demonstrated in the following example:
// Query execution.
foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
Queries that perform aggregation functions over a range of source elements must first
iterate over those elements. Examples of such queries are Count, Max, Average, and
First. These execute without an explicit foreach statement because the query itself must
use foreach in order to return a result. Note also that these types of queries return a
single value, not an IEnumerable collection. The following query returns a count of the
even numbers in the source array:
var evenNumQuery =
from num in numbers
where (num % 2) == 0
select num;
To force immediate execution of any query and cache its results, you can call the
ToList<TSource> or ToArray<TSource> methods.
List<int> numQuery2 =
(from num in numbers
where (num % 2) == 0
select num).ToList();
// or like this:
// numQuery3 is still an int[]
var numQuery3 =
(from num in numbers
where (num % 2) == 0
select num).ToArray();
You can also force execution by putting the foreach loop immediately after the query
expression. However, by calling ToList or ToArray you also cache all the data in a
single collection object.
Activity 2:
This example shows how to perform a simple query over a list of Student objects. Each
Student object contains some basic information about the student, and a list that
represents the student's scores on four examinations.
The following query returns the students who received a score of 90 or greater on their
first exam.
public class StudentClass
{
protected enum GradeLevel { FirstYear = 1, SecondYear, ThirdYear, FourthYear };
protected class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
public GradeLevel Year;
public List<int> ExamScores;
}
Activity 3:
Consider following relational database where Patients get Appointments from Doctors.
Patient table contains the information of patients. Doctor table contains the information
of doctors. Appointment table is a centre table which has Patient’s ID as pID and
Doctor’s ID as dID
Write LINQ to show data from Appointments table where match in Doctor and Patient
table is found. Using joins and navigation method.
Solution:
Using Joins
from a in Appointments
join p in Patients on a.PID equals p.PID
join d in Doctors on a.DID equals d.Id
select new {
a.AID,
Patient = p.Name,
Doctor = d.Name,
a.AppointmentDate,
a.AppointmentTime
}
from a in Appointments
select new {
a.AID,
Patient = a.Patient.Name,
Doctor = a.Doctor.Name,
CCS412 –Lab Manual 75
a.AppointmentDate,
a.AppointmentTime
}
3) Stage v (verify)
Home Activities:
Consider given ERD to perform following activities:
Activity 1:
1. Create Tables according to above database diagram and create relations in MS SQL
Server or in Database File.
2. Create WPF forms to display courses and input fields to insert new course record.
Activity 2:
1. Create WPF form to display Students and input fields to insert new student record.
2. Create WPF form to display Students and their assigned courses. Also, provide with
search options where user can search courses or students.
Activity 3:
1. Create WPF form to display all courses which are not assigned to any student.
2. Create WPF form to display all students which has not been assigned any course.
LAB # 11
Statement Purpose:
Purpose – the purpose of this lab is to learn how to build a MVC application using ASP.net.
the students shall learn how to handle and add models, views and controllers to their
application.
Activity Outcomes:
This lab teaches you the following topics:
5) StageJ(Journey)
Introduction
ASP.NET is a unified Web development model that includes the services necessary for
you to build enterprise-class Web applications with a minimum of coding. ASP.NET is part
of the .NET Framework, and when coding ASP.NET applications you have access to classes
in the .NET Framework. You can code your applications in any language compatible with
the common language runtime (CLR), including Microsoft Visual Basic, C#, JScript .NET,
and J#. These languages enable you to develop ASP.NET applications that benefit from
the common language runtime, type safety, inheritance, and so on.
ASP.NET Web pages are completely object-oriented. Within ASP.NET Web pages you can
work with HTML elements using properties, methods, and events. The ASP.NET page
framework removes the implementation details of the separation of client and server
inherent in Web-based applications by presenting a unified model for responding to
client events in code that runs at the server. The framework also automatically maintains
the state of a page and the controls on that page during the page processing life cycle.
The ASP.NET page and controls framework also enables you to encapsulate common UI
functionality in easy-to-use, reusable controls. Controls are written once, can be used in
many pages, and are integrated into the ASP.NET Web page that they are placed in during
rendering. The ASP.NET page and controls framework also provides features to control
the overall look and feel of your Web site via themes and skins. You can define themes
and skins and then apply them at a page level or at a control level.
MODEL-VIEW-CONTROLLER
ASP.NET MVC is stateless. In this we do not pass requests to the page like Web Forms. But
we talk to the controller. And controller handles the request and fetches the desired data
from the model and transfers the data to the view. And this View is used to display the
data to the end user.
• Model
• View
• Controller
Model
This is the business layer. It helps retrieve data from the database. These are simple class files
that contain the properties.
View
This component is responsible for displaying data on the screen. In MVC we use Razor Syntax.
The extension of the view has “*.cshtml” instead of “.aspx” which we have used in ASP.NET
in the past.
Controller
It handles input control. All the user interaction is done through Controller. A Controller is
nothing but a class file that contains the methods.
Finally take a take a look at your Solution explorer and notice that Models, Views and
Controllers are stored within separate folders as shown in the following screenshot.
A more detailed breakdown of the folders in your project structure is shown be low.
6) Stage a1 (apply)
Lab Activities:
Activity 1: Create a MVC web application
Create a web project: File > New Project.)
Then select Visual C# on the left, then Web and then select ASP.NET Web Application.
Name your project "MvcMovie" and then click OK.
In the New ASP.NET Project dialog, click MVC and then click OK.
Visual Studio used a default template for the ASP.NET MVC project you just created, so
you have a working application right now without doing anything! This is a simple "Hello
World!" project, and it's a good place to start your application.
Click F5 to start debugging. F5 causes Visual Studio to start IIS Express and run your web
app. Visual Studio then launches a browser and opens the application's home page. Notice
that the address bar of the browser says localhost:port# and not something like example.com.
That's because localhost always points to your own local computer, which in this case is
running the application you just built. When Visual Studio runs a web project, a random
port is used for the web server. In the image below, the port number is 1234. When you
run the application, you'll see a different port number.
Right out of the box this default template gives you Home, Contact and About pages. The
image above doesn't show the Home, About and Contact links. Depending on the size of
your browser window, you might need to click the navigation icon to see these links.
The application also provides support to register and log in. The next step is to change
how this application works and learn a little bit about ASP.NET MVC. Close the ASP.NET
MVC application and let's change some code.
In the Add Scaffold dialog box, click MVC 5 Controller - Empty, and then click Add.
Notice in Solution Explorer that a new file has been created named
HelloWorldController.cs and a new folder Views\HelloWorld. The controller is open in
the IDE.
C#
using System.Web;
using System.Web.Mvc;
namespace MvcMovie.Controllers
{
public class HelloWorldController : Controller
{
//
// GET: /HelloWorld/
//
// GET: /HelloWorld/Welcome/
The controller methods will return a string of HTML as an example. The controller is
named HelloWorldController and the first method is named Index. Let's invoke it from a
browser. Run the application (press F5 or Ctrl+F5). In the browser, append "HelloWorld"
to the path in the address bar. (For example, in the illustration below, it's
http://localhost:1234/HelloWorld. ) The page in the browser will look like the following
screenshot. In the method above, the code returned a string directly. You told the system
to just return some HTML, and it did!
ASP.NET MVC invokes different controller classes (and different action methods within
them) depending on the incoming URL. The default URL routing logic used by ASP.NET
MVC uses a format like this to determine what code to invoke:
/[Controller]/[ActionName]/[Parameters]
C#
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
When you run the application and don't supply any URL segments, it defaults to the
"Home" controller and the "Index" action method specified in the defaults section of the
code above.
The first part of the URL determines the controller class to execute. So /HelloWorld maps
to the HelloWorldController class. The second part of the URL determines the action metho d
on the class to execute. So /HelloWorld/Index would cause the Index method of the
HelloWorldController class to execute. Notice that we only had to browse to /HelloWorld and
the Index method was used by default. This is because a method named Index is the default
method that will be called on a controller if one is not explicitly specified. The third part
of the URL segment ( Parameters) is for route data. We'll see route data later on in this
tutorial.
Let's modify the example slightly so that you can pass some parameter information from
the URL to the controller (for example, /HelloWorld/Welcome?name=Scott&numtimes=4).
Change your Welcome method to include two parameters as shown below. Note that the
code uses the C# optional-parameter feature to indicate that the numTimes parameter
should default to 1 if no value is passed for that parameter.
C#
In the sample above, the URL segment ( Parameters ) is not used, the name and numTimes
parameters are passed as query strings. The ? (question mark) in the above URL is a
separator, and the query strings follow. The & character separates query strings.
C#
This time the third URL segment matched the route parameter ID. The Welcome action
method contains a parameter (ID) that matched the URL specification in the RegisterRoutes
method.
C#
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
In ASP.NET MVC applications, it's more typical to pass in parameters as route data (like
we did with ID above) than passing them as query strings. You could also add a route to
pass both the name and numtimes in parameters as route data in the URL. In the
App_Start\RouteConfig.cs file, add the "Hello" route:
C#
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{i d}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Hello",
url: "{controller}/{action}/{name}/{id}"
);
}
}
For many MVC applications, the default route works fine. You'll learn later in this tutorial
to pass data using the model binder, and you won't have to modify the default route for
that.
In these examples the controller has been doing the "VC" portion of MVC — that is, the
view and controller work. The controller is returning HTML directly. Ordinarily you don't
want controllers returning HTML directly, since that becomes very cumbersome to code.
Instead we'll typically use a separate view template file to help generate the HTML
response. Let's look next at how we can do this.
You'll create a view template file using the Razor view engine. Razor-based view
templates have a .cshtml file extension, and provide an elegant way to create HTML output
using C#. Razor minimizes the number of characters and keystrokes required when
writing a view template, and enables a fast, fluid coding workflow.
Currently the Index method returns a string with a message that is hard-coded in the
controller class. Change the Index method to return a View object, as shown in the following
code:
The Index method above uses a view template to generate an HTML response to the
browser. Controller methods (also known as action methods), such as the Index method
above, generally return an ActionResult (or a class derived from ActionResult), not
primitive types like string.
Right click the Views\HelloWorld folder and click Add, then click MVC 5 View Page with
(Layout Razor).
In the Specify Name for Item dialog box, enter Index, and then click OK.
In the Select a Layout Page dialog, accept the default _Layout.cshtml and click OK.
In the dialog above, the Views\Shared folder is selected in the left pane. If you had a
custom layout file in another folder, you could select it.
cshtml
@{
Layout = "~/Views/Shared/_Layout.cshtml"; }
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>Hello from our View Template!</p>
CCS412 –Lab Manual 93
You can also right click the Index.cshtml file and select View in Page Inspector.
Alternatively, run the application and browse to the HelloWorld controller
(http://localhost:xxxx/HelloWorld). The Index method in your controller didn't do much work;
it simply ran the statement return View(), which specified that the method should use a
view template file to render a response to the browser. Because you didn't explicitly
specify the name of the view template file to use, ASP.NET MVC defaulted to using the
Index.cshtml view file in the \Views\HelloWorld folder. The image below shows the string
"Hello from our View Template!" hard-coded in the view.
Layout templates allow you to specify the HTML container layout of your site in one place
and then apply it across multiple pages in your site. Find the @RenderBody() line. RenderBody
is a placeholder where all the view-specific pages you create show up, "wrapped" in the
layout page. For example, if you select the About link, the Views\Home\About.cshtml view
is rendered inside the RenderBody method.
Change the contents of the title element. Change the ActionLink in the layout template
from "Application name" to "MVC Movie" and the controller from Home to Movies . The
complete layout file is shown below:
cshtml
CCS412 –Lab Manual 96
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - Movie App</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles /modernizr")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collaps e" data-target=".nav ba r-
collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("MVC Movie", "Index", "Movies", null, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
@Html.Partial("_LoginParti al")
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
@Scripts.Render("~/bundles /jquery")
@Scripts.Render("~/bundles /bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
Run the application and notice that it now says "MVC Movie ". Click the About link, and
you see how that page shows "MVC Movie", too. We were able to make the change once
in the layout template and have all pages on the site reflect the new title.
cshtml
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
The Razor code above is explicitly setting the layout page. Examine the
Views\_ViewStart.cshtml file, it contains the exact same Razor markup. The
Views\_ViewStart.cshtml file defines the common layout that all views will use, therefore
you can comment out or remove that code from the Views\HelloWorld\Index.cshtml file.
cshtml
@*@{
Layout = "~/Views/Shared/_Layout.cshtml";
}*@
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
You can use the Layout property to set a different layout view, or set it to null so no layout
file will be used.
cshtml
@{
ViewBag.Title = "Movie List";
}
To indicate the HTML title to display, the code above sets a Title property of the ViewBag
object (which is in the Index.cshtml view template). Notice that the layout template (
Views\Shared\_Layout.cshtml ) uses this value in the <title> element as part of the <head>
section of the HTML that we modified previously.
cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - Movie App</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles /modernizr")
</head>
Using this ViewBag approach, you can easily pass other parameters between your view
template and your layout file.
Run the application. Notice that the browser title, the primary heading, and the secondary
headings have changed. (If you don't see changes in the browser, you might be viewing
cached content. Press Ctrl+F5 in your browser to force the response from the server to
be loaded.) The browser title is created with the ViewBag.Title we set in the Index.cshtml
view template and the additional "- Movie App" added in the layout file.
Also notice how the content in the Index.cshtml view template was merged with the
_Layout.cshtml view template and a single HTML response was sent to the browser.
Layout templates make it really easy to make changes that apply across all of the pages
in your application.
Our little bit of "data" (in this case the "Hello from our View Template!" message) is hard -
coded, though. The MVC application has a "V" (view) and you've got a "C" (controller), but
no "M" (model) yet. Shortly, we'll walk through how create a database and retrieve model
data from it.
Controllers are responsible for providing whatever data or objects are required in order
for a view template to render a response to the browser. A best practice: A view template
should never perform business logic or interact with a database directly. Instead, a
view template should work only with the data that's provided to it by the controller.
Maintaining this "separation of concerns" helps keep your code clean, testable and more
maintainable.
Currently, the Welcome action method in the HelloWorldController class takes a name and a
numTimes parameter and then outputs the values directly to the browser. Rather than have
the controller render this response as a string, let's change the controller to use a view
template instead. The view template will generate a dynamic response, which means that
you need to pass appropriate bits of data from the controller to the view in order to
generate the response. You can do this by having the controller put the dynamic data
(parameters) that the view template needs in a ViewBag object that the view template can
then access.
Return to the HelloWorldController.cs file and change the Welcome method to add a Message
and NumTimes value to the ViewBag object. ViewBag is a dynamic object, which means you
can put whatever you want in to it; the ViewBag object has no defined properties until you
put something inside it. The ASP.NET MVC model binding system automatically maps the
named parameters (name and numTimes ) from the query string in the address bar to
parameters in your method. The complete HelloWorldController.cs file looks like this:
C#
using System.Web;
using System.Web.Mvc;
namespace MvcMovie.Controllers
{
public class HelloWorldController : Controller
{
public ActionResult Index()
{
return View();
}
return View();
}
}
}
Now the ViewBag object contains data that will be passed to the view automatically. Next,
you need a Welcome view template! In the Build menu, select Build Solution (or
Ctrl+Shift+B) to make sure the project is compiled. Right click the Views\HelloWorld
folder and click Add, then click MVC 5 View Page with (Layout Razor).
In the Specify Name for Item dialog box, enter Welcome, and then click OK.
In the Select a Layout Page dialog, accept the default _Layout.cshtml and click OK.
Replace the markup in the Welcome.cshtml file. You'll create a loop that says "Hello" as
many times as the user says it should. The complete Welcome.cshtml file is shown below.
cshtml
@{
ViewBag.Title = "Welcome";
}
<h2>Welcome</h2>
<ul>
@for (int i = 0; i < ViewBag.NumTimes; i++)
{
<li>@ViewBag.Message</li>
}
</ul>
http://localhost:xx/HelloWorld/Welcome?name=Scott&numtimes=4
Now data is taken from the URL and passed to the controller using the model binder. The
controller packages the data into a ViewBag object and passes that object to the view. The
view then displays the data as HTML to the user.
In the sample above, we used a ViewBag object to pass data from the controller to a view.
In this section you'll add some classes for managing movies in a database. These classes
will be the "model" part of the ASP.NET MVC app.
You'll use a .NET Framework data-access technology known as the Entity Framework to
define and work with these model classes. The Entity Framework (often referred to as
EF) supports a development paradigm called Code First. Code First allows you to create
model objects by writing simple classes. (These are also kno wn as POCO classes, from
"plain-old CLR objects.") You can then have the database created on the fly from your
classes, which enables a very clean and rapid development workflow. If you are required
to create the database first, you can still follow this tutorial to learn about MVC and EF
app development. You can then follow Tom Fizmakens ASP.NET Scaffolding tutorial,
which covers the database first approach.
C#
using System;
namespace MvcMovie.Models
{
public class Movie
{
We'll use the Movie class to represent movies in a database. Each instance of a Movie object
will correspond to a row within a database table, and each property of the Movie class will
map to a column in the table.
C#
using System;
using System.Data.Entity;
namespace MvcMovie.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
The MovieDBContext class represents the Entity Framework movie database context, which
handles fetching, storing, and updating Movie class instances in a database. The
MovieDBContext derives from the DbContext base class provided by the Entity Framework.
In order to be able to reference DbContext and DbSet, you need to add the following using
statement at the top of the file:
C#
using System.Data.Entity;
You can do this by manually adding the using statement, or you can right click on the red
squiggly lines and click using System.Data.Entity;
Note: Several unused using statements have been removed. You can do this by right
clicking in the file, click Organize Usings, and then click Remove Unused Usings.
We've finally added a model (the M in MVC). In the next section you'll work with the
database connection string.
8) Stage v (verify)
Home Activities:
Activity 1:
Build a MVC application that contains model for patient doctor informa tion system. The
application should contain views for patients, doctors that can manage their personal
information and for receptionist that can manage the appointments for patients and
doctors.
For this exercise Consider following relational database where Patients get
Appointments from Doctors. Patient table contains the information of patients. Doctor
table contains the information of doctors. Appointment table is a centre table which has
Patient’s ID as pID and Doctor’s ID as dID
9) Stagea2(assess)
LAB # 12
Lab Sessional 2
LAB # 13
Statement Purpose:
Students will learn to use Entity framework to access and query data from model classes
and database.
Activity Outcomes:
This lab teaches you the following topics:
6) StageJ(Journey)
Introduction
Traditionally we have been designing and developing data centric applications(Data
Driven Design). What this means is that we used to think about what data is required to
fulfill our business needs and then we build our software bottom up from the database
schema. This approach is still being followed for many applications and for such
applications we should use the Entity framework database first approach.
So Entity framework code first approach enables us to write Plain Old CLR
Objects(POCOs) for our models and then let us persist them in a data store by defining a
DbContext class for our model classes. Few of the benefits of using this approach are:
7) Stage a1 (apply)
Lab Activities:
Activity: MVC with Entity Framework
The Contoso University Web Application
The application you'll be building in these tutorials is a simple university web site. Users
can view and update student, course, and instructor information. Here are a few of the
screens you'll create.
The UI style of this site has been kept close to what's generated by the built-in templates,
so that the tutorial can focus mainly on how to use the Entity Framework.
Open Visual Studio and create a new C# Web project named "ContosoUniversity".
In the New ASP.NET Project dialog box select the MVC template.
If the Host in the cloud check box in the Microsoft Azure section is selected, clear it.
In the Change Authentication dialog box, select No Authentication, and then click OK.
Back in the New ASP.NET Project dialog box, click OK to create the project.
A few simple changes will set up the site menu, layout, and home page.
• Add menu entries for Students, Courses, Instructors, and Departments, and delete
the Contact entry.
cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<div class="container">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - Contoso University</p>
</footer>
</div>
@Scripts.Render("~/bundles /jquery")
@Scripts.Render("~/bundles /bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
In Views\Home\Index.cshtml, replace the contents of the file with the following code to
replace the text about ASP.NET and MVC with text about this application:
cshtml
@{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
<h1>Contoso University</h1>
</div>
<div class="row">
<div class="col-md-4">
<h2>Welcome to Contoso University</h2>
<p>Contoso University is a sample application that
demonstrates how to use Entity Framework 6 in an
ASP.NET MVC 5 web application.</p>
</div>
<div class="col-md-4">
<h2>Build it from scratch</h2>
<p>You can build the application by following the steps in the tutorial series on the ASP.NET site.</p>
<p><a class="btn btn-default" href="http://www.asp.net/mvc/tutorials/getti ng-started-with- ef-
using-mvc/">See the tutorial »</a></p>
</div>
<div class="col-md-4">
<h2>Download it</h2>
<p>You can download the completed project from the Microsoft Code Gallery.</p>
<p><a class="btn btn-default" href="http://code.msdn.microsoft.com/ASPNET-MVC-Applicati o n-
b01a9fe8">Download »</a></p>
</div>
</div>
Press CTRL+F5 to run the site. You see the home page with the main menu.
From the Tools menu click NuGet Package Manager and then click Package Manager
Console.
Install-Package EntityFramework
The image shows 6.0.0 being installed, but NuGet will install the latest version of Entity
Framework (excluding pre-release versions).
This step is one of a few steps that this tutorial has you do manually, but which could have
been done automatically by the ASP.NET MVC scaffolding feature. You're doing them
manually so that you can see the steps required to use the Entity Framework. You'll use
scaffolding later to create the MVC controller and views. An alternative is to let scaffolding
automatically install the EF NuGet package, create the database context class, and create
the connection string. When you're ready to do it that way, all you have to do is skip those
steps and scaffold your MVC controller after you create your entity classes.
Next you'll create entity classes for the Contoso University application. You'll start with
the following three entities:
There's a one-to-many relationship between Student and Enrollment entities, and there's a
one-to-many relationship between Course and Enrollment entities. In other words, a student
can be enrolled in any number of courses, and a course can have any number of students
enrolled in it.
CCS412 –Lab Manual 115
In the following sections you'll create a class for each one of these entities.
In the Models folder, create a class file named Student.cs and replace the template code
with the following code:
using System;
using System.Collections.Generic;
namespace ContosoUniversity.Models
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
Navigation properties are typically defined as virtual so that they can take advantage of
certain Entity Framework functionality such as lazy loading. If a navigation property can
hold multiple entities (as in many-to-many or one-to-many relationships), its type must
be a list in which entries can be added, deleted, and updated, such as ICollection.
In the Models folder, create Enrollment.cs and replace the existing code with the following
code:
C#
namespace ContosoUniversity.Models
{
public enum Grade
{
A, B, C, D, F
}
The Grade property is an enum. The question mark after the Grade type declaration
indicates that the Grade property is nullable. A grade that's null is different from a zero
grade — null means a grade isn't known or hasn't been assigned yet.
The StudentID property is a foreign key, and the corresponding navigation property is
Student. An Enrollment entity is associated with one Student entity, so the property can only
hold a single Student entity (unlike the Student.Enrollments navigation property you saw
earlier, which can hold multiple Enrollment entities).
The CourseID property is a foreign key, and the corresponding navigation property is
Course. An Enrollment entity is associated with one Course entity.
In the Models folder, create Course.cs, replacing the template code with the following code:
C#
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations. Schema;
namespace ContosoUniversity.Models
{
public class Course
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
The main class that coordinates Entity Framework functionality for a given data model is
the database context class. You create this class by deriving from the
System.Data.Entity.DbContext class. In your code you specify which entities are included
CCS412 –Lab Manual 118
in the data model. You can also customize certain Entity Framework behavior. In this
project, the class is named SchoolContext.
C#
using ContosoUniversity.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions ;
namespace ContosoUniversity.DAL
{
public class SchoolContext : DbContext
{
You could have omitted the DbSet<Enrollment> and DbSet<Course> statements and it would
work the same. The Entity Framework would include them implicitly because the Student
entity references the Enrollment entity and the Enrollment entity references the Course entity.
C#
public SchoolContext() : base("SchoolContext")
{
}
You could also pass in the connection string itself instead of the name of one that is stored
in the Web.config file. For more information about options for specifying the database to
use, see Entity Framework - Connections and Models.
If you don't specify a connection string or the name of one explicitly, Entity Framework
assumes that the connection string name is the same as the class name. The default
connection string name in this example would then be SchoolContext, the same as what
you're specifying explicitly.
The Entity Framework can automatically create (or drop and re-create) a database for
you when the application runs. You can specify that this should be done every time your
application runs or only when the model is out of sync with the existing database. You
can also write a Seed method that the Entity Framework automatically calls after creating
the database in order to populate it with test data.
The default behavior is to create a database only if it doesn't exist (and throw an
exception if the model has changed and the database already exists). In this section you'll
specify that the database should be dropped and re-created whenever the model changes.
Dropping the database causes the loss of all your data. This is generally OK during
development, because the Seed method will run when the database is re-created and will
re-create your test data. But in production you generally don't want to lose all your data
every time you need to change the database schema.
In the DAL folder, create a new class file named SchoolInitializer.cs and replace the
template code with the following code, which causes a database to be created when
needed and loads test data into the new database.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using ContosoUniversity.Models;
namespace ContosoUniversity.DAL
{
public class SchoolInitializer : System.Data.Entity. DropCreateDatabaseIfModel Changes<SchoolCont ex t>
{
protected override void Seed(SchoolContext context)
{
var students = new List<Student>
{
new
Student{FirstMidName="Carson",Las tName="Alexander",EnrollmentD ate=DateTime.Parse( "2005-09-
01")},
new
Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-
01")},
new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime. Parse("200 3-
09-01")},
new
Student{FirstMidName="Gytis",Las tName="Barzdukas",EnrollmentD ate=DateTime.Parse( "2002-09-
01")},
new Student{FirstMidName="Yan",LastName="Li", EnrollmentDate=D ateTime.Pars e("2002 -0 9-
01")},
new Student{FirstMidName="Peggy",Las tName="Justice",EnrollmentDate=DateTime. Parse("200 1-
09-01")},
new
Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime. Parse("2003-09-01")},
new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime. Parse("200 5-
09-01")}
};
To tell Entity Framework to use your initializer class, add an element to the
entityFramework element in the application Web.config file (the one in the root project
folder), as shown in the following example:
XML
<entityFramework>
<contexts>
<context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity">
<databaseInitializer type="ContosoUniversity.DAL.SchoolIniti alizer, ContosoUniversity" />
</context>
</contexts>
<defaultConnectionFactory type="System.Data. Entity.Infrastructure.LocalDbConnectionFactory,
EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<providerinvariantName="System.Data. SqlClient"
type="System.Data.Enti ty.SqlServ er.SqlProviderServices, EntityFramework.SqlServ er" />
</providers>
</entityFramework>
The context type specifies the fully qualified context class name and the assembly it's in,
and the databaseinitializer type specifies the fully qualified name of the initializer class and
the assembly it's in. (When you don't want EF to use the initializer, you can set an
attribute on the context element: disableDatabaseInitialization="true".) For more information,
see Entity Framework - Config File Settings.
The application is now set up so that when you access the database for the first time in a
given run of the application, the Entity Framework compares the database to the model
(your SchoolContext and entity classes). If there's a difference, the application drops and re -
creates the database.
LocalDB is a lightweight version of the SQL Server Express Database Engine. It's easy to
install and configure, starts on demand, and runs in user mode. LocalDB runs in a special
execution mode of SQL Server Express that enables you to work with databases as .mdf
files. You can put LocalDB database files in the App_Data folder of a web project if you
want to be able to copy the database with the project. The user instance feature in SQL
Server Express also enables you to work with .mdf files, but the user instance feature is
deprecated; therefore, LocalDB is recommended for working with .mdf files. In Visual
Studio 2012 and later versions, LocalDB is installed by default with Visual Studio.
Typically SQL Server Express is not used for production web applications. LocalDB in
particular is not recommended for production use with a web application because it is
not designed to work with IIS.
In this tutorial you'll work with LocalDB. Open the application Web.config file and add a
connectionStrings element preceding the appSettings element, as shown in the following
example. (Make sure you update the Web.config file in the root project folder. There's also
a Web.config file is in the Views subfolder that you don't need to update.)
If you are using Visual Studio 2015, replace "v11.0" in the connection string with
"MSSQLLocalDB", as the default SQL Server instance name has changed.
XML
<connectionStrings>
<add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Ini ti al
Catalog=ContosoUniversity1;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
The connection string you've added specifies that Entity Framework will use a LocalDB
database named ContosoUniversity1.mdf. (The database doesn't exist yet; EF will create
it.) If you wanted the database to be created in your App_Data folder, you could add
AttachDBFilename=|DataDirectory|\ContosoUniversity1.mdf to the connection string.
You don't actually have to have a connection string in the Web.config file. If you don't
supply a connection string, Entity Framework will use a default one based on your context
class.
Now you'll create a web page to display data, and the process of requesting the data will
automatically trigger the creation of the database. You'll begin by creating a new
controller. But before you do that, build the project to make the model and context classes
available to MVC controller scaffolding.
1. Right-click the Controllers folder in Solution Explorer, select Add, and then click
New Scaffolded Item.
2. In the Add Scaffold dialog box, select MVC 5 Controller with views, using Entity
Framework.
3. In the Add Controller dialog box, make the following selections and then click Add:
When you click Add, the scaffolder creates a StudentController.cs file and
a set of views (.cshtml files) that work with the controller. In the future
when you create projects that use Entity Framework you can also take
advantage of some additional functionality of the scaffolder: just create
your first model class, don't create a connection string, and then in the Add
Controller box specify new context class. The scaffolder will create your
DbContext class and your connection string as well as the controller and
views.
C#
The Index action method gets a list of students from the Students entity set by
reading the Students property of the database context instance:
C#
cshtml
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstMidName)
</th>
<th>
@Html.DisplayNameFor(model => model.EnrollmentDate)
</th>
<th></th>
</tr>
Click the Students tab to see the test data that the Seed method inserted.
Depending on how narrow your browser window is, you'll see the Student tab link
in the top address bar or you'll have to click the upper right corner to see the link.
When you ran the Students page and the application tried to access the database, EF saw
that there was no database and so it created one, then it ran the seed method to populate
the database with data.
You can use either Server Explorer or SQL Server Object Explorer (SSOX) to view the
database in Visual Studio. For this tutorial you'll use Server Explorer. (In Visual Studio
Express editions earlier than 2013, Server Explorer is called Database Explorer.)
3. Right-click the Student table and click Show Table Data to see the columns that
were created and the rows that were inserted into the table.
The ContosoUniversity1.mdf and .ldf database files are in the C:\Users\<yourusernam e>
folder.
Because you're using the DropCreateDatabaseIfModel Changes initializer, you could now make
a change to the Student class, run the application again, and the database would
automatically be re-created to match your change. For example, if you add an EmailAddress
property to the Student class, run the Students page again, and then look at the table again,
you will see a new EmailAddress column.
Home Activities:
Activity 1:
Create an application similar to the one made in lab according to our own university.
11) Stagea2(assess)
LAB # 14
Statement Purpose:
In C#, you can write applications that perform multiple tasks at the same time. Tasks with
the potential of holding up other tasks can execute on separate threads, a process known as
multithreading or free threading.
Applications that use multithreading are more responsive to user input because the user
interface stays active as processor-intensive tasks execute on separate threads.
Multithreading is also useful when you create scalable applications, because you can add
threads as the workload increases.
Activity Outcomes:
This lab teaches you the following topics:
7) Stage J (Journey)
Introduction
The most reliable way to create a multithreaded application is to use the
BackgroundWorker component. This class manages a separate thread dedicated to
processing the method that you specify.
The ThreadPool class provides your application with a poo l of worker threads that are
managed by the system, allowing you to concentrate on application tasks rather than
thread management. If you have short tasks that require background processing, the
managed thread pool is an easy way to take advantage of multiple threads. For example,
beginning with the .NET Framework 4 you can create Task and Task<TResult> objects,
which perform asynchronous tasks on thread pool threads.
8) Stage a1 (apply)
Lab Activities:
Activity 1:
Create an application containing a label and a button. Label will be used for showing
counter from 0 to 100 and increments after each 100 milliseconds. Button will be used
to start the counter.
Solution:
private void btnStart_Click(object sender, EventArgs e) {
backgroundWorker1.RunWorkerAsync();
}
Activity 2:
In above activity, counter cannot be stopped. It only stopes when counting finishes. Now
add feature in the application to start and stop the counter. Button text should be
changed based on the counter state (i.e. Start, Stop, Running).
Solution:
private void btnStart_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
this.btnStart.Text = "Start";
}
else
{
backgroundWorker1.RunWorkerAsync();
this.lblStatus.Text = "Running";
this.btnStart.Text = "Stop";
}
int i = 0;
Activity 3:
We can distinguish between thread completed or stopped status. Introduce a label for
status which indicates the thread status (i.e. Finished or Stopped).
Solution:
private void btnStart_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
this.btnStart.Text = "Start";
}
else
{
backgroundWorker1.RunWorkerAsync();
this.lblStatus.Text = "Running";
this.btnStart.Text = "Stop";
}
Activity 4:
Consider a form contains two labels name lblC1 and lblC2. These labels are used for
counter. lblC1 is for counting in ascending order (1 to 100) and lblC2 is for counting in
descending order (100 to 1). When form is loaded, both counter starts and display
counter increment/decrement after one second. You are required to use
BackgroundWorker(s) to achieve above mentioned functionality.
Solution:
BackgroundWorker bwInc, bwDec;
public Window1()
{
InitializeComponent();
Activity 5:
Use ThreadPool class to display a loop counter between 1 to 10 on console.
Solution:
private void button1_Click(object sender, EventArgs e)
{
WaitCallback callBack = new WaitCallback(work);
ThreadPool.QueueUserWorkItem(callBack, "MyThread");
}