0% found this document useful (0 votes)
78 views40 pages

Index SR No Aim Date Sign 1 2 3 4 5

This document contains code samples and output for various C# programs demonstrating core C# concepts like variables, data types, conditionals, loops, methods, classes, constructors, function overloading, inheritance, and namespaces. It is organized into multiple practical sections with each section focusing on specific C# features and providing 3-5 code examples along with their outputs.

Uploaded by

ron
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)
78 views40 pages

Index SR No Aim Date Sign 1 2 3 4 5

This document contains code samples and output for various C# programs demonstrating core C# concepts like variables, data types, conditionals, loops, methods, classes, constructors, function overloading, inheritance, and namespaces. It is organized into multiple practical sections with each section focusing on specific C# features and providing 3-5 code examples along with their outputs.

Uploaded by

ron
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/ 40

Name Roll No :

Index
Sr Aim Date Sign
No
1 Write C# programs for understanding C# basics
involving

2 Write C# programs for Object oriented concepts of C#

3 Design ASP.NET Pages with Rich Controls (Calendar /


Ad Rotator)
4 Design ASP.NET Pages for State Management

5 Perform the following activities


a. Design ASP.NET page and perform validation using
various Validation Controls
b. Design an APS.NET master web page and use it other
(at least 2-3) content pages.
c. Design ASP.NET Pages with various Navigation
Controls

6 Performing ADO.NET data access in ASP.NET

7 Design ASP.NET application for Interacting (Reading /


Writing) with XML documents

8 Design ASP.NET Pages for Performance improvement


using Caching

9 Design ASP.NET application to query a Database using


LINQ

10 Design and use AJAX based ASP.NET pages.

S.Y.C.S Sem IV .Net Technology pg. 1


Name Roll No :

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:

S.Y.C.S Sem IV .Net Technology pg. 2


Name Roll No :

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:

S.Y.C.S Sem IV .Net Technology pg. 3


Name Roll No :

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:

S.Y.C.S Sem IV .Net Technology pg. 4


Name Roll No :

Factorial using function:


Source Code:

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:

S.Y.C.S Sem IV .Net Technology pg. 5


Name Roll No :

Practical No 2

Aim: Write C# programs for Object oriented concepts of C# such as:


a. Program using classes
b. Constructor and Function Overloading
c. Inheritance d. Namespaces

Program using classes:


Source Code:

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:

S.Y.C.S Sem IV .Net Technology pg. 6


Name Roll No :

Constructor and Function Overloading :


Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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();
}

static void Main(string[] args)


{
overloading o = new overloading();
o.area(5);
o.area(4, 5);
}
}
}

Output:

S.Y.C.S Sem IV .Net Technology pg. 7


Name Roll No :

Inheritance & Namespaces


1) Single inheritance:
Source Code:

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

namespace single_inheritance
{
public class animal
{

public void like()


{
Console.WriteLine("Like Milk...");

}
public class cat : animal
{
public void type()
{
Console.WriteLine("Domestic animal");
Console.ReadLine();
}

class inheritanceDemo
{

static void Main(string[] args)


{
cat c1 = new cat();
c1.like();
c1.type();
}

Output:

S.Y.C.S Sem IV .Net Technology pg. 8


Name Roll No :

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
{

static void Main(string[] args)


{
B b1 = new B();
C c1 = new C();
b1.msg(); //can be called by both b1 and c1
b1.info();
c1.getinfo();

}
}
}
Output:

S.Y.C.S Sem IV .Net Technology pg. 9


Name Roll No :

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
{

static void Main(string[] args)


{
result r=new result();
r.getmarks();
r.getsports();
r.cal();
}
}
}

S.Y.C.S Sem IV .Net Technology pg. 10


Name Roll No :

Output:

4) Multiple Inheritance:
Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace miltipleinheritance
{

public interface Ibase1


{
void message();
}
public interface Ibase2
{
void message();
}
public class child : Ibase1, Ibase2
{
public void message()
{
Console.WriteLine("Hello Multiple Inheritance");
}
class Program
{
static void Main(string[] args)
{
child ch = new child();
ch.message();
Console.ReadKey();
}
}
}

Output:

S.Y.C.S Sem IV .Net Technology pg. 11


Name Roll No :

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
{

S.Y.C.S Sem IV .Net Technology pg. 12


Name Roll No :

static void Main(string[] args)


{
result r=new result();
r.getmarks();
r.getsports();
r.gettest();
r.cal();
}
}
}
}
}

Output:

S.Y.C.S Sem IV .Net Technology pg. 13


Name Roll No :

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&nbsp; 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)
{

protected void Calendar1_SelectionChanged(object sender, EventArgs e)


{

Label1.Text = Calendar1.TodaysDate.ToShortDateString();
Label2.Text = Calendar1.SelectedDate.ToShortDateString();
}

S.Y.C.S Sem IV .Net Technology pg. 14


Name Roll No :

}}
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>

S.Y.C.S Sem IV .Net Technology pg. 15


Name Roll No :

Output:

S.Y.C.S Sem IV .Net Technology pg. 16


Name Roll No :

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" %>

<!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 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>
&nbsp;</p>
<p>
&nbsp;</p>
<p>
&nbsp;</p>
<p>
&nbsp;</p>
<p>
&nbsp;</p>
<p>
&nbsp;</p>
<div>

</div>
</form>
</body>
</html>

Webform1.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;

S.Y.C.S Sem IV .Net Technology pg. 17


Name Roll No :

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

protected void ColorSelector_SelectedIndexChanged(object sender, EventArgs e)


{
BodyTag.Style["background-color"] = ColorSelector.SelectedValue;

HttpCookie cookie = new HttpCookie("Backgroundcolor");


cookie.Value = ColorSelector.SelectedValue;
cookie.Expires = DateTime.Now.AddHours(1);
Response.SetCookie(cookie);
}

}
}
Output:

S.Y.C.S Sem IV .Net Technology pg. 18


Name Roll No :

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
{

protected void Application_Start(object sender, EventArgs e)


{
Application["user"] = 0;

protected void Session_Start(object sender, EventArgs e)


{
Application.Lock();
Application["user"] = (int)Application["user"] +1;
Application.UnLock();

protected void Application_BeginRequest(object sender, EventArgs e)


{

S.Y.C.S Sem IV .Net Technology pg. 19


Name Roll No :

protected void Application_AuthenticateRequest(object sender, EventArgs e)


{

protected void Application_Error(object sender, EventArgs e)


{

protected void Session_End(object sender, EventArgs e)


{
Application.Lock();
Application["user"] = (int)Application["user"] - 1;
Application.UnLock();

protected void Application_End(object sender, EventArgs e)


{

}
}
}

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:

S.Y.C.S Sem IV .Net Technology pg. 20


Name Roll No :

C) Session state:
WebForm1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="sessionstatedemo.WebForm1" %>

<!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 />
<asp:Label ID="Label1" runat="server" Text="Username:"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<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)

S.Y.C.S Sem IV .Net Technology pg. 21


Name Roll No :

protected void Button1_Click(object sender, EventArgs e)


{
Session["Username"] = TextBox1.Text;
Session["Password"] = TextBox2.Text;

Response.Redirect("WebForm2.aspx");
}
}
}

WebForm2.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs"
Inherits="sessionstatedemo.WebForm2" %>

<!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 />
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

S.Y.C.S Sem IV .Net Technology pg. 22


Name Roll No :

{
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:

S.Y.C.S Sem IV .Net Technology pg. 23


Name Roll No :

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" %>

<!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>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />

<table id="Table1" runat="server" align="center">


<tr>
<td>Enter name </td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please enter your name" ControlToValidate="TextBox1"
ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>age </td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ErrorMessage="you are not eligible" ControlToValidate="TextBox2"
ForeColor="Red" MaximumValue="50" MinimumValue="18"></asp:RangeValidator>
</td>
</tr>
<tr>
<td>Email id</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

S.Y.C.S Sem IV .Net Technology pg. 24


Name Roll No :

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

S.Y.C.S Sem IV .Net Technology pg. 25


Name Roll No :

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" %>

<!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>
<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>

S.Y.C.S Sem IV .Net Technology pg. 26


Name Roll No :

<asp:MenuItem Text="Computer sci" Value="Computer sci">


<asp:MenuItem Text="FY" Value="FY"></asp:MenuItem>
<asp:MenuItem Text="SY" Value="SY"></asp:MenuItem>
<asp:MenuItem Text="TY" Value="TY"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">


<asp:Menu ID="Menu2" runat="server" values="Contact us"
NavigateUrl="~/contact.aspx">
</asp:Menu>
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>

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>

S.Y.C.S Sem IV .Net Technology pg. 27


Name Roll No :

Output:

S.Y.C.S Sem IV .Net Technology pg. 28


Name Roll No :

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())
{

S.Y.C.S Sem IV .Net Technology pg. 29


Name Roll No :

Console.WriteLine(dtReader[0] + " " + dtReader[1] + " " + dtReader[2]


+ " " + dtReader[3]);
}
}
finally
{
if (dtReader != null)
dtReader.Close();
if (conn != null)
conn.Close();
}
}
public void InsertData()
{
try
{
conn.Open();
string name = "PQR ", loc = "Thane";
int marks = 86;

//string insString="INSERT INTO Student(SName,SLocation,SMarks)


VALUES('PQR','Thane',85)";
string insString = "INSERT INTO Student(SName,SLocation,SMarks) VALUES('"
+ name + "','" + loc + "'," + marks + ")";
SqlCommand cmd = new SqlCommand(insString, conn);
cmd.ExecuteNonQuery();// used to insert update data in DB
}
finally
{
if (conn != null)
conn.Close();
}
}
public void DeleteData()
{
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("Delete from Student where SName ='PQR'",
conn);
cmd.ExecuteNonQuery();// used to insert update data in DB
}
finally
{
if (conn != null)
conn.Close();
}
}
}
}

S.Y.C.S Sem IV .Net Technology pg. 30


Name Roll No :

Output:

S.Y.C.S Sem IV .Net Technology pg. 31


Name Roll No :

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}";

XmlTextReader xmlreader = new XmlTextReader("BookData.xml");

while (xmlreader.Read())
{
{
out1 = String.Format(format, (xmlreader.NodeType),
xmlreader.Name, xmlreader.Value);
Console.WriteLine(out1);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

S.Y.C.S Sem IV .Net Technology pg. 32


Name Roll No :

Console.ReadLine();
}

Output:

S.Y.C.S Sem IV .Net Technology pg. 33


Name Roll No :

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" %>

<!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 />
<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" />
&nbsp;<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" />

S.Y.C.S Sem IV .Net Technology pg. 34


Name Roll No :

&nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"


CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
sid:
<asp:Label ID="sidLabel" runat="server" Text='<%# Eval("sid") %>' />
<br />
sname:
<asp:Label ID="snameLabel" runat="server" Text='<%# Bind("sname") %>' />
<br />
saddr:
<asp:Label ID="saddrLabel" runat="server" Text='<%# Bind("saddr") %>' />
<br />

<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"


CommandName="Edit" Text="Edit" />
&nbsp;<asp:LinkButton ID="DeleteButton" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete" />
&nbsp;<asp:LinkButton ID="NewButton" runat="server"
CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:collegeConnectionString %>"
DeleteCommand="DELETE FROM [stud] WHERE [sid] = @original_sid AND [sname] =
@original_sname AND [saddr] = @original_saddr"
InsertCommand="INSERT INTO [stud] ([sname], [saddr]) VALUES (@sname, @saddr)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [stud]"
UpdateCommand="UPDATE [stud] SET [sname] = @sname, [saddr] = @saddr WHERE
[sid] = @original_sid AND [sname] = @original_sname AND [saddr] = @original_saddr">
<DeleteParameters>
<asp:Parameter Name="original_sid" Type="Int32" />
<asp:Parameter Name="original_sname" Type="String" />
<asp:Parameter Name="original_saddr" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="saddr" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="saddr" Type="String" />
<asp:Parameter Name="original_sid" Type="Int32" />
<asp:Parameter Name="original_sname" Type="String" />
<asp:Parameter Name="original_saddr" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<br />
<br />
<br />
<br />
<br />
<br />

S.Y.C.S Sem IV .Net Technology pg. 35


Name Roll No :

</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:

S.Y.C.S Sem IV .Net Technology pg. 36


Name Roll No :

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" %>

<!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 />
<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;}

public static List<Book_data>getBooks()


{
List<Book_data> B_list=new List<Book_data>();
B_list.Add(new Book_data{Id="01",Title="System Prog",Price=620});

S.Y.C.S Sem IV .Net Technology pg. 37


Name Roll No :

B_list.Add(new Book_data{Id="02",Title="Web Technology",Price=250});


B_list.Add(new Book_data{Id="03",Title="Visual Basic",Price=320});
B_list.Add(new Book_data{Id="01",Title="Adv JAVA",Price=210});
return B_list;
}
}

protected void Page_Load(object sender, EventArgs e)


{
List<Book_data> Books=Book_data.getBooks();

var bookTitles=from t1 in Books select t1.Title;

foreach (var i in bookTitles)


Label1.Text+=i+"<br>";

}
}
}

Output:

S.Y.C.S Sem IV .Net Technology pg. 38


Name Roll No :

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" %>

<!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>

<asp:ScriptManager ID="ScriptManager1" runat="server">


</asp:ScriptManager>
<br />
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Partial PostBack" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Total PostBack" />
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />

S.Y.C.S Sem IV .Net Technology pg. 39


Name Roll No :

</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)
{

protected void Button1_Click(object sender, EventArgs e)


{
string s1 = DateTime.Now.ToLongTimeString();
Label1.Text = "Showing Time From Panel Control " + s1;
Label2.Text = "Showing time from Outside panel control" + s1;
}

protected void Button2_Click(object sender, EventArgs e)


{
string s1 = DateTime.Now.ToLongTimeString();
Label1.Text = "Showing Time From Panel Control " + s1;
Label2.Text = "Showing time from Outside panel control" + s1;
}
}
}

Output:

S.Y.C.S Sem IV .Net Technology pg. 40

You might also like