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

C#.Net - Lab Manual

The document is a laboratory manual for a C# .NET lab course. It contains instructions and code examples for 4 programming assignments: 1) Command line argument processing for square root and sum/average, 2) Boxing/unboxing and invalid unboxing, 3) Operator overloading to add complex numbers, and 4) Finding the sum of each row in a jagged array. The document provides background information on relevant C# concepts and includes fully coded solutions for each programming problem.

Uploaded by

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

C#.Net - Lab Manual

The document is a laboratory manual for a C# .NET lab course. It contains instructions and code examples for 4 programming assignments: 1) Command line argument processing for square root and sum/average, 2) Boxing/unboxing and invalid unboxing, 3) Operator overloading to add complex numbers, and 4) Finding the sum of each row in a jagged array. The document provides background information on relevant C# concepts and includes fully coded solutions for each programming problem.

Uploaded by

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

#132, AECS Layout, IT Park Road, Kundalahalli, Bangalore – 560 037

T:+9180 28524466 / 77

CMR INSTITUTE  OF TECHNOLOGY


Department of MCA

LABORATORY MANUAL

  Semester : V

Subject Name : C# .NET LABORATORY

 Subject Code :    18MCA56 

  Scheme             :     2018

1 18MCA56 – C# .NET
Laboratory
PART - A

1.Write a Program in C# to demonstrate Command line arguments processing for the following.
a) To find the square root of a given number.
b) To find the sum & average of three numbers.

 The Console Class provides access to the standard input, standard output, and standard error streams.
Standard input – is associated with the keyboard- anything that the user types on the keyboard can be
read from the standard input stream.
 Console.Read Method – reads the next character from the keyboard. It returns the int value -1 if there is
no more input available. Otherwise it returns as int representing the character read.
 Console.ReadLine Method - reads all characters up to the end of the input line. The input is returned as
a string of characters.
 Standard output – is usually directed to the screen, as is the standard error stream.
 Console.Write Method – writes text to the console without a carriage return
 Console.WriteLine Method – writes a text string including a carriage return to the out console

a) To find the square root of a given number.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;

namespace Squareroot_1A
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{

2 18MCA56 – C# .NET
Laboratory
Console.WriteLine("No Arguments Passed");
}
else
{
Console.WriteLine("Following are the arguments");

foreach (string arg in args)


{
int a = Convert.ToInt16(arg);

double b = Math.Sqrt(a);
Console.WriteLine(b.ToString ()+ " , ");
}
}
Console.ReadLine();

}
}
}

To show with Command Line Arguments:

 Right click your project in Solution Explorer and select Properties from the menu

 Go to Configuration Properties -> Debugging

 Set the Command Arguments in the property list.


Input : 9 225

b) To find the sum & average of three numbers.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sumthree_1B
{

3 18MCA56 – C# .NET
Laboratory
class Program
{
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("No arguments. ");
}
else
{
double a, b, c, tot, avg;
a = Convert.ToInt16(args[0]);
b = Convert.ToInt16(args[1]);
c = Convert.ToInt16(args[2]);
tot = a + b + c;
avg = tot / 3.0;
Console.WriteLine("Three given numbers :" + a.ToString() + ", " + b.ToString() + ", " +
c.ToString());
Console.WriteLine("Sum of the three given numbers :"+tot.ToString ());

Console.WriteLine("Average of the three given numbers :" + avg.ToString());


}
Console.ReadLine();
}
}
}

Input : 4 7 22

4 18MCA56 – C# .NET
Laboratory
2. Write a Program in C# to demonstrate the following
a) Boxing and Unboxing b) Invalid Unboxing.

Boxing
It is defined as the process of explicitly converting a value type into a corresponding reference type by
storing the variable in a System.Object.

Consider an example a variable of type short:


short s = 25;

If, during the course of your application, you wish to represent this value type as a reference type, you would
“box” the value as follows:
// Box the value into an object reference.
object objShort = s;

Unboxing

It is the process of converting the value held in the object reference back into a corresponding value type on
the stack. The unboxing operation begins by verifying that the receiving data type is equivalent to the boxed
type, and if so, it copies the value back into a local stack-based variable.

The following unboxing operation works successfully, given that the underlying type of the objShort is indeed
a short.

// Unbox the reference back into a corresponding short.


short anotherShort = (short)objShort;

a) Boxing and Unboxing

namespace Prg_2a
{
class Program
{
static void box(object obj)
{
Console.WriteLine("Value : " + obj);
}

static void Main(string[] args)


{
Object o;
int a = 10;
double d = 4.4;
o = a; //boxing integer
Console.WriteLine("Passing integer");
box(a);
Console.WriteLine("Passing Object");
box(o);
int x = (int)o;//Unboxing
Console.WriteLine("");
Console.WriteLine("Unboxing");
Console.WriteLine("a= " + x);
o = d; //boxing double
Console.WriteLine("Passing double");
box(d);
Console.WriteLine("Passing Object");
box(o);

5 18MCA56 – C# .NET
Laboratory
double dd = (double)o; //Unboxing
Console.WriteLine("d= : " + dd);
Console.ReadLine();

}
}
}

b) Invalid Unboxing.

namespace Prg_2a
{
class Prg_2b
{

static void Main(string[] args)


{

int i = 123;
object o = i; // implicit boxing
try
{
int j = (short)o; // attempt to unbox

System.Console.WriteLine("Unboxing OK.");
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("{0} Error: Incorrect unboxing.", e.Message);
}
Console.ReadLine();
}
}
}

6 18MCA56 – C# .NET
Laboratory
3. Write a program in C# to add Two complex numbers using Operator overloading .

class Prg3
{
class Complex
{
private int x, y;
public Complex() { }
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine(Convert.ToString(x) + " " + Convert.ToString(y));
}
public static Complex operator -(Complex c)
{
Complex temp = new Complex();
temp.x = -c.x;
temp.y = -c.y;
return temp;
}
public static Complex operator +(Complex c1, Complex c2)
{
Complex temp = new Complex();
temp.x = c1.x + c2.x;
temp.y = c1.y + c2.y;
return temp;
}
}
static void Main(string[] args)
{
Complex c1 = new Complex(10, 20);
Complex c2 = new Complex(30, 40);
Complex c3, c4 = new Complex();
Console.Write("Complex Number1 :");
c1.ShowXY();
Console.Write ("Complex Number2 :");
c2.ShowXY();
c3 = -c1;
Console.Write("Changing the sign of Complex Number1 :");
c3.ShowXY();
7 18MCA56 – C# .NET
Laboratory
c4 = c1 + c2;
Console.Write("Addition of two complex Numbers 1 and 2:");
c4.ShowXY();
Console.ReadLine();
}
}

4. Write a Program in C# to find the sum of each row of given jagged array of 3 inner arrays.

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 is a declaration of a single-dimensional array that has three elements, each of which is a
single-dimensional array of integers:

int[][] jaggedArray = new int[3][];


Before using jaggedArray, its elements must be initialized.
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
Each of the elements is a single-dimensional array of integers. The first element is an array of 5 integers, the
second is an array of 4 integers, and the third is an array of 2 integers.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program4
{
class Program
{
static void Main(string[] args)
{
int[][] arr = new int[3][];
int arr0_length, arr1_length, arr2_length;
int i,j, sum;

Console.WriteLine ("Enter the length of the first array : ");


arr0_length =Convert.ToInt32(Console.ReadLine());
arr[0] = new int[arr0_length];

8 18MCA56 – C# .NET
Laboratory
Console.WriteLine ("Enter the Element of First array : ");
for (i = 0; i < arr0_length; i++)
arr[0][i] = Convert.ToInt32(Console.ReadLine());

Console.WriteLine ("Enter the length of the second array : ");


arr1_length =Convert.ToInt32(Console.ReadLine());
arr[1] = new int[arr1_length];
Console.WriteLine ("Enter the Element of Second array : ");
for(i=0;i<arr1_length;i++)
arr[1][i]=Convert.ToInt32(Console.ReadLine());

Console.WriteLine ("Enter the length of the Third array : ");


arr2_length =Convert.ToInt32(Console.ReadLine());
arr[2] = new int[arr2_length];
Console.WriteLine ("Enter the Element of Third array : ");
for(i=0;i<arr2_length;i++)
arr[2][i]=Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Array with its length :");


for (i = 0; i < 3; i++)
{
sum = 0;
Console.WriteLine("Elements of Array " + i + "and its Sum ");
Console.WriteLine();
for (j = 0; j< arr[i].Length; j++)
{
Console.Write(arr[i][j] + " ");
sum = sum + arr[i][j];
}
Console.WriteLine(" Sum : "+sum);
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output :

9 18MCA56 – C# .NET
Laboratory
5. Write a Program in C# to demonstrate Array Out of Bound Exception using Try, Catch and Finally blocks.

An exception is a problem that arises during the execution of a program. A C# exception is a response to
an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

Exceptions provide a way to transfer control from one part of a program to another. C# exception handling
is built upon four keywords: try, catch, finally, and throw.

 try: A try block identifies a block of code for which particular exceptions is activated. It is followed
by one or more catch blocks.
 catch: A program catches an exception with an exception handler at the place in a program where
you want to handle the problem. The catch keyword indicates the catching of an exception.
 finally: The finally block is used to execute a given set of statements, whether an exception is
thrown or not thrown. For example, if you open a file, it must be closed whether an exception is
raised or not.
 throw: A program throws an exception when a problem shows up. This is done using a throw
keyword.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program_5
{
class Program

10 18MCA56 – C# .NET
Laboratory
{
static void Main(string[] args)
{
int[] a=new int[]{10,20,30,40,50};
Console.WriteLine("List of elements in the array : ");
try
{
for (int i=0; i<=5; i++)
Console.Write(a[i]+" ");

}
catch(IndexOutOfRangeException e)
{
Console.WriteLine();
Console.WriteLine("Exception Caught:" + e.Message);

}
finally
{
Console.ReadLine();
}
}
}
}

11 18MCA56 – C# .NET
Laboratory
6.Write a Program to Demonstrate Use of Virtual and override key words in C# with a simple program

Overrriding method is basically run time polymorphism in C#. When a subclass contains a method with the
same name and signature as in the supper class then it is called as method overriding.
Virtual method is a method in a base class which the child class can reuse it or redefine and customize its
behavior to be appropriate to its functionality. The virtual keyword is used to modify a method, property,
indexer or event declaration, and allow it to be overridden in a derived class. For example, this method can
be overridden by any class that inherits it:

public virtual double Area()


{
return x * y;
}

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program6
{
public class Customer
{
public virtual void CustomerType()
{
Console.WriteLine("I am customer");
}
}
public class CorporateCustomer : Customer
{
public override void CustomerType()
{
Console.WriteLine("I am a Corporate Customer");
}
}
public class PersonalCustomer : Customer
{
public override void CustomerType()
{
Console.WriteLine("I am a personal customer");
}
}
class Program
{
static void Main(string[] args)
{
Customer[] c = new Customer[3];
c[0] = new Customer();
c[1] = new CorporateCustomer();
c[2] = new PersonalCustomer();

foreach (Customer customerobject in c)


{
customerobject.CustomerType();
}
Console.ReadLine();

12 18MCA56 – C# .NET
Laboratory
}
}
}

7. Write a Program in C# to create and implement a Delegate for any two arithmetic operations

 # delegates are similar to pointers to functions, in C or C++. A delegate is a reference type variable that
holds the reference to a method. The reference can be changed at runtime.
 Delegates are especially used for implementing events and the call-back methods. All delegates are
implicitly derived from the System.Delegate class.
 A delegate can refer to a method, which has the same signature as that of the delegate.

For example, consider a delegate:

public delegate int MyDelegate (string s);

The preceding delegate can be used to reference any method that has a singlestring parameter and returns
an int type variable.

Syntax for delegate declaration is:

delegate <return type> <delegate-name> <parameter list>

public delegate double Operation(double no1, double no2);


class Prg7
{

public static double Addtwonos(double a, double b)


{
return a + b;
}

public static double Multwonos(double a, double b)


{
return a * b;
}

13 18MCA56 – C# .NET
Laboratory
static void Main(string[] args)
{
Operation doaddition = new Operation(Addtwonos);
Operation domultiplication = new Operation(Multwonos);
Console.Write("Enter Number 1 : ");
double n1 = Double.Parse(Console.ReadLine());
Console.Write("Enter Number 2 : ");
double n2 = Double.Parse(Console.ReadLine());
double sum = doaddition(n1, n2);
double mul = domultiplication(n1, n2);
Console.WriteLine("\n{0} + {1} = {2}\n", n1,n2, sum);
Console.WriteLine("\n{0} * {1} = {2}\n", n1, n2, mul);
Console.ReadLine();

}
}

14 18MCA56 – C# .NET
Laboratory
8. Write a Program in C# to demonstrate abstract class and abstract methods in C#.

The abstract keyword enables you to create classes and class members that are incomplete and must be
implemented in a derived class.
An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition
of a base class that multiple derived classes can share. For example, a class library may define an abstract
class that is used as a parameter to many of its functions, and require programmers using that library to
provide their own implementation of the class by creating a derived class.

Abstract classes may also define abstract methods. This is accomplished by adding the
keyword abstract before the return type of the method. For example:

public abstract class A


{
public abstract void DoWork(int i);
}

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program8
{
public abstract class Vehicle
{
public string Name;
public int Wheels;
public double Amount;
public abstract void Calculate();
}
public class Motorcycle : Vehicle
{
public Motorcycle()
{
this.Wheels = 2;
}
public Motorcycle(string s)
{
this.Wheels = 2;
this.Name = s;
}
public override void Calculate()
{
this.Amount = 100000 + (500 * this.Wheels);
Console.WriteLine("This motor cycle name is " + this.Name + " and its price is " + this.Amount);
}
}
public class Car : Vehicle
{
private string EngineType;
public Car()
{

15 18MCA56 – C# .NET
Laboratory
this.Wheels = 4;
}
public Car(string s, string t)
{
this.Wheels = 4;
this.Name = s;
this.EngineType = t;
}
public override void Calculate()
{
this.Amount = 400000 + (500 * this.Wheels) + 8000;
Console.WriteLine("This car name is " + this.Name + " has engine type " + this.EngineType + " and
price " + this.Amount);
}
}

class Program
{
static void Main(string[] args)
{
Vehicle v;
Motorcycle m = new Motorcycle("Pulsar");
Car c = new Car("Jazz", "Tata");
v = m;
v.Calculate();
v = c;
v.Calculate();
Console.ReadLine();

}
}
}

16 18MCA56 – C# .NET
Laboratory
9. Write a program to Set & Get the Name & Age of a person using Properties of C# to illustrate the use of
different properties in C#.

Properties enable a class to expose a public way of getting and setting values, while hiding implementation
or verification code. The get property accessor is used to return the property value, and a set accessor is
used to assign a new value. These accessors can have different access levels. The value keyword is used
to define the value being assigned by the set indexer.

· Properties that do not implement a set method are read only.


· The code block for the get accessor is executed when the property is read; the code block for the
set accessor is executed when the property is assigned a new value.
· A property without a set accessor is considered read-only. A property without a get accessor is
considered write-only. A property with both accessors is read-write.

class Student
{
private string fullName;
private int ageyears;

public string Name


{
get { return fullName; }
set { fullName = value; }
}

public int Age


{
get { return ageyears; }
set
{
ageyears = value;
}
}
}

class Prg9
{
static void Main(string[] args)
{
Student e = new Student();
e.Name = "Saran";
e.Age = 25;
Console.WriteLine("Name ={0}", e.Name);
Console.WriteLine("Age = {0}", e.Age);
Console.ReadLine();

}
}

17 18MCA56 – C# .NET
Laboratory
18 18MCA56 – C# .NET
Laboratory
10. Write a Program in C# Demonstrate arrays of interface types (for runtime polymorphism).

An interface looks like a class, but has no implementation. The only thing it contains are declarations
of events, indexers, methods and/or properties. The reason interfaces only provide declarations is because
they are inherited by classes and structs, which must provide an implementation for each interface .

interface IMyInterface
{
    void MethodToImplement();
}

The above code defines an interface named IMyInterface. A common naming convention is to prefix all
interface names with a capital “I”. This interface has a single method named MethodToImplement(). 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program10
{
public interface IShape
{
void Calculate();
void Display();
}
public class Rectangle : IShape
{
private double Area;
private double Length;
private double Breadth;
public Rectangle()
{
this.Length = 0;
this.Breadth = 0;
}
public Rectangle(double l, double b)
{
this.Length = l;
this.Breadth = b;
}
public void Calculate()
{
this.Area = this.Length * this.Breadth;
}
public void Display()
{
Console.WriteLine("Area of Rectangle is : " + this.Area);
}
}
public class Square : IShape
{
private double Area;
private double Side;
public Square()
{

19 18MCA56 – C# .NET
Laboratory
this.Side = 0;
}
public Square(double s)
{
this.Side = s;
}
public void Calculate()
{
this.Area = this.Side * this.Side;
}
public void Display()
{
Console.WriteLine("Area of Square is : " + this.Area);
}
}
public class Circle : IShape
{
private double Area;
private double Radius;
public Circle()
{
this.Radius = 0;
}
public Circle(double s)
{
this.Radius = s;
}
public void Calculate()
{
this.Area = 3.1416 * this.Radius * this.Radius;
}
public void Display()
{
Console.WriteLine("Area of Circle is : " + this.Area);
}
}
class Program
{
static void Main(string[] args)
{
IShape[] s = { new Rectangle(10, 20), new Square(30), new Circle(40) };
for (int i = 0; i < s.Length; i++)
{
s[i].Calculate();
s[i].Display();
}
Console.ReadLine();
}
}
}

20 18MCA56 – C# .NET
Laboratory
21 18MCA56 – C# .NET
Laboratory
PART - B

1. Consider the Database db_EMS (Employee Management System) consisting of the following tables :

tbl_Designations (IdDesignation: int, Designation: string)


tbl_EmployeeDetails(IdEmployee: int, EmployeeName: string, ContactNumber: string, IdDesignation:
int, IdReportingTo: int)

Develop a suitable window application using C#.NET having following options.

1. Enter new Employee details with designation & Reporting Manager.


2. Display all the Project Leaders (In a Grid) reporting to selected Project Managers (In a Combo box).
3. Display all the Engineers (In a Grid) reporting to selected Project Leader (In a Combo box).
4. Display all the Employees (In a Grid) with their reporting Manager (No Value for PM).

Form1.cs

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
Add_Employee newemp = new Add_Employee();
newemp.Show();

1|Page

22 18MCA56 – C# .NET
Laboratory
}

private void button2_Click(object sender, EventArgs e)


{
Display_PL disppl = new Display_PL();
disppl.Show();
}

private void button3_Click(object sender, EventArgs e)


{
Display_Engg dispengg = new Display_Engg();
dispengg.Show();
}

private void button4_Click(object sender, EventArgs e)


{
Display_Emp dispemp = new Display_Emp();
dispemp.Show();
}
}
}

Add_Employee.cs

using System.Data;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
public partial class Add_Employee : Form
{
private OleDbConnection conn;
private OleDbCommand oleDbCmd = new OleDbCommand();
private String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_EMS.accdb";

public Add_Employee()
{
conn = new OleDbConnection(connStr);
InitializeComponent();

private void Add_Employee_Load(object sender, EventArgs e)


{
fillDesigCombo();
ManagerCombo();
}

public void fillDesigCombo()


{

String query = "select DISTINCT * from tbl_Designations";


try
{
conn.Open();
2|Page
OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(query,
conn));
DataSet ds = new DataSet(); adapter.Fill(ds); comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "Designation";
comboBox1.ValueMember = "IdDesignation";
}
catch (OleDbException e)
{
Console.WriteLine(e.ToString()); Console.ReadLine();
return;
}
finally
{
conn.Close();
}
}

public void ManagerCombo()


{

String query = "select IdEmployee, EmployeeName from tbl_EmployeeDetails";


try
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(query,
conn));
DataSet ds = new DataSet(); adapter.Fill(ds); comboBox2.DataSource = ds.Tables[0];
comboBox2.DisplayMember = "EmployeeName";
comboBox2.ValueMember = "IdEmployee";
}
catch (OleDbException e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
return;
}
finally
{
conn.Close();
}
}

private void button1_Click(object sender, EventArgs e)


{
conn.Open();
oleDbCmd.Connection = conn;
int desigid = (int)this.comboBox1.SelectedValue;
int repto = Convert.ToInt16(comboBox2.SelectedValue);
int empid = Convert.ToInt32(this.textBox1.Text);
String str = "insert into tbl_EmployeeDetails(IdEmployee, EmployeeName,
ContactNumber,IdDesignation, IdReportingTo) values (" + empid + ",'" + textBox2.Text + "','" + textBox3.Text + "'," + +desigid
+ "," + repto + ");";

3|Page
oleDbCmd.CommandText = str;
int temp = oleDbCmd.ExecuteNonQuery();
if (temp > 0)
{
MessageBox.Show("New Employee detail is Added");
}
else
{
MessageBox.Show("Record Fail to Added");
}
conn.Close();
}
}
}

Display_PL.cs

using System.Data;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
public partial class Display_PL : Form
{
private OleDbConnection conn;
private OleDbCommand oleDbCmd = new OleDbCommand();
private String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_EMS.accdb";
public Display_PL()
{
conn = new OleDbConnection(connStr); InitializeComponent();

private void Display_PL_Load(object sender, EventArgs e)


{
ManagerCombo();
}
public void ManagerCombo()
{

String query = "select IdEmployee, EmployeeName from tbl_EmployeeDetails where IdReportingTo=0";


try
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(query,
conn));
DataSet ds = new DataSet(); adapter.Fill(ds); comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "EmployeeName";
comboBox1.ValueMember = "IdEmployee";
}
catch (OleDbException e)
{

4|Page
Console.WriteLine(e.ToString()); Console.ReadLine();
return;
}
finally
{
conn.Close();
}
}

private void button1_Click(object sender, EventArgs e)


{
int repto = Convert.ToInt16(comboBox1.SelectedValue);
String query = "select * from tbl_EmployeeDetails where IdReportingTo = " +
repto;
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(query, conn); DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;

conn.Close();
}
}
}

Display_Engg.cs

using System.Data;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
public partial class Display_Engg : Form
{
private OleDbConnection conn;
private OleDbCommand oleDbCmd = new OleDbCommand();
private String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_EMS.accdb";
public Display_Engg()
{
conn = new OleDbConnection(connStr); InitializeComponent();
}

private void Display_Engg_Load(object sender, EventArgs e)


{
ManagerCombo();
}

private void button1_Click(object sender, EventArgs e)


{
int repto = Convert.ToInt16(comboBox1.SelectedValue);
5|Page
String query = "select * from tbl_EmployeeDetails where IdReportingTo = " +
repto;
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(query, conn); DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;

conn.Close();

public void ManagerCombo()


{

String query = "select IdEmployee, EmployeeName from tbl_EmployeeDetails where IdDesignation=2";


try
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(query,
conn));
DataSet ds = new DataSet(); adapter.Fill(ds); comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "EmployeeName";
comboBox1.ValueMember = "IdEmployee";
}
catch (OleDbException e)
{
Console.WriteLine(e.ToString()); Console.ReadLine();
return;
}
finally
{
conn.Close();
}
}

}
}

Display_Emp.cs

using System.Data;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
public partial class Display_Emp : Form
{
private OleDbConnection conn;
private OleDbCommand oleDbCmd = new OleDbCommand();
private String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_EMS.accdb";

6|Page
.Net Laboratory – 16MCA 57 Part – B

public Display_Emp()
{
conn = new OleDbConnection(connStr);
InitializeComponent();
}

private void Display_Emp_Load(object sender, EventArgs e)


{

String query = "select a.IdEmployee, a.EmployeeName,b.EmployeeName FROM


tbl_EmployeeDetails a, tbl_EmployeeDetails b WHERE a.IdReportingTo = b.IdEmployee";

conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(query, conn); DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;

conn.Close();
}
}
}

Form1.cs
7|Page
Add_Employee.cs : Employee details with designation & Reporting Manager

Display_PL.cs
Display_Emp.cs

Display_Engg.cs
9|Page
2. Consider the Database db_LSA (Lecturer Subject Allocation) consisting of the following tables:

tbl_Subjects(IdSubject: int, SubjectCode: string, SubjectName: string) tbl_Lecturers(IdLecturer: int,


LecturerName: string, ContactNumber: string) tbl_LecturerSubjects(IdSubject: int, SubjectCode: string,
IdLecturer: int)

Develop a suitable window application using C#.NET having following options.


1. Enter new Subject Details.
2. Enter New Lecturer Details.
3. Subject Allocation with Lecturer Name in a Combo box and subjects to be allocated in Grid with
checkbox
Column.
4. Display all the subjects allocated (In a Grid) to the selected Lecturer (In a Combo Box).

using System;
using System.Data;
using System.Data.OleDb;

namespace Lecturer_Subject_Allocation
{
public partial class Form1 : Form
{
private OleDbConnection conn;
private OleDbCommand oleDbCmd = new OleDbCommand();
private String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_LSA.accdb";
public Form1()
{
conn = new OleDbConnection(connStr); InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
// TODO: This line of code loads data into the 'db_LSADataSet.tbl_Subjects' table. You can move, or remove it, as needed.
fillLecturer();
this.tbl_SubjectsTableAdapter.Fill(this.db_LSADataSet.tbl_Subjects);

private void button1_Click(object sender, EventArgs e)


{
conn.Open();
oleDbCmd.Connection = conn;
String str = "insert into tbl_Subjects(SubjectCode, SubjectName) values ('" +
textBox1.Text + "','" + textBox2.Text + "');";
oleDbCmd.CommandText = str;
int temp = oleDbCmd.ExecuteNonQuery();
if (temp > 0)
{
MessageBox.Show("New Subject has been Added");
}
else
{
MessageBox.Show("Re
cord Fail to Add");
}
conn.Close();
}

private void button4_Click(object sender, EventArgs e)


{
conn.Open();
oleDbCmd.Connection = conn;
String str = "insert into tbl_Lecturers(LecturerName, ContactNumber) values
('" + textBox3.Text + "','" + textBox4.Text + "');";
oleDbCmd.CommandText = str;
int temp = oleDbCmd.ExecuteNonQuery();
if (temp > 0)
{
MessageBox.Show("New Lecturer has been Added");
}
else
{
MessageBox.Show("Record Fail to Add");
}
conn.Close();
}

public void fillLecturer()


{

String query = "select DISTINCT IdLecturer, LecturerName from tbl_Lecturers";


try
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(query,
conn));
DataSet ds = new DataSet(); adapter.Fill(ds); comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "LecturerName"; comboBox1.ValueMember = "IdLecturer";
comboBox2.DataSource = ds.Tables[0]; comboBox2.DisplayMember = "LecturerName";
comboBox2.ValueMember = "IdLecturer";
}
catch (OleDbException e)
{
Console.WriteLine(e.ToString()); Console.ReadLine();
return;
}
finally
{
conn.Close();
}
}

private void button7_Click(object sender, EventArgs e)


{
int subid = 0;
string subcode="";

int totsub = 0;
for (int i=0; i<=dataGridView1.RowCount-1;i++)
{
if
(Convert.ToBoolean(dataGridView1.Rows[i].Cells["Column1"].Value)==true)
{
subid = Convert.ToInt16(dataGridView1.Rows[i].Cells[1].Value);
subcode = dataGridView1.Rows[i].Cells[2].Value.ToString();

/* Adding the record in tbl_LecturerSubjects table */

conn.Open();
int lecid = Convert.ToInt16(comboBox1.SelectedValue);
oleDbCmd.Connection = conn;
String str = "insert into tbl_LecturerSubjects(IdSubject,SubjectCode, IdLecturer) values (" + subid +",'"+ subcode
+ "'," + lecid + ");";
oleDbCmd.CommandText = str;
int temp = oleDbCmd.ExecuteNonQuery();
if (temp > 0)
{
totsub = totsub + 1;
}
else
{
MessageBox.Show("Record Fail to Add");
}
conn.Close();

}
}
MessageBox.Show(totsub.ToString() + "subjects has been allocated");
}

private void button8_Click(object sender, EventArgs e)


{
int lecturerid = 0;
lecturerid = Convert.ToInt32(comboBox2.SelectedValue);

String query = "select IdSubject, SubjectCode from tbl_LecturerSubjects where


IdLecturer = " + lecturerid;
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(query, conn); DataTable dt = new DataTable();
da.Fill(dt);
dataGridView2.DataSource = dt;

conn.Close();
}

private void button3_Click(object sender, EventArgs e)


{

this.Close();

private void button2_Click(object sender, EventArgs e)


{
textBox1.Text = "";
textBox2.Text = "";
}

private void button5_Click(object sender, EventArgs e)


{
textBox3.Text = "";
textBox4.Text = "";
}

private void button6_Click(object sender, EventArgs e)


{
this.Close();
}

private void button9_Click(object sender, EventArgs e)


{

String query = "select distinct IdSubject, SubjectCode, SubjectName from tbl_Subjects";


conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(query, conn); DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;

conn.Close();
}
}
}

.Net Laboratory – 16MCA 57 Part – B


3. Consider the database db_VSS (Vehicle Service Station) consisting of the following tables:

tbl_VehicleTypes(IdVehicleType: int, VehicleType: string, ServiceCharge: int) tbl_ServiceDetails(IdService:


int, VehicleNumber: string, ServiceDetails: string, IdVehicleType: int) Develop a suitable window
application using C#.NET having following options.
1. Enter new Service Details for the Selected Vehicle Type (In a Combo Box).
2. Update the Existing Service Charges to Database.
3. Total Service Charges Collected for the Selected Vehicle (In a Combo box) with total amount displayed
in a text box.

NOTE: tbl_VehicleType is a static table containing the following Rows in it.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb;

namespace Vehicle_Service_Station
{
public partial class Form1 : Form
{

private OleDbConnection conn;


private OleDbCommand oleDbCmd = new OleDbCommand();
private String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_VSS.accdb";
public Form1()
{
conn = new OleDbConnection(connStr);
InitializeComponent();
}

private void newServiceToolStripMenuItem_Click(object sender, EventArgs e)


{
Add_Service newser = new Add_Service();
newser.Show();
}

private void updateChargesToolStripMenuItem_Click(object sender, EventArgs e)


{
Update_Serv upserv = new Update_Serv();

upserv.Show();
}

private void updateSToolStripMenuItem_Click(object sender, EventArgs e)


{
Calc_total Totchar = new Calc_total();
Totchar.Show();

}
}

Add_Service

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing; using System.Linq; using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Vehicle_Service_Station
{
public partial class Add_Service : Form
{
private OleDbConnection conn;
private OleDbCommand oleDbCmd = new OleDbCommand();
private String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_VSS.accdb";
public Add_Service()
{
conn = new OleDbConnection(connStr); InitializeComponent();
}

private void Add_Service_Load(object sender, EventArgs e)


{
fillservice_charges();
}
public void fillservice_charges()
{

String query = "select DISTINCT IdVehicleType, VehicleType from tbl_VehicleTypes";


try
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(query,
conn));
DataSet ds = new DataSet();
adapter.Fill(ds);
comboBox1.DataSource = ds.Tables[0]; comboBox1.DisplayMember = "VehicleType"; comboBox1.ValueMember =
"IdVehicleType";

}
catch (OleDbException e)
{
Console.WriteLine(e.ToString()); Console.ReadLine();
return;
}
finally
{
conn.Close();
}
}

private void button1_Click(object sender, EventArgs e)


{
conn.Open();
oleDbCmd.Connection = conn;

int vech_id = (int)this.comboBox1.SelectedValue;

String str = "insert into tbl_ServiceDetails( VehicleNumber, ServiceDetails, IdVehicleType) values ('" + textBox1.Text + "','" +
textBox2.Text + "'," + vech_id + ");";
oleDbCmd.CommandText = str;
int temp = oleDbCmd.ExecuteNonQuery();
if (temp > 0)
{
MessageBox.Show("New Service detail is Added");
}
else
{
MessageBox.Show("Record Fail to Added");
}
conn.Close();
}

private void button2_Click(object sender, EventArgs e)


{
textBox1.Text = "";
textBox2.Text = "";
}

private void button3_Click(object sender, EventArgs e)


{
this.Close();
}

}
}

Update_Serv

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb;

namespace Vehicle_Service_Station
{
public partial class Update_Serv : Form
{
private OleDbConnection conn;
private OleDbCommand oleDbCmd = new OleDbCommand();

private String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data


Source=C:\Users\Helen\Documents\db_VSS.accdb";

public Update_Serv()
{
conn = new OleDbConnection(connStr); InitializeComponent();
}

private void Update_Serv_Load(object sender, EventArgs e)


{

load_Vechicletype();

public void load_Vechicletype()


{

String query = "select DISTINCT IdVehicleType, VehicleType from tbl_VehicleTypes";


try
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(query,
conn));
DataSet ds = new DataSet(); adapter.Fill(ds); comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "VehicleType";
comboBox1.ValueMember = "IdVehicleType";

}
catch (OleDbException e)
{
Console.WriteLine(e.ToString());

Console.ReadLine();
return;
}
finally
{
conn.Close();
}
}

private void button3_Click(object sender, EventArgs e)


{
//string vech_id = (comboBox1.GetItemText(comboBox1.SelectedItem)).ToString();
//int vec_id = Convert.ToInt32(vech_id);
int vech_id = Convert.ToInt16(comboBox1.SelectedValue);
//select ServiceCharges from tbl_VehicleTypes where
IdVehicleType=1
String query = "select ServiceCharges from tbl_VehicleTypes where
IdVehicleType=" + vech_id;

conn.Open(); oleDbCmd.Connection = conn; oleDbCmd.CommandText =query;

int n = Convert.ToInt32((oleDbCmd.ExecuteScalar()));
textBox1.Text = Convert.ToString(n);
conn.Close();

private void button1_Click(object sender, EventArgs e)


{
conn.Open();
oleDbCmd.Connection = conn;

int vech_id = (int)this.comboBox1.SelectedValue;

int newcharge=Convert.ToInt32(textBox1.Text);
// String str = "update tbl_ServiceDetails set ( VehicleNumber, ServiceDetails, IdVehicleType) values ('" +
textBox1.Text + "','" + textBox2.Text + "',"
+ vech_id + ");";
String str ="update tbl_VehicleTypes SET ServiceCharges = "+newcharge+" where
IdVehicleType="+vech_id;

oleDbCmd.CommandText = str;
int temp = oleDbCmd.ExecuteNonQuery();
if (temp > 0)
{
}
}
else
{
MessageBox.Show("Re
cord updated");

MessageBox.Show("Re
cord Fail to Update");
conn.Close();

//update tab1,tab2 set tab1.f1 = tab2.f2 where tab1.f2 = tab2.f3"


}
}
}

Calc_total

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb;

namespace Vehicle_Service_Station
{
public partial class Calc_total : Form
{
private OleDbConnection conn;
private OleDbCommand oleDbCmd = new OleDbCommand();

private String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data


Source=C:\Users\Helen\Documents\db_VSS.accdb";

public Calc_total()

{
conn = new OleDbConnection(connStr);
InitializeComponent();
}

private void Calc_total_Load(object sender, EventArgs e)


{
fillservice_charges();
}
public void fillservice_charges()
{

String query = "select DISTINCT IdVehicleType, VehicleType from tbl_VehicleTypes";


try
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(query,
conn));
DataSet ds = new DataSet(); adapter.Fill(ds); comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "VehicleType";
comboBox1.ValueMember = "IdVehicleType";
}
catch (OleDbException e)
{
Console.WriteLine(e.ToString()); Console.ReadLine();
return;
}
finally
{
conn.Close();
}
}

private void button3_Click(object sender, EventArgs e)


{
int vech_id = Convert.ToInt16(comboBox1.SelectedValue);
String query ="SELECT tot_vehicle*t2.ServiceCharges FROM (SELECT tbl_ServiceDetails.IdVehicleType,
Count(tbl_ServiceDetails.IdVehicleType) AS tot_vehicle FROM tbl_ServiceDetails GROUP BY
tbl_ServiceDetails.IdVehicleType) AS t1 LEFT JOIN tbl_VehicleTypes AS t2 ON t1.IdVehicleType = t2.IdVehicleType WHERE
(((t2.IdVehicleType)="+vech_id+"))";

conn.Open(); oleDbCmd.Connection = conn; oleDbCmd.CommandText = query;

int n = Convert.ToInt32((oleDbCmd.ExecuteScalar()));
textBox1.Text = Convert.ToString(n);
conn.Close();
}
}
}
Output
4. Develop a web application using C#.NET and ASP.NET for the Postal System Management. The master
page should contain the hyper links for adding Area Details, Postman details, Letter distributions and
View Letters.

Consider the database db_PSM (Postal System Management) consisting of the following tables:
tbl_AreaDetails(IdArea: int, AreaName: string)
tbl_PostmanDetails(IdPostman: int, PostmanName: string, ContactNumber: string, IdArea: int)
tbl_AreaLetters(IdLetter: int, LetterAddress: string, IdArea: int)
Develop the suitable content pages for the above created 4 hyper links with the following details:
1. Enter New Area Details
2. Enter New Postman Details with the Area he/she is in-charge of (display Area in a Combo box)
3. Enter all the Letters distributed to the selected Area (display Area in a Combo box)
4. Display all the Letter addresses (In a Grid) to be distributed by the selected Postman (In a Combo box)

area_details

using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class area_details : System.Web.UI.Page


{

protected void Button1_Click(object sender, EventArgs e)


{

String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data


Source=C:\Users\Helen\Documents\db_PSM.accdb;Persist Security Info=False;"; OleDbConnection con = new
OleDbConnection(connStr);
con.Open();
string query = "insert into tbl_AreaDetails (IdArea,AreaName) values ('" + TextBox1.Text + "','" + TextBox2.Text + "')";
OleDbCommand cmd = new OleDbCommand(query, con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script language='javascript'>alert('Inserted
Succesfully')</script>");
}
else
{
Response.Write("<script language='javascript'>alert('Insertion
Failed')</script>");

}
}
}

PostMan_details

using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class PostMan_details : System.Web.UI.Page


{
String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_PSM.accdb;Persist Security Info=False;";

protected void Page_Load(object sender, EventArgs e)


{

load_areaname();
}

public void load_areaname()


{
if (!IsPostBack)
{
using (OleDbConnection con = new OleDbConnection(connStr))
{
con.Open();

String query="select IdArea,AreaName from tbl_AreaDetails"; OleDbCommand cmd = new OleDbCommand(query, con);

DropDownList1.DataSource=cmd.ExecuteReader();

DropDownList1.DataTextField = "AreaName"; DropDownList1.DataValueField = "IdArea"; DropDownList1.DataBind();


con.Close();
}
}
DropDownList1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
}

protected void Button3_Click(object sender, EventArgs e)


{
OleDbConnection con = new OleDbConnection(connStr);
con.Open();

string query = "insert into tbl_PostmanDetails (PostmanName,ContactNumber,IdArea) values ('" + TextBox1.Text + "','" +
TextBox2.Text + "',"+ DropDownList1.SelectedValue+")";
OleDbCommand cmd = new OleDbCommand(query, con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script language='javascript'>alert('Inserted
Succesfully')</script>");
}

else
{
Response.Write("<script language='javascript'>alert('Insertion
Failed')</script>");

}
}
}

Distrib_letters

using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class Distrib_letters : System.Web.UI.Page


{
String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_PSM.accdb;Persist Security Info=False;";
protected void Page_Load(object sender, EventArgs e)
{
load_area();
}
public void load_area()
{
if (!IsPostBack)
{
using (OleDbConnection con = new OleDbConnection(connStr))
{
con.Open();

String query = "select IdArea,AreaName from tbl_AreaDetails"; OleDbCommand cmd = new OleDbCommand(query, con);

DropDownList1.DataSource = cmd.ExecuteReader();

DropDownList1.DataTextField = "AreaName"; DropDownList1.DataValueField = "IdArea"; DropDownList1.DataBind();


con.Close();
}
}
DropDownList1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
}
protected void Button3_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(connStr);
con.Open();

string query = "insert into tbl_AreaLetters (LetterAddress,IdArea) values ('" + TextBox2.Text + "'," +
DropDownList1.SelectedValue + ")";
OleDbCommand cmd = new OleDbCommand(query, con);
int i = cmd.ExecuteNonQuery();
if (i > 0)

{
Response.Write("<script language='javascript'>alert('Inserted
Succesfully')</script>");
}
else
{
Response.Write("<script language='javascript'>alert('Insertion
Failed')</script>");
}
}
}

View_letter

using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class View_letter : System.Web.UI.Page


{
String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_PSM.accdb;Persist Security Info=False;";
protected void Page_Load(object sender, EventArgs e)
{
load_Postman_details();
}
public void load_Postman_details()
{
if (!IsPostBack)
{
using (OleDbConnection con = new OleDbConnection(connStr))
{
con.Open();

String query = "select IdArea,PostmanName from tbl_PostmanDetails"; OleDbCommand cmd = new OleDbCommand(query,
con);

DropDownList1.DataSource = cmd.ExecuteReader();

DropDownList1.DataTextField = "PostmanName"; DropDownList1.DataValueField = "IdArea"; DropDownList1.DataBind();


con.Close();
}
}
DropDownList1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(connStr);
try
{

con.Open();
string query = "select IdLetter, LetterAddress from tbl_AreaLetters where
IdArea=" + DropDownList1.SelectedValue;
OleDbCommand cmd = new OleDbCommand(query, con); OleDbDataAdapter da = new OleDbDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); con.Close();


}
catch (Exception ex)
{
Response.Write(ex.Message);
}

}
protected void Button1_Click(object sender, EventArgs e)
{

}
}
5. Develop a web application using C#.NET and ASP.NET for the Complaint Management System. The
master page should contain the hyper links for Add Engineer, Complaint Registration, Complaint
Allocation and View Complaints.

Consider the database db_CMS (Complaint Management System) consisting of the following tables:
tbl_Departments(IdDepartment: int, DepartmentName: string)
tbl_Engineers(IdEngineer: int, EngineerName: string, ContactNumber: string, IdDepartment: int)
tbl_RegisteredComplaints(IdComplaint: int, ComplaintDescription: string)
tbl_DepartmentComplaints(IdDepartment: int, IdComplaint: int)
Develop the suitable content pages for the above created 4 hyper links with the following details:
1. Enter New Engineers belonging to the selected department (displayed in a combo box)
2. Register a new Complaint with a submit button.
3. View all registered complaints & allocate to the corresponding department (displayed in a combo box)
4. Display all the Complaints (In a Grid) to be handled by the selected Engineer (In a Combo box)

NOTE: Consider the table tbl_Departments as a static table containing some pre-entered departments,
which are displayed in all the remaining modules

Add_Engg

using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class Add_Engg : System.Web.UI.Page


{
String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_CMS.accdb;Persist Security Info=False;";
protected void Page_Load(object sender, EventArgs e)
{
load_department();
}
public void load_department()
{
if (!IsPostBack)
{
using (OleDbConnection con = new OleDbConnection(connStr))
{
con.Open();

String query = "select IdDepartment,DepartmentName from tbl_Departments"; OleDbCommand cmd = new


OleDbCommand(query, con);
DropDownList1.DataSource = cmd.ExecuteReader(); DropDownList1.DataTextField = "DepartmentName";
DropDownList1.DataValueField = "IdDepartment"; DropDownList1.DataBind();
con.Close();

}
}
DropDownList1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
}

protected void Button1_Click(object sender, EventArgs e)


{
OleDbConnection con = new OleDbConnection(connStr);
con.Open();

string query = "insert into tbl_Engineers (EngineerName,ContactNumber,IdDepartment) values ('" + TextBox1.Text + "','" +
TextBox2.Text + "'," + DropDownList1.SelectedValue + ")";
OleDbCommand cmd = new OleDbCommand(query, con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script language='javascript'>alert('Inserted
Succesfully')</script>");
}
else
{
Response.Write("<script language='javascript'>alert('Insertion
Failed')</script>");

}
}
}

Reg_Compliant.cs

using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class Reg_Complaint : System.Web.UI.Page


{
String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_CMS.accdb;Persist Security Info=False;";
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(connStr);
con.Open();

string query = "insert into tbl_RegisteredComplaints (ComplaintDescription)


values ('" + TextBox1.Text + "')";
OleDbCommand cmd = new OleDbCommand(query, con);

int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script language='javascript'>alert('Inserted
Succesfully')</script>");
}
else
{
Response.Write("<script language='javascript'>alert('Insertion
Failed')</script>");

}
}
}

Complaint_Allocation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class Complaint_Allocation : System.Web.UI.Page


{
String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_CMS.accdb;Persist Security Info=False;";
protected void Page_Load(object sender, EventArgs e)
{
load_department();
load_complaint();
}
public void load_department()
{
if (!IsPostBack)
{
using (OleDbConnection con = new OleDbConnection(connStr))
{
con.Open();

String query = "select IdDepartment,DepartmentName from tbl_Departments"; OleDbCommand cmd = new


OleDbCommand(query, con);

DropDownList2.DataSource = cmd.ExecuteReader();

DropDownList2.DataTextField = "DepartmentName"; DropDownList2.DataValueField = "IdDepartment";


DropDownList2.DataBind();
con.Close();
}
}
}

public void load_complaint()


{
if (!IsPostBack)
{
using (OleDbConnection con = new OleDbConnection(connStr))
{
con.Open();

String query = "select IdComplaint, ComplaintDescription from tbl_RegisteredComplaints";


OleDbCommand cmd = new OleDbCommand(query, con);

DropDownList1.DataSource = cmd.ExecuteReader();

DropDownList1.DataTextField = "ComplaintDescription"; DropDownList1.DataValueField = "IdComplaint";


DropDownList1.DataBind();
con.Close();
}
}

}
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(connStr);
con.Open();

string query = "insert into tbl_DepartmentComplaints (IdDepartment,IdComplaint)


values (" + DropDownList1.SelectedValue + ","+DropDownList2.SelectedValue + ")";
OleDbCommand cmd = new OleDbCommand(query, con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script language='javascript'>alert('Inserted
Succesfully')</script>"); DropDownList1.SelectedItem.Enabled = false;
}
else
{

Response.Write("<script language='javascript'>alert('Insertion
Failed')</script>");

}
}
}

View_Complaint

using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class View_Complaint : System.Web.UI.Page


{
String connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\Helen\Documents\db_CMS.accdb;Persist Security Info=False;";
protected void Page_Load(object sender, EventArgs e)
{
load_engineer();
}
public void load_engineer()
{
if (!IsPostBack)
{
using (OleDbConnection con = new OleDbConnection(connStr))
{
con.Open();

String query = "select EngineerName,IdDepartment from tbl_Engineers"; OleDbCommand cmd = new


OleDbCommand(query, con);

DropDownList1.DataSource = cmd.ExecuteReader();

DropDownList1.DataTextField = "EngineerName"; DropDownList1.DataValueField = "IdDepartment";


DropDownList1.DataBind();
con.Close();
}
}

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(connStr);
try
{
con.Open();
string query = "select ComplaintDescription from tbl_RegisteredComplaints
where IdComplaint IN (Select IdComplaint from tbl_DepartmentComplaints where IdDepartment
="+ DropDownList1.SelectedValue+")";
OleDbCommand cmd = new OleDbCommand(query, con);

OleDbDataAdapter da = new OleDbDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); con.Close();


}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}

You might also like