C# and ASP.NET Programming Exercises
C# and ASP.NET Programming Exercises
Index
Sr Aim Date Sign
No
1 Write C# programs for understanding C# basics
involving
Practical No 1
Aim: Write C# programs for understanding C# basics involving
a. Variables and Data Types b. Object-Based Manipulation
c. Conditional Logic d. Loops e. Methods
Even odd:
Source Code:
using System;
using [Link];
using [Link];
using [Link];
namespace evenodd
{
class Program
{
static void Main(string[] args)
{
int num;
[Link]("Enter a number:");
num = Convert.ToInt32([Link]());
if (num % 2 == 0)
{
[Link]("It is even number");
}
else
{
[Link]("It is odd number");
}
[Link]();
}
}
}
Output:
Factorial:
Source Code:
using System;
using [Link];
using [Link];
using [Link];
namespace factorial
{
class Program
{
static void Main(string[] args)
{
int i,num,fact=1;
[Link]("Enter a number:");
num = Convert.ToInt32([Link]());
for(i=1;i<=num;i++)
{
fact=fact*i;
}
[Link]("Factorial of " + num + " is " + fact);
[Link]();
}
}
}
Output:
Reverse No:
Source Code:
using System;
using [Link];
using [Link];
using [Link];
namespace reverse_no
{
class Program
{
static void Main(string[] args)
{
int num;
[Link]("Enter a number:");
num = Convert.ToInt32([Link]());
int reverse = 0;
while (num>0)
{
int reminder = num % 10;
reverse = (reverse * 10) + reminder;
num = num / 10;
}
[Link]("Reverse number is " + reverse);
[Link]();
}
}
}
Output:
using System;
using [Link];
using [Link];
using [Link];
namespace factorial_using_function
{
class factorialexample
{
public int factorial(int n)
{
int result;
if(n==1)
{
return 1;
}
else
{
result=factorial(n-1)*n;
return result;
}
}
static void Main(string[] args)
{
factorialexample f=new factorialexample();
int fact=[Link](5);
[Link]("factorial of number is: "+fact);
[Link]();
}
}
}
Output:
Practical No 2
using System;
using [Link];
using [Link];
using [Link];
namespace classdemo
{
public class Student
{
int rollno;
String name;
static void Main(string[] args)
{
Student s1 = new Student();
[Link] = 123;
[Link] = "ABC";
[Link]([Link]);
[Link]([Link]);
[Link]();
}
}
}
Output:
namespace area
{
class overloading
{
public void area(int side)
{
[Link]("Area of squre:" + (side * side));
}
public void area(int l, int b)
{
[Link]("Area of rectangle:" + (l * b));
[Link]();
}
Output:
using System;
using [Link];
using [Link];
using [Link];
namespace single_inheritance
{
public class animal
{
}
public class cat : animal
{
public void type()
{
[Link]("Domestic animal");
[Link]();
}
class inheritanceDemo
{
Output:
2) Hierarchical Inheritance:
Source code:
using System;
using [Link];
using [Link];
using [Link];
namespace hierarchical_inheritance
{
class A
{
public void msg()
{
[Link]("This is class A Method");
}
}
class B:A
{
public void info()
{
[Link]("This is class B Method");
}
}
class C:A
{
public void getinfo()
{
[Link]("This is class C Method");
[Link]();
}
}
class D
{
}
}
}
Output:
3) Multilevel Inheritance:
Source Code:
using System;
using [Link];
using [Link];
using [Link];
namespace multilevel_inheritance
{
class student
{
public int hin,mar,eng,sports,tot,avg;
public void getmarks()
{
[Link]("Enter marks of three subjects:");
hin = Convert.ToInt32([Link]());
mar = Convert.ToInt32([Link]());
eng = Convert.ToInt32([Link]());
}
}
class sportsclass:student
{
public void getsports()
{
[Link]("Enter marks of sports:");
sports = Convert.ToInt32([Link]());
}
}
class result:sportsclass
{
public void cal()
{
tot=hin+mar+eng+sports;
avg=tot/3;
[Link]("Total marks:"+tot);
[Link]("Total marks:"+tot);
[Link]("Average:"+avg);
[Link]();
}
}
class D
{
Output:
4) Multiple Inheritance:
Source Code:
using System;
using [Link];
using [Link];
using [Link];
namespace miltipleinheritance
{
Output:
5) Hybrid Inheritance:
Source Code:
using System;
using [Link];
using [Link];
using [Link];
namespace hybridinheritance
{
class Program
{
interface student
{
void getmarks();
}
interface sportsintf:student
{
void getsports();
}
interface testinft
{
void gettest();
}
class result:sportsintf,testinft
{
public int hin,mar,eng,sports,test,tot,avg;
public void getmarks()
{
[Link]("Enter marks of three subjects:");
hin=Convert.ToInt32([Link]());
mar=Convert.ToInt32([Link]());
eng=Convert.ToInt32([Link]());
}
public void getsports()
{
[Link]("Enter marks of sports:");
sports=Convert.ToUInt16([Link]());
}
public void gettest()
{
[Link]("Enter marks of test:");
test=Convert.ToInt32([Link]());
}
public void cal()
{
tot=hin+mar+eng+sports+test;
avg=tot/5;
[Link]("Total marks:"+tot);
[Link]("Average:"+avg);
[Link]();
}
class d
{
Output:
Practical No 3
Aim: Rich Controls (Calendar / Ad Rotator)
Calendar control
Source Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[Link]
[Link]">
<html xmlns="[Link]
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
Enter your Birthdate<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
</div>
</form>
</body>
</html>
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace CalendarEx
{
public partial class calendar : [Link]
{
protected void Page_Load(object sender, EventArgs e)
{
[Link] = [Link]();
[Link] = [Link]();
}
}}
Output:
Ad Rotator Control
Source Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="[Link]"
Inherits="[Link]" %>
<!DOCTYPE html>
<html xmlns="[Link]
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/[Link]" />
<br />
</div>
</form>
</body>
</html>
Output:
Practical No 4
Aim: Design [Link] Pages for State Management.
A) Cookies State:
Source Code:
[Link]:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="[Link]"
Inherits="cookie.WebForm1" %>
<html xmlns="[Link]
<head runat="server">
<title></title>
</head>
<body runat="server" id="BodyTag">
<form id="form1" runat="server">
<p>
<br />
<asp:DropDownList id="ColorSelector" runat="server" autopostback="true"
onselectedindexchanged="ColorSelector_SelectedIndexChanged">
<asp:ListItem>red</asp:ListItem>
<asp:ListItem>yellow</asp:ListItem>
<asp:ListItem>green</asp:ListItem>
<asp:ListItem>black</asp:ListItem>
<asp:ListItem>pink</asp:ListItem>
</asp:DropDownList>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<div>
</div>
</form>
</body>
</html>
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace cookie
{
public partial class WebForm1 : [Link]
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Output:
B) Application state:
Source Code:
[Link]:
<?xml version="1.0"?>
<!--
For more information on how to configure your [Link] application, please visit
[Link]
-->
<configuration>
<[Link]>
<compilation debug="true" targetFramework="4.0" />
<sessionState mode="InProc" timeout="20" cookieless="true">
</sessionState>
</[Link]>
</configuration>
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace appstateexample
{
public class Global : [Link]
{
}
}
}
Webform1. [Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace appstateexample
{
public partial class WebForm1 : [Link]
{
protected void Page_Load(object sender, EventArgs e)
{
[Link]("The no of online users: " + Application["user"].ToString());
}
}
}
Output:
C) Session state:
[Link]:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="[Link]"
Inherits="sessionstatedemo.WebForm1" %>
<html xmlns="[Link]
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:Label ID="Label1" runat="server" Text="Username:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Login" />
</div>
</form>
</body>
</html>
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace sessionstatedemo
{
public partial class WebForm1 : [Link]
{
protected void Page_Load(object sender, EventArgs e)
[Link]("[Link]");
}
}
}
[Link]:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="[Link]"
Inherits="sessionstatedemo.WebForm2" %>
<html xmlns="[Link]
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
Username:-<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<br />
Password:-<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
</div>
</form>
</body>
</html>
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace sessionstatedemo
{
public partial class WebForm2 : [Link]
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Username"] != null)
{
[Link] = Session["Username"].ToString();
}
if(Session["Password"]!=null)
{
[Link]=Session["Password"].ToString();
}
}
}
}
Output:
Practical No 5
Aim: Perform the following activities:
a) Design [Link] page and perform validation using various Validation
Controls
Source Code:
[Link]:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="[Link]"
Inherits="[Link]" %>
<html xmlns="[Link]
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server"
ControlToValidate="TextBox3" ErrorMessage="Your email id is not validate"
ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></
asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>Contact no </td>
<td>
<asp:TextBox ID="TextBox4"
runat="server"></asp:TextBox><asp:RegularExpressionValidator
ID="RegularExpressionValidator2" runat="server"
ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox4"
ValidationExpression="((\(\d{3}\)
?)|(\d{3}-))?\d{3}-\d{4}"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>New Password</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="CompareValidator" ControlToCompare="TextBox5"
ControlToValidate="TextBox6"
ValueToCompare="Textbox6"></asp:CompareValidator>
</td>
</tr>
<tr>
<td>Confirm Password </td>
<td>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="Button1" runat="server" Text="Submit" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Output:
b) Design an [Link] master web page and use it other (at least 2-3) content
pages.
Source Code:
[Link]:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="[Link]"
Inherits="[Link]" %>
<html xmlns="[Link]
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
<Items>
<asp:MenuItem Text="Home" Value="Home"
NavigateUrl="~/[Link]"></asp:MenuItem>
<asp:MenuItem Text="About us" Value="About us"
NavigateUrl="~/[Link]"></asp:MenuItem>
[Link]:
<%@ Page Title="" Language="C#" MasterPageFile="~/[Link]"
AutoEventWireup="true" CodeBehind="[Link]" Inherits="[Link]" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<p>
<br />
<asp:Label ID="Label1" runat="server" ForeColor="#FF6666"
Text="Welcome to Home page" Width="55px"></asp:Label>
</p>
<p>
</p>
<p>
</p>
</asp:Content>
[Link]:
<%@ Page Title="" Language="C#" MasterPageFile="~/[Link]"
AutoEventWireup="true" CodeBehind="[Link]" Inherits="[Link]" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
[Link]:
<%@ Page Title="" Language="C#" MasterPageFile="~/[Link]"
AutoEventWireup="true" CodeBehind="[Link]" Inherits="[Link]" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
Output:
Practical No:6
Aim: Performing [Link] data access in [Link].
A) ADO .net with console application.
Source Code:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace SQLConnectionConsoleAppDemo
{
class Program
{
//public Program()
//{
SqlConnection conn = new SqlConnection("Data Source= (local); Initial Catalog
=College; Integrated Security = SSPI");
//}
static void Main(string[] args)
{
Program obj1 = new Program();
[Link]("Before insert cammand ");
[Link]();
[Link]();
[Link]("After insert cammand ");
[Link]();
[Link]();
[Link]("After Delete cammand ");
[Link]();
[Link]();
}
public void ReadData()
{
SqlDataReader dtReader = null;
try
{
[Link]();
SqlCommand cmd = new SqlCommand("select * from Student", conn);
dtReader = [Link]();
while ([Link]())
{
Output:
Practical No: 7
Aim: Design [Link] application for Interacting (Reading / Writing) with XML documents
Source Code:
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace xmldemo2
{
class Program
{
static void Main(string[] args)
{
XmlTextWriter writer = new XmlTextWriter("[Link]",null);
[Link]("Books");
[Link]("ID","101");
[Link]("Title","JAVA");
[Link]("Price","200");
[Link]("ID","102");
[Link]("Title","Dot .NET");
[Link]("Price","256");
[Link]("ID","103");
[Link]("Title","CN");
[Link]("Price","210");
[Link]();
[Link]();
try
{
string out1 = "";
String format = "XmlNodeType::{0,10}{1,8}{2}";
while ([Link]())
{
{
out1 = [Link](format, ([Link]),
[Link], [Link]);
[Link](out1);
}
}
}
catch (Exception ex)
{
[Link]([Link]);
}
[Link]();
}
Output:
Practical No 8
Aim: Design [Link] Pages for Performance improvement using Caching
Source Code:
[Link]:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="[Link]"
Inherits="datacachingdemo.WebForm1" %>
<%@ OutputCache Duration="20" VaryByParam="none" %>
<html xmlns="[Link]
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:FormView ID="FormView1" runat="server" AllowPaging="True"
DataKeyNames="sid" DataSourceID="SqlDataSource2">
<EditItemTemplate>
sid:
<asp:Label ID="sidLabel1" runat="server" Text='<%# Eval("sid") %>' />
<br />
sname:
<asp:TextBox ID="snameTextBox" runat="server" Text='<%# Bind("sname") %>'
/>
<br />
saddr:
<asp:TextBox ID="saddrTextBox" runat="server" Text='<%# Bind("saddr") %>'
/>
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
sname:
<asp:TextBox ID="snameTextBox" runat="server" Text='<%# Bind("sname") %>'
/>
<br />
saddr:
<asp:TextBox ID="saddrTextBox" runat="server" Text='<%# Bind("saddr") %>'
/>
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
</div>
</form>
</body>
</html>
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace datacachingdemo
{
public partial class WebForm1 : [Link]
{
protected void Page_Load(object sender, EventArgs e)
{
[Link](20000);
[Link]("This page was generated and catched at :" +
[Link]());
[Link] = [Link]("Page posted at:{0}",
[Link]());
}
}
}
Output:
Practical No 9
Aim: Design [Link] application to query a Database using LINQ
Source code:
[Link]:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="[Link]"
Inherits="LINQDemo.WebForm1" %>
<html xmlns="[Link]
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
</div>
</form>
</body>
</html>
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace LINQDemo
{
public partial class WebForm1 : [Link]
{
public class Book_data
{
public string Id{get;set;}
public string Title {get;set;}
public decimal Price{get;set;}
}
}
}
Output:
Practical No 10
Aim: Design and use AJAX based [Link] pages.
Source Code:
[Link]:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="[Link]"
Inherits="AjaxDemo.WebForm1" %>
<html xmlns="[Link]
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace AjaxDemo
{
public partial class WebForm1 : [Link]
{
protected void Page_Load(object sender, EventArgs e)
{
Output: