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

19IT470 3IT42 PracticalFile

The document provides code examples for 7 C# concepts: constructors and copy constructors, destructors, method overloading, properties, inheritance, exception handling, and delegates. For each concept, it gives a brief explanation and a code sample to demonstrate how to implement that concept in C#.

Uploaded by

Amol Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

19IT470 3IT42 PracticalFile

The document provides code examples for 7 C# concepts: constructors and copy constructors, destructors, method overloading, properties, inheritance, exception handling, and delegates. For each concept, it gives a brief explanation and a code sample to demonstrate how to implement that concept in C#.

Uploaded by

Amol Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 55

3IT42: Dot Net Technology

Practical 1. Write the following programs in C#.NET :-


1. Write a program to print “Hello World”

using System;

namespace LAB
{
class manual_1_1
{
static void Main(string[] args)
{
Console.WriteLine("hello World!");
Console.ReadKey();
}
}
}

2. Write a program to print a pattern

using System;

namespace LAB
{
class manual_1_2
{
public static void Main(String[] args)
{
int n,m;
Console.Write("Enter the layers of the pyramid:");
n=Convert.ToInt32(Console.ReadLine());
m=n;
for (int i = 1; i<= n; i++)
{
for (int j = 1; j <= m - 1; j++)
{
Console.Write(" ");
}
i
for (int k = 1; k <= 2 * i - 1; k++)
3IT42: Dot Net Technology

{
Console.Write("*");
}
m--;

Console.WriteLine("");
}
}
}

3. Write a program to reverse a number

using System;

namespace LAB
{
class manual_1_3
{
static void Main(string[] args)
{
Console.WriteLine("Enter a No. to
reverse"); int Number =
int.Parse(Console.ReadLine()); int Reverse =
0;
while (Number > 0)
{
int remainder = Number % 10;
Reverse = (Reverse * 10) +
remainder; Number = Number / 10;
}
Console.WriteLine("Reverse No. is {0}", Reverse);
Console.ReadLine();
}
}
}
3IT42: Dot Net Technology

4. Write a program to find the greatest value of the three values

using System;

namespace LAB
{
class manual_1_4
{
static void Main(string[] args)
{
int num1, num2, num3;

num1 = 10;
num2 = 20;
num3 = 50;
if (num1 > num2)
{
if (num1 > num3)
{
Console.Write("Number one is the largest!\n");
}
else
{
Console.Write("Number three is the largest!\n");
}
}
else if (num2 > num3)
Console.Write("Number two is the largest!\n");
else
Console.Write("Number three is the largest!\n");
}
}
}
3IT42: Dot Net Technology

5. Write a program to sort an integer array of 10 elements in ascending

using System;
namespace practical1
{
class sortarray
{
public static void Main()
{
int[] arr = new int[] { 10,9,8,7,6,5,4,3,2,1 };
int temp = 0;

Console.WriteLine("Elements of original array: ");


for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
for (int i = 0; i < arr.Length; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] =
arr[j]; arr[j]
= temp;
}
}
}
Console.WriteLine();
Console.WriteLine("Elements of array sorted in ascending order: ");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
}
}
}
3IT42: Dot Net Technology

Practical 2.(a) Write C# code to prompt a user to input his/her name and country name
and then the output will be shown as an example below:

Hello Ram from country India!

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

namespace LAB
{
class manual_2_1
{
static void Main(string[] args)
{
Console.WriteLine("Enter your name:");
string name = Console.ReadLine();

Console.WriteLine("Enter your country name:");


string country = Console.ReadLine();

Console.WriteLine("Hello" +name+ "from country " +country+"!");

Console.ReadKey();
}
}
}
3IT42: Dot Net Technology

Practical 2.(b) Write C# code to perform various string manipulation command on string.

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

namespace LAB
{
class maunal_2_2
{
static void Main(string[] args)
{
int x=0,y=0;
Console.WriteLine("Enter the value of x");
x=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the value of y");
y=Convert.ToInt32(Console.ReadLine());
int add=0,sub=0,mult=0,div=0;
add=x+y;
sub=x-y;
mult=x*y;
div=x/y;
Console.WriteLine("Addition of x and y is {0}",add);
Console.WriteLine("Subtraction of x and y is {0}",sub);
Console.WriteLine("Multiplication of x and y is {0}",mult);
Console.WriteLine("Division of x and y is {0}",div);
Console.ReadLine();
}
}
}
3IT42: Dot Net Technology

Practical 3. Create console applications to implement following C# concepts.


1. Constructor & Copy Constructor.

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

namespace LAB
{
class Box
{
public int x,y,z;
public Box(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public Box(Box copybox)
{
this.x =
copybox.x; this.y
= copybox.y; this.z
= copybox.z;
}
}
class maunal_3_1
{
static void Main(string[] args)
{
Box cube = new Box(10, 10, 10);
Box copycube = new Box(cube);
Console.WriteLine("Dimenseions of the cube are {0} {1} {2}", cube.x, cube.y, cube.z);
Console.WriteLine("Dimenseions of the copycube are {0} {1} {2}", copycube.x, copycube.y,
copycube.z);
}
}
}
3IT42: Dot Net Technology

2. Destructor

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

namespace LAB
{
class Box
{
public int x,y,z;
public Box(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public Box(Box copybox)
{
this.x =
copybox.x; this.y
= copybox.y; this.z
= copybox.z;
}
~Box()
{
Console.WriteLine("Box destructor called");
}
}
class maunal_3_2
{
static void Main(string[] args)
{
Box cube = new Box(10, 10, 10);
Console.WriteLine("Dimenseions of the cube are {0} {1} {2}", cube.x, cube.y, cube.z);
}
}
}
3IT42: Dot Net Technology

3. Method Overloading

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

namespace LAB
{
class Add
{
public int Addition(int a, int b)
{
return a + b;
}
public int Addition(int a, int b, int c)
{
return a + b + c;
}
}
class maunal_3_3
{
static void Main(string[] args)
{
Add obj = new Add();
Console.WriteLine(obj.Addition(10, 20));
Console.WriteLine(obj.Addition(10, 20, 30));
}
}
}
3IT42: Dot Net Technology

4. Properties

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

namespace LAB
{
class Name
{
private string
firstName; private
string lastName; public
string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
public string FullName
{
get
{
return firstName + " " + lastName;
}
}
}
class maunal_3_4
{
static void Main(string[] args)
3IT42: Dot Net Technology

{
Name name = new Name();
name.FirstName = ("Sanket");
name.LastName = "Detroja";
Console.WriteLine(name.FullName);
}
}
}

5. Inheritance

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

namespace LAB
{
class Parent
{
public void Method()
{
Console.WriteLine("Parent.Method");
}
}
class Child : Parent
{
public new void Method()
{
Console.WriteLine("Child.Method");
}
}
class maunal_3_5
{
static void Main(string[] args)
{
Parent p = new Parent();
p.Method();
3IT42: Dot Net Technology

Child c = new Child();


c.Method();
}
}
}

6. Exception Handling

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

namespace LAB
{
class Division
{
public int result = 0;
public void division (int a, int b)
{
try
{
result = a / b;
}
catch (DivideByZeroException e)
{
Console.WriteLine("Divide by zero exception: " + (e.Message));
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.Message);
}
finally
{
Console.WriteLine("Result: {0}", result);
}
}
}
class maunal_3_6
3IT42: Dot Net Technology

{
static void Main(string[] args)
{
Division d = new Division();
d.division(10, 0);
}
}
}

7. Delegates

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

namespace LAB
{
class Calc
{
public delegate void addnum(int x, int y);
public delegate void subnum(int x, int y);
public delegate void mulnum(int x, int
y); public delegate void divnum(int x, int
y); public void add(int a , int b)
{
Console.WriteLine("Sum is : " + (a + b));
}
public void sub(int a, int b)
{
Console.WriteLine("Sub is : " + (a - b));
}
public void mul(int a, int b)
{
Console.WriteLine("Mul is : " + (a * b));
}
public void div(int a, int b)
{
3IT42: Dot Net Technology

Console.WriteLine("Divis : " + (a / b));


}
}
class maunal_3_7
{
static void Main(string[] args)
{
Calc c = new Calc();
Calc.addnum add = new
Calc.addnum(c.add); add(10, 20);
Calc.subnum sub = new
Calc.subnum(c.sub); sub(10, 20);
Calc.mulnummul = new Calc.mulnum(c.mul);
mul(10, 20);
Calc.divnum div = new Calc.divnum(c.div);
div(10, 20);
}
}
}

8. Indexer

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

namespace LAB
{
class IndexNaming
{
private string[] names = new string[10];
public string this[int i]
{
get
{
return names[i];
3IT42: Dot Net Technology

}
set
{
names[i] = value;
}
}
}
class maunal_3_8
{
static void Main(string[] args)
{
IndexNaming Team = new
IndexNaming(); Team[0] = "Sanket";
Team[1] = "Kunj";
Team[2] = "Sharthak";
Team[3] = "Brijesh";
Team[4] = "Kenil";
Team[5] = "Dharmil";
Team[6] = "Manoj";
Team[7] = "Bhagvanji";
Team[8] = "Vishal";
Team[9] = "Palak";
for (int i = 0; i< 10; i++)
{
Console.WriteLine(Team[i]);
}
}
}
}
3IT42: Dot Net Technology

Practical 4. Write the following programs in C#.NET :-


1. Create a window application for basic window form controls that will show the basic
property and methods of all that controls.

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;

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

private void button1_Click(object sender, EventArgs e)


{
button1.Text = "HEY YOU BUDDY, MY FRIEND SANKET! HOW ARE YOU? HOPE
YOUR ARE FINE :) ";
}

private void Form1_Load(object sender, EventArgs e)


{

}
}
}
3IT42: Dot Net Technology

2. Create a calculator using button, label, textbox control in .NET

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;

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

private void Form1_Load(object sender, EventArgs e)


3IT42: Dot Net Technology

private void button1_Click(object sender, EventArgs e)


{
double txt1 =
Convert.ToDouble(textBox1.Text); double txt2
= Convert.ToDouble(textBox2.Text); double
sum = txt1 + txt2;
textBox3.Text = sum.ToString();
}

private void button2_Click(object sender, EventArgs e)


{
double txt1 =
Convert.ToDouble(textBox1.Text); double txt2
= Convert.ToDouble(textBox2.Text); double
sum = txt1 - txt2;
textBox3.Text = sum.ToString();
}

private void button3_Click(object sender, EventArgs e)


{
double txt1 =
Convert.ToDouble(textBox1.Text); double txt2
= Convert.ToDouble(textBox2.Text); double
sum = txt1 * txt2;
textBox3.Text = sum.ToString();
}

private void button4_Click(object sender, EventArgs e)


{
double txt1 =
Convert.ToDouble(textBox1.Text); double txt2
= Convert.ToDouble(textBox2.Text); double
sum = txt1 / txt2;
textBox3.Text = sum.ToString();
}
}
}
3IT42: Dot Net Technology
3IT42: Dot Net Technology

Practical 5. Perform below window based program using C#


1. Write a program to demonstrate use of radio button, checkbox, list box, combo box
and list view
- Open new window form in visual studio.
- Then open Toolbox shown below.

- Now drag and drop the necessary tools from Toolbox shown below.
3IT42: Dot Net Technology

2. Write a program to demonstrate use of inheritance of a form in another

form ParentForm:

using System;
using System.Windows.Forms;

namespace inheritance
{
public partial class parentForm : Form
{
public parentForm()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
pictureBox1.Left = pictureBox1.Left + 100;
}

private void button2_Click(object sender, EventArgs e)


{
pictureBox1.Left = pictureBox1.Left - 100;
}

private void parentForm_Load(object sender, EventArgs e)


{

}
}
}

childForm:

using System;

namespace inheritance
{
public partial class childForm : parentForm
{
public childForm()
{
InitializeComponent();
}
3IT42: Dot Net Technology

private void childForm_Load(object sender, EventArgs e)


{

}
}
}

3. Write a program to demonstrate use of MDI form

Form 1:
using System;
using System.Windows.Forms;

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

Form2 f2;
3IT42: Dot Net Technology

private void from1ToolStripMenuItem_Click(object sender, EventArgs e)


{
if (f2 == null)
{
f2 = new Form2();
f2.MdiParent =
this; f2.Show();
}
else
{
f2.Activate();
}
}

Form3 f3;
private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f3 == null)
{
f3 = new Form3();
f3.MdiParent =
this; f3.Show();
}
else
{
f3.Activate();
}
}
}
}

Form 2:
using System;

using System.Windows.Forms;

namespace MIDform
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)


3IT42: Dot Net Technology

}
}
}

Form3:
using System;
using System.Windows.Forms;

namespace MIDform
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)


{

}
}
}
3IT42: Dot Net Technology

4. Write a program to demonstrate use of print dialog (print document, print preview control
and print setup)

using System;
using System.Drawing;
using System.Windows.Forms;

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

private void button1_Click(object sender, EventArgs e)


{
printDialog1.Document = printDocument1;
if(printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}

private void printDocument1_PrintPage(object sender,


System.Drawing.Printing.PrintPageEventArgs e)
{
Pen selPen = new Pen(Color.Black);
e.Graphics.DrawRectangle(selPen, 125, 125, 550, 250);
e.Graphics.DrawString("ID : " + textBox1.Text, new Font("Arial", 25), Brushes.Black,
150,150);
e.Graphics.DrawString("Name : " + textBox2.Text, new Font("Arial", 25),
Brushes.Black, 150, 200);
e.Graphics.DrawString("Branch : " + textBox3.Text, new Font("Arial", 25),
Brushes.Black, 150, 250);
e.Graphics.DrawString("Year : " + textBox4.Text, new Font("Arial", 25), Brushes.Black,
150, 300);
}
}
}
3IT42: Dot Net Technology

5. Create Menu Strip in Window form Application.

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;
3IT42: Dot Net Technology

namespace menuitemfinal
{
public partial class Form1 : Form
{
public string var = ""; public Form1()
{
InitializeComponent();
}

private void copyToolStripMenuItem_Click(object sender, EventArgs e)


{
if (richTextBox1.SelectionLength > 0)
{
var = richTextBox1.SelectedText;
}
}

private void cutToolStripMenuItem_Click(object sender, EventArgs e)


{
if (richTextBox1.SelectionLength > 0)
{

var = richTextBox1.SelectedText; richTextBox1.SelectedText = "";


}
}

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)


{
if (var.Length > 0)
{
richTextBox2.Text = richTextBox2.Text + var;
}
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)


{
MessageBox.Show("Goodbye"); this.Close();
}
}
}
3IT42: Dot Net Technology
3IT42: Dot Net Technology

Practical 6. Create a window application for basic Dialog controls that will show the basic
property and methods of all that controls.

1. FolderBrowserDialog

2. OpenFileDialog

3. ColorDialog

4. FontDilalog

5. SaveFileDialog Control

using System;
using System.Windows.Forms;

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

private void button1_Click(object sender, EventArgs e)


{
FolderBrowserDialog a = new FolderBrowserDialog();
if (a.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show("successfully selected the Folder");
}
}

private void button2_Click(object sender, EventArgs e)


{
OpenFileDialog b = new OpenFileDialog();
if (b.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show("open the file successfully : " + b.SafeFileName);
}
}

private void button3_Click(object sender, EventArgs e)


{
3IT42: Dot Net Technology

ColorDialog c = new ColorDialog();


if (c.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show("open color box successfully ");
}
}

private void button4_Click(object sender, EventArgs e)


{
FontDialog d = new FontDialog();
if (d.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show("open Font box successfully ");

}
}

private void button5_Click(object sender, EventArgs e)


{
SaveFileDialog f = new SaveFileDialog();
if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show("Save the file successfully");

}
}
}
}
3IT42: Dot Net Technology
3IT42: Dot Net Technology
3IT42: Dot Net Technology
3IT42: Dot Net Technology

Practical 7. Create a window application for connection with sql server and perform basic
operations on database. (Insert , Update ,Delete) in ADO.NET

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

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

OleDbConnection conn;
OleDbCommand comm;
OleDbDataReader dreader;
string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\smdetroja\\Desktop\\Database.mdb";

private void button1_Click(object sender, EventArgs e)


{
conn = new OleDbConnection(connstring);
conn.Open();
comm = new OleDbCommand("insert into Data_Of_Student values(" + textBox1.Text +
",'" + textBox2.Text + "'," + textBox3.Text + ",'" + textBox4.Text + "')", conn);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("Saved...");
}
catch (Exception)
{
MessageBox.Show("Not Saved");
}
finally
3IT42: Dot Net Technology

{
conn.Close();
}
}

private void button2_Click(object sender, EventArgs e)


{
textBox4.Clear();
textBox3.Clear();
textBox2.Clear();
textBox1.Clear();
textBox1.Focus();
}

private void button3_Click(object sender, EventArgs e)


{
conn = new OleDbConnection(connstring);
conn.Open();
comm = new OleDbCommand("delete from Data_Of_Student where roll_no = "
+ textBox1.Text + " ", conn);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("Deleted...");
textBox4.Clear();
textBox3.Clear();
textBox2.Clear();
textBox1.Clear();
textBox1.Focus();
}
catch (Exception x)
{
MessageBox.Show(" Not Deleted" + x.Message);
}
finally
{
conn.Close();
}
}

private void button4_Click(object sender, EventArgs e)


{
conn = new OleDbConnection(connstring);
conn.Open();
comm = new OleDbCommand("select * from Data_Of_Student where roll_no = "
+ textBox1.Text + " ", conn);
3IT42: Dot Net Technology

try
{
dreader = comm.ExecuteReader();
if (dreader.Read())
{
textBox2.Text =
dreader[1].ToString(); textBox3.Text
= dreader[2].ToString();
textBox4.Text =
dreader[3].ToString();
}
else
{
MessageBox.Show(" No Record");
}
dreader.Close();
}
catch (Exception)
{
MessageBox.Show(" No Record");
}
finally
{
conn.Close();
}
}

private void button5_Click(object sender, EventArgs e)


{
conn = new OleDbConnection(connstring);
conn.Open();
comm = new OleDbCommand("insert into Data_Of_Student values(" + textBox1.Text +
",'" + textBox2.Text + "'," + textBox3.Text + ",'" + textBox4.Text + "')", conn);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("Updated..");
}
catch (Exception)
{
MessageBox.Show(" Not Updated");
}
finally
{
conn.Close();
}
}
3IT42: Dot Net Technology

private void Form1_Load(object sender, EventArgs e)


{
textBox1.Focus();
}
}
}
3IT42: Dot Net Technology

Practical 8. Write the programs in ASP.NET :-


1. Write a program demonstrating validation control

<%@ Page Language="C#" AutoEventWireup="true"


CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 1092px;
height: 307px;
}
.auto-style2 {
width: 360px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table class="auto-style1">
<tr>
<td class="auto-style2">Username </td>
<td>
<asp:TextBox ID="TextBox1" runat="server"
OnTextChanged="TextBox1_TextChanged" Width="359px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBox1" ErrorMessage="Username Required"
ForeColor="Red" ValidationGroup="a"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">Password</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"
OnTextChanged="TextBox2_TextChanged" Width="359px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
runat="server" ControlToValidate="TextBox2" ErrorMessage="Password Required"
ForeColor="Red" ValidationGroup="a"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">Re-Password</td>
3IT42: Dot Net Technology

<td>
<asp:TextBox ID="TextBox3" runat="server"
OnTextChanged="TextBox3_TextChanged" Width="359px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="TextBox2" ControlToValidate="TextBox3" ErrorMessage="Password Not
Match" ForeColor="Red" ValidationGroup="a"
ValueToCompare="a"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="auto-style2">Fullname</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"
OnTextChanged="TextBox4_TextChanged" Width="359px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3"
runat="server" ControlToValidate="TextBox4" ErrorMessage="Fullname Required"
ForeColor="Red" ValidationGroup="a"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">Email</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"
OnTextChanged="TextBox5_TextChanged" Width="359px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ControlToValidate="TextBox5" ErrorMessage="Enter Proper Email"
ForeColor="#FF3300" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="a"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="auto-style2">Age</td>
<td>
<asp:TextBox ID="TextBox6" runat="server"
OnTextChanged="TextBox6_TextChanged" Width="359px"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="TextBox6" ErrorMessage="Enter Correct Age"
ForeColor="#FF3300" MaximumValue="35" MinimumValue="18"
ValidationGroup="a"></asp:RangeValidator>
</td>
</tr>
<tr>
<td class="auto-style2">Country</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Choose Country</asp:ListItem>
3IT42: Dot Net Technology

<asp:ListItem>India</asp:ListItem>
<asp:ListItem>USA</asp:ListItem>
<asp:ListItem>Germany</asp:ListItem>
<asp:ListItem>Canada</asp:ListItem>
<asp:ListItem>Australia</asp:ListItem>
<asp:ListItem>Japan</asp:ListItem>
<asp:ListItem>Newzealand</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="DropDownList1" ErrorMessage="Enter Country "
ForeColor="Red" ValidationGroup="a"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">Gender</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
<asp:ListItem>Trans</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="RadioButtonList1" ErrorMessage="Enter Gender" ForeColor="Red"
ValidationGroup="a"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Save" ValidationGroup="a" />
</td>
</tr>
</table>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ForeColor="Black" ShowMessageBox="True" ValidationGroup="a" />
</form>
</body>
</html>
3IT42: Dot Net Technology
3IT42: Dot Net Technology

Practical 09. Create a web application that will use the concept of cookie, Response, request
and session object.

Cookie:

Using System;

Using System.Web;

Namespace WebFormsControlls

Public partial class CookieExample : System.Web.UI.Page

Protected void Page_Load(object sender, EventArgs e)

// Creating Cookie //

// Creating HttpCookie instance by specifying name

“student” HttpCookie cokie = new HttpCookie(“student”);

// Assigning value to the created

cookie Cokie.Value = “Rahul

Kumar”;

// Adding Cookie to the response instance

Response.Cookies.Add(cokie);

// Fetching Cookie //

Var co_val = Response.Cookies[“student”].Value;

Label1.Text = co_val;

}
3IT42: Dot Net Technology

Default.aspx:

<%@ Page Title=”Home Page” Language=”C#” AutoEventWireup=”true”


CodeBehind=”Default.aspx.cs”

Inherits=”CoockieExample._Default” %>

<form id=”form1” runat=”server”>

<asp:Label ID=”Label1” runat=”server” Text=”Select Brand Preferences”></asp:Label>

<br />

<br />

<asp:CheckBox ID=”apple” runat=”server” Text=”Apple” />

<br />

<asp:CheckBox ID=”dell” runat=”server” Text=”Dell” />

<br />

<asp:CheckBox ID=”lenevo” runat=”server” Text=”Lenevo” />

<br />

<asp:CheckBox ID=”acer” runat=”server” Text=”Acer” />

<br />

<asp:CheckBox ID=”sony” runat=”server” Text=”Sony” />

<br />

<asp:CheckBox ID=”wipro” runat=”server” Text=”Wipro” />

<br />

<br />

<asp:Button ID=”Button1” runat=”server” OnClick=”Button1_Click” Text=”Submit” />

<p>
3IT42: Dot Net Technology

<asp:Label ID=”Label2” runat=”server”></asp:Label>

</p>

</form>

Default.aspx.cs:

Using System;

Using System.Web.UI;

Namespace CoockieExample

Public partial class _Default : Page

Protected void Page_Load(object sender, EventArgs e)

// Setting expiring date and time of the cookies

Response.Cookies[“computer”].Expires = DateTime.Now.AddDays(-1);

Protected void Button1_Click(object sender, EventArgs e)

Label2.Text = “”;

// Adding Coockies //

If (apple.Checked)

Response.Cookies[“computer”][“apple”] = “apple”;

If (dell.Checked)

Response.Cookies[“computer”][“dell”] = “dell”;
3IT42: Dot Net Technology

If (lenevo.Checked) Response.Cookies[“computer”]

[“lenevo”] = “lenevo”;

If (acer.Checked) Response.Cookies[“computer”]

[“acer”] = “acer”;

If (sony.Checked) Response.Cookies[“computer”]

[“sony”] = “sony”;

If (wipro.Checked) Response.Cookies[“computer”]

[“wipro”] = “wipro”;

// Fetching Cookies //

If (Request.Cookies[“computer”].Values.ToString() != null)

If (Request.Cookies[“computer”][“apple”] != null)

Label2.Text += Request.Cookies[“computer”][“apple”] + “

“; If (Request.Cookies[“computer”][“dell”] != null)

Label2.Text += Request.Cookies[“computer”][“dell”] + “

“; If (Request.Cookies[“computer”][“lenevo”] != null)

Label2.Text += Request.Cookies[“computer”][“lenevo”] + “

“; If (Request.Cookies[“computer”][“acer”] != null)

Label2.Text += Request.Cookies[“computer”][“acer”] + “

“; If (Request.Cookies[“computer”][“sony”] != null)

Label2.Text += Request.Cookies[“computer”][“sony”] + “

“; If (Request.Cookies[“computer”][“wipro”] != null)

Label2.Text += Request.Cookies[“computer”][“wipro”] + “ “;

}else Label2.Text = “Please select your choice”;

Response.Cookies[“computer”].Expires = DateTime.Now.AddDays(-1);
3IT42: Dot Net Technology

}
3IT42: Dot Net Technology

}
3IT42: Dot Net Technology

Practical 10. Create Simple Web Service Application.

Step 1: Create simple websapplication

Go to Visual Studio then click on "File" -> "Website" -> "ASP.NET empty website template".
Then provide the website name (for example: WebServiceSample).

Step 2: Add a Web Service File

This will create the following two files:


1. WebService1.asmx (the service file)
2. WebService1.cs (the code file for the service; it will be in the "App_code" folder)

 Write following code in

WebService1.cs: using System;


using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplication4
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

public class WebService1 : System.Web.Services.WebService


{
[WebMethod]
public int Add(int x, int y)
{
return x + y;
}
[WebMethod]
public int Sub(int x, int y)
{
return x - y;
}
[WebMethod]
public int Mul(int x, int y)
{
return x * y;
}
[WebMethod]
public int Div(int x, int y)
{
3IT42: Dot Net Technology

return x / y;
}
}
}

Step 3:

To see whether the service is running correctly go to the Solution Explorer then open "Airthmatic.asmx"
and run your application.
3IT42: Dot Net Technology

Step 4: Creating the client application

<%@ Page Language="C#" AutoEventWireup="true"


CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="2" cellpadding="2" cellspacing="2">
<tr>
<td align="right">
<asp:Label ID="Label1" runat="server" Text="Enter 1st Number"></asp:Label>
</td>
<td align="left">
<asp:TextBox ID="txtFno" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label2" runat="server" Text="Enter 2nd Number"></asp:Label>
</td>
<td align="left">
<asp:TextBox ID="txtSno" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label3" runat="server" Text="Result"></asp:Label>
</td>
<td align="left">
<asp:Label ID="lblResult" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td align="center">
<asp:Button ID="btnAdd" runat="server" Text="Add(+)"
OnClick="btnAdd_Click" />
</td>
<td align="center">
3IT42: Dot Net Technology

<asp:Button ID="btnSub" runat="server" Text="Sub(-)" OnClick="btnSub_Click"


/>
</td>
</tr>
<tr>
<td align="center">
<asp:Button ID="BtnMul" runat="server" Text="Mul(*)"
OnClick="BtnMul_Click" />
</td>
<td align="center">
<asp:Button ID="btnDiv" runat="server" Text="Div(/)" OnClick="btnDiv_Click"
/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Step 5: Add reference to the following client application

For this right click on reference and the click on add reference…
Then it will show you to option for adding WebService1.asmx to client’s web site.

Step 6: Write code in WebForm1.aspx.cs for logic

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
3IT42: Dot Net Technology

using System.Web.UI.WebControls;

namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

WebService1 obj = new


WebService1(); int a, b, c;

protected void btnAdd_Click(object sender, EventArgs e)


{
a = Convert.ToInt32(txtFno.Text);
b =
Convert.ToInt32(txtSno.Text); c =
obj.Add(a, b);
lblResult.Text = c.ToString();
}
protected void btnSub_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(txtFno.Text);
b =
Convert.ToInt32(txtSno.Text); c =
obj.Sub(a, b);
lblResult.Text = c.ToString();
}
protected void BtnMul_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(txtFno.Text);
b =
Convert.ToInt32(txtSno.Text); c =
obj.Mul(a, b);
lblResult.Text = c.ToString();
}
protected void btnDiv_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(txtFno.Text);
b =
Convert.ToInt32(txtSno.Text); c =
obj.Div(a, b);
lblResult.Text = c.ToString();
}
}
3IT42: Dot Net Technology
}
3IT42: Dot Net Technology

Output of the Program:


i

You might also like