Index SR No Aim Date Sign 1 2 3 4 5
Index SR No Aim Date Sign 1 2 3 4 5
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 System.Collections.Generic;
using System.Linq;
using System.Text;
namespace evenodd
{
class Program
{
static void Main(string[] args)
{
int num;
Console.WriteLine("Enter a number:");
num = Convert.ToInt32(Console.ReadLine());
if (num % 2 == 0)
{
Console.WriteLine("It is even number");
}
else
{
Console.Write("It is odd number");
}
Console.ReadLine();
}
}
}
Output:
Factorial:
Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace factorial
{
class Program
{
static void Main(string[] args)
{
int i,num,fact=1;
Console.WriteLine("Enter a number:");
num = Convert.ToInt32(Console.ReadLine());
for(i=1;i<=num;i++)
{
fact=fact*i;
}
Console.WriteLine("Factorial of " + num + " is " + fact);
Console.ReadLine();
}
}
}
Output:
Reverse No:
Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace reverse_no
{
class Program
{
static void Main(string[] args)
{
int num;
Console.WriteLine("Enter a number:");
num = Convert.ToInt32(Console.ReadLine());
int reverse = 0;
while (num>0)
{
int reminder = num % 10;
reverse = (reverse * 10) + reminder;
num = num / 10;
}
Console.WriteLine("Reverse number is " + reverse);
Console.ReadLine();
}
}
}
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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=f.factorial(5);
Console.WriteLine("factorial of number is: "+fact);
Console.ReadLine();
}
}
}
Output:
Practical No 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace classdemo
{
public class Student
{
int rollno;
String name;
static void Main(string[] args)
{
Student s1 = new Student();
s1.rollno = 123;
s1.name = "ABC";
Console.WriteLine(s1.rollno);
Console.WriteLine(s1.name);
Console.ReadLine();
}
}
}
Output:
namespace area
{
class overloading
{
public void area(int side)
{
Console.WriteLine("Area of squre:" + (side * side));
}
public void area(int l, int b)
{
Console.WriteLine("Area of rectangle:" + (l * b));
Console.ReadLine();
}
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace single_inheritance
{
public class animal
{
}
public class cat : animal
{
public void type()
{
Console.WriteLine("Domestic animal");
Console.ReadLine();
}
class inheritanceDemo
{
Output:
2) Hierarchical Inheritance:
Source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace hierarchical_inheritance
{
class A
{
public void msg()
{
Console.WriteLine("This is class A Method");
}
}
class B:A
{
public void info()
{
Console.WriteLine("This is class B Method");
}
}
class C:A
{
public void getinfo()
{
Console.WriteLine("This is class C Method");
Console.ReadLine();
}
}
class D
{
}
}
}
Output:
3) Multilevel Inheritance:
Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace multilevel_inheritance
{
class student
{
public int hin,mar,eng,sports,tot,avg;
public void getmarks()
{
Console.WriteLine("Enter marks of three subjects:");
hin = Convert.ToInt32(Console.ReadLine());
mar = Convert.ToInt32(Console.ReadLine());
eng = Convert.ToInt32(Console.ReadLine());
}
}
class sportsclass:student
{
public void getsports()
{
Console.WriteLine("Enter marks of sports:");
sports = Convert.ToInt32(Console.ReadLine());
}
}
class result:sportsclass
{
public void cal()
{
tot=hin+mar+eng+sports;
avg=tot/3;
Console.WriteLine("Total marks:"+tot);
Console.WriteLine("Total marks:"+tot);
Console.WriteLine("Average:"+avg);
Console.ReadLine();
}
}
class D
{
Output:
4) Multiple Inheritance:
Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace miltipleinheritance
{
Output:
5) Hybrid Inheritance:
Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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()
{
Console.WriteLine("Enter marks of three subjects:");
hin=Convert.ToInt32(Console.ReadLine());
mar=Convert.ToInt32(Console.ReadLine());
eng=Convert.ToInt32(Console.ReadLine());
}
public void getsports()
{
Console.WriteLine("Enter marks of sports:");
sports=Convert.ToUInt16(Console.ReadLine());
}
public void gettest()
{
Console.WriteLine("Enter marks of test:");
test=Convert.ToInt32(Console.ReadLine());
}
public void cal()
{
tot=hin+mar+eng+sports+test;
avg=tot/5;
Console.WriteLine("Total marks:"+tot);
Console.WriteLine("Average:"+avg);
Console.ReadLine();
}
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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 System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CalendarEx
{
public partial class calendar : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Calendar1.TodaysDate.ToShortDateString();
Label2.Text = Calendar1.SelectedDate.ToShortDateString();
}
}}
Output:
Ad Rotator Control
Source Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddRotator.aspx.cs"
Inherits="AdRotatorEx.AddRotator" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/ADS.xml" />
<br />
</div>
</form>
</body>
</html>
Output:
Practical No 4
Aim: Design ASP.NET Pages for State Management.
A) Cookies State:
Source Code:
Webform1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="cookie.WebForm1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
Webform1.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace cookie
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Output:
B) Application state:
Source Code:
Web.config:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<sessionState mode="InProc" timeout="20" cookieless="true">
</sessionState>
</system.web>
</configuration>
Global.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace appstateexample
{
public class Global : System.Web.HttpApplication
{
}
}
}
Webform1. aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace appstateexample
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("The no of online users: " + Application["user"].ToString());
}
}
}
Output:
C) Session state:
WebForm1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="sessionstatedemo.WebForm1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
WebForm1.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace sessionstatedemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
Response.Redirect("WebForm2.aspx");
}
}
}
WebForm2.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs"
Inherits="sessionstatedemo.WebForm2" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
WebForm2.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace sessionstatedemo
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Username"] != null)
{
TextBox1.Text = Session["Username"].ToString();
}
if(Session["Password"]!=null)
{
TextBox2.Text=Session["Password"].ToString();
}
}
}
}
Output:
Practical No 5
Aim: Perform the following activities:
a) Design ASP.NET page and perform validation using various Validation
Controls
Source Code:
Registration.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs"
Inherits="validationControlEx.Registration" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<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 APS.NET master web page and use it other (at least 2-3) content
pages.
Source Code:
MasterPage.Master:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Mymasterpage.master.cs"
Inherits="MasterPageEX.Mymasterpage" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<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="~/Home.aspx"></asp:MenuItem>
<asp:MenuItem Text="About us" Value="About us"
NavigateUrl="~/Aboutus.aspx"></asp:MenuItem>
Home.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/Mymasterpage.Master"
AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="MasterPageEX.Home" %>
<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>
Abouts.axps:
<%@ Page Title="" Language="C#" MasterPageFile="~/Mymasterpage.Master"
AutoEventWireup="true" CodeBehind="Aboutus.aspx.cs" Inherits="MasterPageEX.Aboutus" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
Contact.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/Mymasterpage.Master"
AutoEventWireup="true" CodeBehind="contact.aspx.cs" Inherits="MasterPageEX.contact" %>
<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 ADO.NET data access in ASP.NET.
A) ADO .net with console application.
Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
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();
Console.WriteLine("Before insert cammand ");
obj1.ReadData();
obj1.InsertData();
Console.WriteLine("After insert cammand ");
obj1.ReadData();
obj1.DeleteData();
Console.WriteLine("After Delete cammand ");
obj1.ReadData();
Console.ReadLine();
}
public void ReadData()
{
SqlDataReader dtReader = null;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Student", conn);
dtReader = cmd.ExecuteReader();
while (dtReader.Read())
{
Output:
Practical No: 7
Aim: Design ASP.NET application for Interacting (Reading / Writing) with XML documents
Source Code:
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
namespace xmldemo2
{
class Program
{
static void Main(string[] args)
{
XmlTextWriter writer = new XmlTextWriter("BookData.xml",null);
writer.WriteStartElement("Books");
writer.WriteElementString("ID","101");
writer.WriteElementString("Title","JAVA");
writer.WriteElementString("Price","200");
writer.WriteElementString("ID","102");
writer.WriteElementString("Title","Dot .NET");
writer.WriteElementString("Price","256");
writer.WriteElementString("ID","103");
writer.WriteElementString("Title","CN");
writer.WriteElementString("Price","210");
writer.WriteEndElement();
writer.Close();
try
{
string out1 = "";
String format = "XmlNodeType::{0,10}{1,8}{2}";
while (xmlreader.Read())
{
{
out1 = String.Format(format, (xmlreader.NodeType),
xmlreader.Name, xmlreader.Value);
Console.WriteLine(out1);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
Output:
Practical No 8
Aim: Design ASP.NET Pages for Performance improvement using Caching
Source Code:
WebForm1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="datacachingdemo.WebForm1" %>
<%@ OutputCache Duration="20" VaryByParam="none" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
WebForm1.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
namespace datacachingdemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Thread.Sleep(20000);
Response.Write("This page was generated and catched at :" +
DateTime.Now.ToString());
Label1.Text = String.Format("Page posted at:{0}",
DateTime.Now.ToLongTimeString());
}
}
}
Output:
Practical No 9
Aim: Design ASP.NET application to query a Database using LINQ
Source code:
WebForm1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="LINQDemo.WebForm1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
WebForm1.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LINQDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
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 ASP.NET pages.
Source Code:
WebForm.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="AjaxDemo.WebForm1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
WebForm1.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AjaxDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Output: