C# Lab Report VTU

Download as pdf or txt
Download as pdf or txt
You are on page 1of 75

.

NET LABORATORY (18MCA56)

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.

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

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Square root of "+args[0]+" given number is: " +
Math.Sqrt(Convert.ToDouble(args[0])));
Console.Read();
}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

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

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

namespace sumaverage
{
class Program
{
static void Main(string[] args)
{
float sum = 0;
int i = 0;
while (i < 3)
{
sum += Convert.ToInt64(args[i]);
i = i + 1;

}
Console.WriteLine("sum of all the numbers:" + sum + "\naverage of those numbers:" + sum / 3);
Console.Read();
}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

2. Write a Program in C# to demonstrate the following

a) Boxing and Unboxing.

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

namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
ArrayList a = new ArrayList();
a.Add(10);
a.Add(10.4);
a.Add(false);
int x = (int)a[0];
double y = (double)a[1];
bool z = (bool)a[2];
Console.WriteLine("value of x is:" + x);
Console.WriteLine("value of Y is:" + y);
Console.WriteLine("value of z is:" + z);
Console.ReadLine();
}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

b) Invalid Unboxing.

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

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
int i = 143;
object o = i;
try
{
int j = (short)o;
Console.WriteLine("unboxing");
}
catch (System.InvalidCastException e)
{
Console.WriteLine("{0} Error: Incorrect unboxing", e.Message);
}
Console.ReadLine();
}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

3. Write a program in C# to add Two complex numbers using Operator


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

namespace sab3
{
class Program
{
public struct Complex
{
public int real;
public int img;
public Complex(int r, int img)
{
this.real = r;
this.img = img;
}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.img + c2.img);
}
public override string ToString()
{
return (String.Format("{0}+{1}i",real,img));
}
}

static void Main(string[] args)


{
Complex n1 = new Complex(2,3);
Complex n2 = new Complex(3,4);
Complex sum = n1 + n2;
Console.WriteLine("First Complex Number:{0}",n1);
Console.WriteLine("Second Complex Number:{0}" , n2);
Console.WriteLine("First Complex Number:{0}" ,asum);
Console.ReadLine();

}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

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

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

namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int[][] jag = new int[3][];
int sum = 0;
jag[0] = new int[] { 1, 1, 1, 6, 7 };
jag[1] = new int[] { 1, 1, 1};
jag[2] = new int[] { 1, 1, 1};
for (int i = 0; i<jag.Length; i++)
{
sum = 0;
for (int j = 0; j < jag[i].Length; j++)
{
sum = sum + jag[i][j];
}
Console.WriteLine("Sum of rows:" + sum);
}
Console.ReadLine();

}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

5. Write a Program in C# to demonstrate Array Out of Bound Exception


using Try, Catch and Finally blocks.

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

namespace lab5
{
class Program
{
static void Main(string[] args)
{
int a,b,c;
Console.WriteLine("Enter a and b : ");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
try
{
c = a / b;
Console.WriteLine("C="+c);

}
catch (DivideByZeroException e)
{
Console.WriteLine("Cannot divide by zero");
}
catch (ArithmeticException e)
{
Console.WriteLine("Error in arithmetic exception");
}

finally
{
Console.WriteLine("Program Ends");
}
Console.ReadLine();
}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

6. Demonstrate Use of Virtual and override key words in C# with a simple program.

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

namespace ConsoleApplication6
{
class shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing shape .... ");
}
}

class circle : shape


{
public override void Draw()
{
Console.WriteLine("Drawing circle. .... ");
}
}
class Rectangle : shape
{
public override void Draw()
{
Console.WriteLine("Drawing Rectangle. ..... ");
}
}

class Program
{
static void Main(string[] args)
{
shape tshape = new shape();
tshape = new circle();
tshape.Draw();
tshape = new Rectangle();
tshape.Draw();
Console.ReadLine();
}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

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


arithmetic operations using System

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

delegate int arthop(int x,int y);


namespace ConsoleApplication2
{
class MathOperation
{
public static int add(int a, int b)
{
return (a + b);
}
public static int sub(int a, int b)
{
return (a - b);
}
}
class Program
{
static void Main(string[] args)
{
arthop operation1 = new arthop(MathOperation.add);
arthop operation2 = new arthop(MathOperation.sub);
int result1 = operation1(200, 100);
int result2 = operation2(200, 100);
Console.WriteLine("Result1 = " + result1);
Console.WriteLine("Result2 = " + result2);
Console.ReadLine();

}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

8. Write a Program in C# to demonstrate abstract class and abstract


methods in C#.

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

namespace ConsoleApplication3
{
abstract class Department
{
string HOD;
int no_of_faculty;
public string hod_name
{
get
{
return HOD;
}
set
{
HOD = value;
}
}
public int nfaculty
{
get
{
return no_of_faculty;
}
set
{
no_of_faculty = value;
}
}
public abstract void getdetail();
public abstract void displaydetail();
}

class MCA : Department


{
public override void getdetail()
{
Console.WriteLine("Enter MCA hod name");

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

hod_name = Console.ReadLine();
Console.WriteLine("Enter number of faculties in MCA");
nfaculty = int.Parse(Console.ReadLine());
}
public override void displaydetail()
{
Console.WriteLine("MCA hod name={0}", hod_name);
Console.WriteLine("Number of faculties={0}", nfaculty);
}
}

class MBA : Department


{
public override void getdetail()
{
Console.WriteLine("Enter MBA hod name");
hod_name = Console.ReadLine();
Console.WriteLine("Enter number of faculties in MCA");
nfaculty = int.Parse(Console.ReadLine());
}

public override void displaydetail()


{
Console.WriteLine("MBA hod name={0}", hod_name);
Console.WriteLine("Number of faculties={0}", nfaculty);
}
}

class Program
{
static void Main(string[] args)
{
MCA mc = new MCA();
MBA mb = new MBA();
mc.getdetail();
mb.getdetail();
mc.displaydetail();
mb.displaydetail();
Console.ReadLine();

}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

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#

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

namespace ConsoleApplication4
{
class person
{
string name;
int age;

public string pname


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

public int page


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

class Program
{
static void Main(string[] args)
{
person obj = new person();
Console.WriteLine("enter your name");
obj.pname = Console.ReadLine();
Console.WriteLine("enter your age");
obj.page = int.Parse(Console.ReadLine());
Console.WriteLine(" ------ ");
Console.WriteLine("name={0} age={1}", obj.pname, obj.page);
Console.ReadLine();

}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

10. Write a Program in C# Demonstrate arrays of interface types (for


runtime polymorphism).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
public interface Ishape
{ double Area(double x); }

public class Square : Ishape


{
public double Area(double x)
{
Console.Write("Area of square:");
return (x * x);
} }

public class Circle : Ishape


{
public double Area(double x)
{
Console.Write("Area of circle:");
return (Math.PI * x * x);
} }

class Program
{
static void Main(string[] args)
{
Ishape[] sh = { new Square(), new Circle() };
for (int i = 0; i<sh.Length; i++)
{
if (sh[i] is Ishape)
{
Ishape iss = (Ishape)sh[i];
Console.WriteLine(iss.Area(20.2));
}
}
Console.ReadLine();
}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

PART - B

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

I. 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)

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

1 Project Manager
2 Project Leader
3 Engineer

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

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Partb1
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();

private void addEmployeeToolStripMenuItem_Click(object sender,


EventArgs e)
{
Form5 frm = new Form5();
frm.Show();
this.Hide();
}
private void projectLeaderToolStripMenuItem_Click(object
sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
this.Hide();
}
private void managerToolStripMenuItem_Click(object sender,
EventArgs e)
{
Form3 frm = new Form3();
frm.Show();
this.Hide();
}
private void reportToolStripMenuItem_Click(object sender,
EventArgs e)
{
Form4 frm = new Form4();

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

frm.Show();
this.Hide();
}

private void selectToolStripMenuItem_Click(object sender,


EventArgs e)
{

}
}
}

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Partb1
{
public partial class Form5 : Form
{

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

SqlConnection scon = new SqlConnection(@"Data


Source=.\SQLEXPRESS;AttachDbFilename=E:\Partb1\Partb1\db_EMS.mdf;Integ
rated Security=True;User Instance=True");
public Form5()
{
InitializeComponent();
reteievalId();
}
private void button2_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.Show();
this.Hide();
}

private void button1_Click(object sender, EventArgs e)


{
string query = @"INSERT INTO
tbl_EmployeeDetails(IdEmployee,EmployeeName,
ContactNumber, IdDesignation, IdReportingTo) values('" +
textBox1.Text + "','" +
textBox2.Text + "','" + textBox3.Text + "'," + textBox4.Text +
", " + textBox5.Text + ")";
SqlDataAdapter da = new SqlDataAdapter(query, scon);
DataTable dt = new DataTable();
da.Fill(dt);

MessageBox.Show("Employees Added Successfully...!");


}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

2. Display all the Project Leaders (In a Grid) reporting to selected Project Managers
(In a Combo box).

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Partb1
{
public partial class Form2 : Form
{
SqlConnection scon = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=E:\Partb1\Partb1\db_EMS.mdf;Integ
rated Security=True;User Instance=True");
SqlDataAdapter da;
DataTable dt;

public Form2()
{
InitializeComponent();
scon.Open();
FillcomboBox1();
}
public void FillcomboBox1()
{
da = new SqlDataAdapter("Select
ed.IdEmployee,ed.EmployeeName from tbl_EmployeeDetails ed where
ed.IdDesignation=1", scon);
dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count > 0)
{
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "EmployeeName";
comboBox1.ValueMember = "IdEmployee";
}
}
private void button1_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("SELECT EmployeeName,ContactNumber
FROM tbl_EmployeeDetails WHERE IdDesignation=2 and
IdReportingTo="+comboBox1.SelectedValue, scon);

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count >=1)
dataGridView1.DataSource = dt;
else
{
dataGridView1.DataSource = null;
MessageBox.Show("Not availabe for s...!");
}
}
private void button2_Click(object sender, EventArgs e)
{
Form1 f3 = new Form1();
f3.Show();
this.Hide();
}
}
}

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

3. Display all the Engineers (In a Grid) reporting to selected Project Leader (In a
Combo box).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Partb1
{
public partial class Form3 : Form
{
SqlConnection scon = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=E:\Partb1\Partb1\db_EMS.mdf;Integ
rated Security=True;User Instance=True");
SqlDataAdapter da;
DataTable dt;
public Form3()
{
InitializeComponent();
FillcomboBox1();
}
public void FillcomboBox1()
{
da = new SqlDataAdapter("Select
ed.IdEmployee,ed.EmployeeName from tbl_EmployeeDetails ed where
ed.IdDesignation=2", scon);
dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "EmployeeName";
comboBox1.ValueMember = "IdEmployee";
}
}
private void button1_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("SELECT EmployeeName,ContactNumber
FROM tbl_EmployeeDetails WHERE IdDesignation=3 and IdReportingTo=" +
comboBox1.SelectedValue, scon);

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count >= 1)
dataGridView1.DataSource = dt;
else
{
dataGridView1.DataSource = null;
MessageBox.Show("Not availabe for s...!");
}
}
private void Form3_MouseDoubleClick(object sender,
MouseEventArgs e)
{
Form4 f4 = new Form4();
f4.Show();
this.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
Form1 f4 = new Form1();
f4.Show();
this.Hide();
}
}
}

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

4. Display all the Employees (In a Grid) with their reporting Manager (No Value for
PM).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Partb1
{
public partial class Form4 : Form
{
SqlConnection scon = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=E:\Partb1\Partb1\db_EMS.mdf;Integ
rated Security=True;User Instance=True");
SqlDataAdapter da;
DataTable dt;
public Form4()
{
InitializeComponent();
scon.Open();
}

private void Form4_Load(object sender, EventArgs e)


{

da = new SqlDataAdapter("SELECT e.EmployeeName as


EMPLOYEE,e1.EmployeeName as ReportingManager from tbl_EmployeeDetails
e,tbl_EmployeeDetails e1 where e.IdReportingTo=e1.IdEmployee", scon);
dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
dataGridView1.DataSource = dt;
else
{
dataGridView1.DataSource = null;
MessageBox.Show("No employees availabe!");
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

}
private void button1_Click_1(object sender, EventArgs e)
{
Form1 f4 = new Form1();
f4.Show();
this.Hide();
}
}
}

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

II. 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.

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PartBII
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
Form2 f2 = new Form2();
f2.Show();
this.Hide();
}

private void button2_Click(object sender, EventArgs e)


{
Form3 f3 = new Form3();
f3.Show();
this.Hide();
}
private void button4_Click(object sender, EventArgs e)
{
Form4 f4 = new Form4();
f4.Show();
this.Hide();
}

private void button3_Click(object sender, EventArgs e)


{
Form5 f5 = new Form5();
f5.Show();
this.Hide();
}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

1. Enter new Subject Details.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace PartBII
{
public partial class Form2 : Form
{
//intialize connection
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=c:\users\temp\documents\visual
studio 2010\Projects\PartBII\PartBII\Database1.mdf;Integrated
Security=True;User Instance=True");

public Form2()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)


{
Form1 f = new Form1();
f.Show();
this.Hide();

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)
}

private void button1_Click(object sender, EventArgs e)


{
string query = @"INSERT INTO tbl_subjects values(" +
idSubjectTextBox.Text + ",'" + subjectCodeTextBox.Text + "','" +
subjectNameTextBox.Text + "')";

SqlDataAdapter da = new SqlDataAdapter(query, con);


DataTable dt = new DataTable();
da.Fill(dt);
MessageBox.Show(" Subject Added Successfully...!");
con.Close();

}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

2. Enter New Lecturer Details.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)
namespace PartBII
{
public partial class Form3 : Form
{

//intialize connection
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=c:\users\temp\documents\visual
studio 2010\Projects\PartBII\PartBII\Database1.mdf;Integrated
Security=True;User Instance=True");

public Form3()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
string query = @"INSERT INTO tbl_Lecturers values(" +
idLecturerTextBox.Text + ",'" + lecturerNameTextBox.Text + "','" +
contactNumberTextBox.Text + "')";

SqlDataAdapter da = new SqlDataAdapter(query, con);


DataTable dt = new DataTable();
da.Fill(dt);
MessageBox.Show(" Lecture Added Successfully...!");
con.Close();

private void button2_Click(object sender, EventArgs e)


{
Form1 f = new Form1();
f.Show();
this.Hide();
}
}
}
OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

3. Subject Allocation with Lecturer Name in a Combo box and subjects to be


allocated in Grid with checkbox Column.

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace PartBII
{
public partial class Form4 : Form
{

//intialize connection
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=c:\users\temp\documents\visual
studio 2010\Projects\PartBII\PartBII\Database1.mdf;Integrated
Security=True;User Instance=True");

public Form4()
{
InitializeComponent();
fill_comboBox1();
}

private void button1_Click(object sender, EventArgs e)


{
Form1 f = new Form1();
f.Show();
this.Hide();
}
private void fill_comboBox1()
{
SqlDataAdapter da = new SqlDataAdapter("SELECT
IdLecturer,LecturerName from tbl_Lecturers", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "LecturerName";
comboBox1.ValueMember = "IdLecturer";
}
}

private void button2_Click(object sender, EventArgs e)

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)
{
string col2
=dataGridView1[0,dataGridView1.CurrentCell.RowIndex].Value.ToString();
string col3
=dataGridView1[1,dataGridView1.CurrentCell.RowIndex].Value.ToString();

string query = @"INSERT INTO


tbl_LecturerSubjects(IdSubject,SubjectCode,IdLecturer) values(" + col2 + ",'" +
col3 + "'," + comboBox1.SelectedValue + ")";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
da.Fill(dt);
MessageBox.Show("Subject Allocated Successfully...!");
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs


e)
{
SqlDataAdapter da = new SqlDataAdapter(@"SELECT * FROM
tbl_subjects WHERE SubjectCode not in(select SubjectCode from
tbl_LecturerSubjects)", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
dataGridView1.DataSource = dt;
else
{
dataGridView1.DataSource = null;
MessageBox.Show("All Subjects are allocated!");
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

4. Display all the subjects allocated (In a Grid) to the selected Lecturer (In a Combo
Box).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)
using System.Windows.Forms;
using System.Data.SqlClient;
namespace PartBII
{
public partial class Form5 : Form
{
//intialize connection
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=c:\users\temp\documents\visual
studio 2010\Projects\PartBII\PartBII\Database1.mdf;Integrated
Security=True;User Instance=True");

public Form5()
{
InitializeComponent();
fill_comboBox1();
}

private void fill_comboBox1()


{
SqlDataAdapter da = new SqlDataAdapter("SELECT
IdLecturer,LecturerName from tbl_Lecturers", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "LecturerName";
comboBox1.ValueMember = "IdLecturer";
}
}

private void button2_Click(object sender, EventArgs e)


{

Form1 f = new Form1();


f.Show();
this.Hide();
}

private void button1_Click(object sender, EventArgs e)


{
SqlDataAdapter da = new SqlDataAdapter("SELECT
s.SubjectCode,s.SubjectName FROM tbl_Subjects s,tbl_LecturerSubjects ls
WHERE ls.SubjectCode=s.SubjectCode and ls.IdLecturer=" +
comboBox1.SelectedValue, con);
Dept. of MCA, BIT
.NET LABORATORY (18MCA56)
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
dataGridView1.DataSource = dt;
else
{
dataGridView1.DataSource = null;

MessageBox.Show("No subjects alloted to the subject...!");

}
}
}
}
OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

III. 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)

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


1 TWO Wheeler 500
2 FOUR Wheeler 1000
3 THREE Wheeler 700

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


options.

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

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

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

private void button1_Click(object sender, EventArgs e)


{
Form2 f2 = new Form2();
f2.Show();
this.Hide();
}

private void button2_Click(object sender, EventArgs e)


{
Form3 f3 = new Form3();
f3.Show();
this.Hide();

private void button3_Click(object sender, EventArgs e)


{
Form4 f4 = new Form4();
f4.Show();
this.Hide();

}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

1. Enter new Service Details for the Selected Vehicle Type (In a Combo
Box).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace PartBIII
{
public partial class Form2 : Form
{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=c:\users\temp\documents\visual
studio 2010\Projects\PartBIII\PartBIII\Database1.mdf;Integrated
Security=True;User Instance=True");
public Form2()
{
InitializeComponent();
con.Open();
FillCombox1();
//comboBox1.SelectedIndexChanged
+=comboBox1_SelectedIndexChanged;

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)
private void button2_Click(object sender, EventArgs e)
{
Form1 f1=new Form1();
f1.Show();
this.Hide();
}

private void button1_Click(object sender, EventArgs e)


{
string query = @"insert into
tbl_ServiceDetails(IdService,VehicleNumber,ServiceDetails,IdVehicleType)value
s(" + idServiceTextBox.Text + ",'" + vehicleNumberTextBox.Text + "','" +
serviceDetailsTextBox.Text + "'," + comboBox1.SelectedValue +")";

SqlDataAdapter da = new SqlDataAdapter(query, con);


DataTable dt = new DataTable();
da.Fill(dt);
MessageBox.Show("Service details added into database successfully!");

public void FillCombox1()


{
SqlDataAdapter da = new SqlDataAdapter("SELECT
IdVehicleType,VehicleType from tbl_VehicleTypes", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "VehicleType";
comboBox1.ValueMember = "IdVehicleType";
}
}

}
}

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

2. Update the Existing Service Charges to Database.

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace PartBIII
{
public partial class Form3 : Form
{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=c:\users\temp\documents\visual
studio 2010\Projects\PartBIII\PartBIII\Database1.mdf;Integrated
Security=True;User Instance=True");

public Form3()
{
InitializeComponent();
con.Open();
FillComboBox1();
}

public void FillComboBox1()


{
SqlDataAdapter da = new SqlDataAdapter("SELECT
IdVehicleType,VehicleType from tbl_VehicleTypes", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "VehicleType";
comboBox1.ValueMember = "IdVehicleType";
}
}

private void button2_Click(object sender, EventArgs e)


{
Form1 f1 = new Form1();
f1.Show();
this.Hide();
}

private void button1_Click(object sender, EventArgs e)

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)
{
var item =this.comboBox1.GetItemText(this.comboBox1.SelectedItem);
string query = @"update tbl_VehicleTypes set ServiceCharge='" +
ServiceCharge.Text + "' where VehicleType='" + item + "'";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
da.Fill(dt);
MessageBox.Show("Service details updated into database successfully!");

}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

3. Total Service Charges Collected for the Selected Vehicle (In a Combo
box) with total amount displayed in a text box.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
Dept. of MCA, BIT
.NET LABORATORY (18MCA56)
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace PartBIII
{
public partial class Form4 : Form
{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=c:\users\temp\documents\visual
studio 2010\Projects\PartBIII\PartBIII\Database1.mdf;Integrated
Security=True;User Instance=True");

public Form4()
{
InitializeComponent();
con.Open();
FillComboBox1();
}

public void FillComboBox1()


{
SqlDataAdapter da = new SqlDataAdapter("SELECT
IdVehicleType,VehicleType from tbl_VehicleTypes", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "VehicleType";
comboBox1.ValueMember = "IdVehicleType";
}
}

private void button1_Click(object sender, EventArgs e)


{
var item =this.comboBox1.GetItemText(this.comboBox1.SelectedItem);
SqlDataAdapter da = new SqlDataAdapter("select sum(v.ServiceCharge)
from tbl_VehicleTypes v join tbl_ServiceDetails s on
v.IdVehicleType=s.IdVehicleType where v.VehicleType='"+item+ "' group by
s.IdVehicleType", con);
DataTable dt = new DataTable();
da.Fill(dt);
try
{
ServiceCharge.Text = dt.Rows[0][0].ToString();

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)
}
catch
{
ServiceCharge.Text = 0.ToString();
}

private void button2_Click_1(object sender, EventArgs e)


{

Form1 f1 = new Form1();


f1.Show();
this.Hide();

}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

IV. Develop a web application using C#.NET and ASP.NET for the Postal
System Management. The master page should contain the hyperlinks 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:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

1. Enter New Area Details.

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

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


{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mithin
K\Documents\db_PSM.mdf;Integrated Security=True;Connect
Timeout=30;User Instance=True");
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlDataAdapter da = new SqlDataAdapter("INSERT INTO
AreaDetails(IdArea,AreaName) VALUES ('" + TextBox1.Text + "', '" +
TextBox2.Text + "')", con);
DataTable dt = new DataTable();
da.Fill(dt);
Label3.Text = "Area Details Saved Successfully..!";
}
catch (Exception ex)
{
Label3.Text = ex.Message.ToString();
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

}
OUTPUT:

2. Enter New Postman Details with the Area he/she is in-charge of (display
Area in a Combo box).

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

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


{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mithin
K\Documents\db_PSM.mdf;Integrated Security=True;Connect
Timeout=30;User Instance=True");
protected void Button1_Click(object sender, EventArgs e)
{

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

try
{
SqlDataAdapter da = new SqlDataAdapter("INSERT INTO
PostmanDetails(IdPostman,PostmanName,ContactNumber,IdArea) VALUES ('"
+ TextBox1.Text + "', '" + TextBox2.Text + "','" + TextBox2.Text +
"'," + DropDownList1.SelectedValue + ")", con);
DataTable dt = new DataTable();
da.Fill(dt);
Label3.Text = "postmandetails Saved Successfully..!";
}
catch (Exception ex)
{
Label3.Text = ex.Message.ToString();
}
}
}

OUTPUT:

3. Enter all the Letters distributed to the selected Area (display Area in a
Combo box).

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

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

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


{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mithin
K\Documents\db_PSM.mdf;Integrated Security=True;Connect
Timeout=30;User Instance=True");
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlDataAdapter da = new SqlDataAdapter("INSERT INTO
AreaLetters(Idletter,LetterAddress,IdArea) VALUES ('" + TextBox1.Text
+ "', '" + TextBox2.Text + "'," + DropDownList1.SelectedValue + ")",
con);
DataTable dt = new DataTable();
da.Fill(dt);
Label3.Text = "Arealetters Saved Successfully..!";
}
catch (Exception ex)
{
Label3.Text = ex.Message.ToString();
}
}
}

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

4. Display all the Letter addresses (In a Grid) to be distributed by the


selected Postman (In a Combo box)

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

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


{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mithin
K\Documents\db_PSM.mdf;Integrated Security=True;Connect
Timeout=30;User Instance=True");
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlDataAdapter da = new SqlDataAdapter("SELECT
a.letteraddress from AreaLetters a,postmandetails p where
a.IdArea=p.idarea and p.Idpostman=" + DropDownList2.SelectedValue,
con);
DataTable dt = new DataTable();
{
GridView1.DataSource = null;
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
Label3.Text = " ";
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

else
{
GridView1.DataBind();
Label3.Text = "No Records Found..!!!!";
}
}
}
catch (Exception ex)
{
Label3.Text = ex.Message.ToString();
}
}
}

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

V. 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, 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).

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

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

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).

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

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


{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mithin
K\Documents\db_CMS.mdf;Integrated Security=True;Connect
Timeout=30;User Instance=True");
protected void Button1_Click(object sender, EventArgs e)
{

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

try
{
string query = "INSERT INTO
Engineers(IdEngineer,EngineerName,ContactNumber,idDepartment)
VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" +
TextBox3.Text + "'," + DropDownList1.SelectedValue + ")";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
sda.Fill(dt);
Label3.Text = "Engineers details Inserted
Successfully...!";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}
catch (Exception ex)
{
Label3.Text = ex.Message.ToString();
}
}
}

OUTPUT:

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

2. Register a new Complaint with a submit button.

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

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


{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mithin
K\Documents\db_CMS.mdf;Integrated Security=True;Connect
Timeout=30;User Instance=True");
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string query = "INSERT INTO
RegisteredComplaints(IdComplaint,ComplaintDescription) VALUES('" +
TextBox1.Text + "','" + TextBox2.Text + "')";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
sda.Fill(dt);
Label3.Text = "Complaint details Inserted
Successfully...!";
TextBox1.Text = "";
TextBox2.Text = "";
}
catch (Exception ex)
{
Label3.Text = ex.Message.ToString();
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

}
}

OUTPUT:

3. View all registered complaints & allocate to the corresponding


department (displayed in a combo box).

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

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


{

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

SqlConnection con = new SqlConnection(@"Data


Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mithin
K\Documents\db_CMS.mdf;Integrated Security=True;Connect
Timeout=30;User Instance=True");
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlDataAdapter da = new SqlDataAdapter("INSERT INTO
DepartmentComplaints(IdDepartment,IdComplaint)VALUES(" +
DropDownList1.SelectedValue + "," + DropDownList2.SelectedValue + ")",
con);
DataTable dt = new DataTable();
da.Fill(dt);
Label3.Text = "Complaints are allocated
Successfully...!";
}
catch (Exception ex)
{
Label3.Text = ex.Message.ToString();
}
}
}

OUTPUT:

4. Display all the Complaints (In a Grid) to be handled by the selected


Engineer (In a Combo box).

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

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

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


{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mithin
K\Documents\db_CMS.mdf;Integrated Security=True;Connect
Timeout=30;User Instance=True");
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlDataAdapter da = new SqlDataAdapter("select
rc.ComplaintDescription from RegisteredComplaints
rc,DepartmentComplaints dc,Engineers e where
rc.IdComplaint=dc.IdComplaint and dc.IdDepartment=e.IdDepartment and
e.IdEngineer= " + DropDownList2.SelectedValue, con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
Label1.Text = " ";
}
else
{
GridView1.DataSource = null;
GridView1.DataBind();
Label1.Text = "No Records Found..!!!!";
}
}
catch (Exception ex)
{
Label1.Text = ex.Message.ToString();
}
}
}

Dept. of MCA, BIT


.NET LABORATORY (18MCA56)

OUTPUT:

Dept. of MCA, BIT

You might also like