Advance Web Programming Practicals
Advance Web Programming Practicals
a. Create an application that obtains four int values from the user and display the
product.
CODE :
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
DESIGN : OUTPUT :
b. Create an application to demonstrate string operation.
Code:
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
private readonly IEnumerable<object> words;
Code:
using System;
struct Student
{
public string studid, name, cname;
public string dob;
}
public partial class _Default : System.Web.UI.Page
{
static Student[] s = new Student[3];
static int i;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
Response.Write(" Add Student Information No: "+" " + i);
s[i].studid = TextBox1.Text;
s[i].name = TextBox2.Text;
s[i].cname = TextBox4.Text;
s[i].dob = TextBox3.Text;
i++;
}
protected void Button2_Click1(object sender, EventArgs e)
{
for (int y = 0; y < i; y++)
{
Response.Write("Student Information:"+" " + y +"<br>");
Response.Write("Student id :" + " " + s[y].studid + "<br>");
Response.Write("Name :" + " " + s[y].name + "<br>");
Response.Write("Date Of birth :" + " " + s[y].dob + "<br>");
Response.Write("class : " + " " + s[y].cname + "<br>");
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
}
1.Generating Fibonacci series
using System;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Code:
using System;
public partial class Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Code:
using System;
public partial class operation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Fig.1.1 Factorial
Fig.1.2 Euro ammount
Fig.1.3 Temperature
4. Quadratic Equation
Code:
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
else
Label3.Text = "The given roots are imaginary";
}
}
1.Function Overloading
Code:
using System;
public class functionover
{
public int sum(int a, int b)
{
int x;
return x = a + b;
}
b=new functionoveer();
}
2.constructor overloading
Code:
using System;
public class marklist
{
int a, b, c;
public marklist()
{
a = 5;
b = 2;
c = 6;
}
public marklist(int e)
{
a = e;
}
Label4.Text = Convert.ToString(l.total());
int a = Convert.ToInt16(TextBox1.Text);
int b = Convert.ToInt16(TextBox2.Text);
marklist l1 = new marklist(a);
Label5.Text = Convert.ToString(l1.total());
Label6.Text = Convert.ToString(l2.total());
}
}
3.Inheritance
Code:
using System;
public class Base1
{
public int d;
public string basemethod()
{
string p = "This is base class method";
return p;
}
}
ch.basemethod();
}
}
4. Interfaces
Code:
using System;
public interface ITransactions
{
string retcode();
double amtfunc();
}
public class Transaction : ITransactions
{
private string tCode;
private double amount;
public Transaction()
{
tCode = "";
amount = 0.0;
}
public Transaction(string c, double a)
{
tCode = c;
amount = a;
}
public double amtfunc()
{
return amount;
}
public string retcode()
{
return tCode;
}
}
}
}
c. Create simple application to demonstrate use of the following concepts:
2. Exception Handling
Code:
using System;
class NegativeException : Exception
{
public NegativeException(string msg)
:base(msg)
{
}
}
calndrCtrl.aspx.cs
protected void btnResult_Click(object sender, EventArgs e)
{
Calendar1.Caption = "SAMBARE";
Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;
Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth;
Calendar1.TitleFormat = TitleFormat.Month;
Label2.Text = "Todays Date"+Calendar1.TodaysDate.ToShortDateString();
Label3.Text = "Ganpati Vacation Start: 9-13-2018";
TimeSpan d = new DateTime(2018, 9, 13) - DateTime.Now;
Label4.Text = "Days Remaining For Ganpati Vacation:"+d.Days.ToString();
TimeSpan d1 = new DateTime(2018, 12, 31) - DateTime.Now;
Label5.Text = "Days Remaining for New Year:"+d1.Days.ToString();
if (Calendar1.SelectedDate.ToShortDateString() == "9-13-2018")
Label3.Text = "<b>Ganpati Festival Start</b>";
if (Calendar1.SelectedDate.ToShortDateString() == "9-23-2018")
Label3.Text = "<b>Ganpati Festival End</b>";
}
protected void Calendar1_DayRender(object sender,
System.Web.UI.WebControls.DayRenderEventArgs e)
{
if (e.Day.Date.Day == 5 && e.Day.Date.Month == 9)
{
e.Cell.BackColor = System.Drawing.Color.Yellow;
Label lbl = new Label();
lbl.Text = "<br>Teachers Day!";
e.Cell.Controls.Add(lbl);
Image g1 = new Image();
g1.ImageUrl = "td.jpg";
g1.Height = 20;
g1.Width = 20;
e.Cell.Controls.Add(g1);
}
if (e.Day.Date.Day == 13 && e.Day.Date.Month == 9)
{
Calendar1.SelectedDate = new DateTime(2018, 9, 12);
Calendar1.SelectedDates.SelectRange(Calendar1.SelectedDate,
Calendar1.SelectedDate.AddDays(10));
Label lbl1 = new Label();
lbl1.Text = "<br>Ganpati!";
e.Cell.Controls.Add(lbl1);
}
}
protected void btnReset_Click(object sender, EventArgs e)
{
Label1.Text = "";
Label2.Text = "";
Label3.Text = "";
Label4.Text = "";
Label5.Text = "";
Calendar1.SelectedDates.Clear();
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Label1.Text = "Your Selected Date:" + Calendar1.SelectedDate.Date.ToString();
}
OUTPUT
Practical 3(c).Demonstrate the use of Treeview control perform following operations.
a) Treeview control and datalist b) Treeview operations
Add XML File
Website -> Add -> XML File and Name it ‘stdetail’.
stdetail.xml
<?xml version="1.0" encoding="utf-8" ?>
<studentdetail>
<student>
<sid>1</sid>
<sname>Tushar</sname>
<sclass>TYIT</sclass>
</student>
<student>
<sid>2</sid>
<sname>Sonali</sname>
<sclass>TYCS</sclass>
</student>
<student>
<sid>3</sid>
<sname>Yashashree</sname>
<sclass>TYIT</sclass>
</student>
<student>
<sid>4</sid>
<sname>Vedshree</sname>
<sclass>TYCS</sclass>
</student>
</studentdetail>
Default2.aspx
<form id="form1" runat="server">
<div>
Treeview control navigation:<asp:TreeView ID = "TreeView1" runat = "server" Width =
"150px" ImageSet="Arrows">
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<Nodes>
<asp:TreeNode Text = "ASP.NET Practs" Value = "New Node">
<asp:TreeNode Text = "Calendar Control" Value = "RED" NavigateUrl="~/calndrCtrl.aspx">
</asp:TreeNode>
<asp:TreeNode Text = "Constructor Overloading" Value = "GREEN"
NavigateUrl="~/clsconstrc.aspx"> </asp:TreeNode>
<asp:TreeNode NavigateUrl="~/singleInh.aspx" Text="Inheritance"
Value="BLUE"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/clsProp.aspx" Text="Class Properties" Value="Class
Properties"></asp:TreeNode>
</asp:TreeNode>
</Nodes>
<NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black"
HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px"
VerticalPadding="0px" />
</asp:TreeView>
<br />
Fetch Datalist Using XML data : </div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<table class = "table" border="1">
<tr>
<td>Roll Num : <%# Eval("sid") %><br />
Name : <%# Eval("sname") %><br />
Class : <%# Eval("sclass")%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Default2.aspx.cs
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("stdetail.xml"));
if (ds != null && ds.HasChanges())
{
DataList1.DataSource = ds;
DataList1.DataBind();
}
else
{
DataList1.DataBind();
}
}
}
OUTPUT
Practical 4(b).Create Web Form to demonstrate use of Adrotator Control.
Add a XML file, name it "adds.xml"
XML File
<Advertisements>
<Ad>
<ImageUrl>rose1.jpg</ImageUrl>
<NavigateUrl>http://www.1800flowers.com</NavigateUrl>
<AlternateText>
Order flowers, roses, gifts and more
</AlternateText>
<Impressions>20</Impressions>
<Keyword>flowers</Keyword>
</Ad>
<Ad>
<ImageUrl>rose2.jpg</ImageUrl>
<NavigateUrl>http://www.babybouquets.com.au</NavigateUrl>
<AlternateText>Order roses and flowers</AlternateText>
<Impressions>20</Impressions>
<Keyword>gifts</Keyword>
</Ad>
<Ad>
<ImageUrl>rose3.jpeg</ImageUrl>
<NavigateUrl>http://www.flowers2moscow.com</NavigateUrl>
<AlternateText>Send flowers to Russia</AlternateText>
<Impressions>20</Impressions>
<Keyword>russia</Keyword>
</Ad>
</Advertisements>
Default.aspx
.
.
MyUserControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyUserControl.ascx.cs"
Inherits="MyUserControl" %>
<h3>This is User Contro1 </h3>
<table>
<tr>
<td>Name</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>City</td>
<td><asp:TextBox ID="txtcity" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="txtSave" runat="server" Text="Save" onclick="txtSave_Click" /> </td>
</tr>
</table><br />
<asp:Label ID="Label1" runat="server" ForeColor="White" Text=" "></asp:Label>
MyUserControl.ascx.cs
protected void txtSave_Click(object sender, EventArgs e)
{
Label1.Text = "Your Name is " + txtName.Text + " and you are from " + txtcity.Text;
}
UserControlDisplay.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserControlDisplay.aspx.cs"
Inherits="UserControlDisplay" %>
<%@ Register Src="~/MyUserControl.ascx" TagPrefix="uc" TagName="Student"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc:Student ID="studentcontrol" runat="server" />
</div>
</form>
</body>
</html>
OUTPUT :
Practical 5(b).Create a web application to demonstrate use of Master Page with applying
Styles and Themes for page beautification.
Adding Master Page
MasterPage.master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs"
Inherits="MasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Master Page Demo</title>
<link href="css/my.css" rel="stylesheet" />
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<style type="text/css">
.auto-style1 {
position: absolute;
top: 373px;
left: 1028px;
bottom: 303px;
}
.auto-style2 {
position: absolute;
top: 537px;
left: 1016px;
z-index: 1;
}
</style>
</head>
<body>
<!DOCTYPE html>
<form id="form1" runat="server">
<html>
<head>
<title>Master</title>
<link rel="stylesheet" type="text/css" href="StyleSheet.css">
</head>
<body>
<header id="header">
<h1>Demo Of Master Page</h1>
</header>
<nav id="nav">
<ul>
<li><a href="home.aspx">Insight</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Downloads</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</nav>
<aside id="side">
<h1>Info</h1>
<a href="#"><p>Product Type 1</p></a>
<a href="#"><p>Product Type 2</p></a>
<a href="#"><p>Product Type 3<a href="#"><asp:ScriptManager ID="ScriptManager1"
runat="server">
</asp:ScriptManager>
</a>
</p>
<asp:Button ID="Button2" runat="server" CssClass="auto-style1" style="z-index: 1"
Text="Button" />
<asp:Button ID="Button1" runat="server" CssClass="auto-style2" Text="Button" />
</aside>
<div id="con">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
<footer id="footer">
copyright @Sambare
</footer>
</body>
</html>
</form>
</body>
</html>
MasterDisplay.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true" CodeFile="MasterDisplay.aspx.cs" Inherits="MasterDisplay" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<h1>Home page</h1>
</asp:Content>
StyleSheet.css
#header{
color: blueviolet;
text-align: center;
font-size: 20px;
}
#nav{
background-color:darkseagreen;
padding: 5px;
}
ul{
list-style-type: none;
}
li a {
color:crimson ;
font-size: 30px;
column-width: 5%;
}
li
{
display: inline;
padding-left: 2px;
column-width: 20px;
}
a{
text-decoration: none;
margin-left:20px
}
li a:hover{
background-color: aqua;
color:coral ;
padding:1%;
}
#side{
text-align: center;
float: right;
width: 15%;
padding-bottom: 79%;
background-color: #F1FAEE;
}
#article{
background-color: burlywood;
padding: 10px;
padding-bottom: 75%;
}
#footer{
background-color: #C7EFCF;
text-align:center;
padding-bottom: 5%;
font-size: 20px;
}
#con{
border:double;
border-color:burlywood;
}
***Database Practicals************
Note: For Database practical’s we have used SQL Server 2014 version.
Here we to add new database in our website, as shown below. Add this database inside
App_Data folder.
Practical 6 (a): Create a web application to bind data in a multiline textbox by querying
in another textbox.
1. Create a webpage with one Button, one Multiline TextBox and one list box with setting
TextMode Property of text box to Multiline as shown below.
2. Write the Database related code in code behind C# file as given below.
Note: The users have to use their own system connection string in place of connection string
given in following code.
The connection string is available in Server Explorer (Right click on Database Name and
Select Properties) as displayed below. User can copy this connection string and can use in
code.
Web.confing
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<connectionStrings>
<add name="connStr" connectionString="Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\tushars\Documents\Visual Studio
2015\WebSites\Workshop\App_Data\Database.mdf';Integrated Security=True" />
</connectionStrings>
</configuration>
4. Now use the following code C# in Default.aspx.cs (Note : First write following using
statements at the top of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class DataBinding : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connStr =
ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
con.Open();
SqlCommand cmd = new SqlCommand(TextBox1.Text, con);
SqlDataReader reader = cmd.ExecuteReader();
ListBox1.Items.Clear();
while (reader.Read())
{
//To add new blank line in the text area
for (int i = 0; i < reader.FieldCount - 1; i++)
{
ListBox1.Items.Add(reader[i].ToString());
}
}
reader.Close();
con.Close();
}
}
Output:
Practical 6 (b): Create a web application to display records by using database.
Create a web page with following design:
Add the following code on Button click event in C# Code behind file.
protected void Button1_Click(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("Select City, State from Customer", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Label1.Text += reader["City"].ToString() + " " + reader["State"].ToString() + "<br>";
}
reader.Close();
con.Close();
}
Output:
10. Once the Connection is made then click on Next button from Data Source Wizard.
11. Then wizard ask for saving the connection string in configuration file. If you already stored
it web.config file then uncheck check box, if you haven’t, then select the checkbook. Then click
on next button.
12. The next screen gives option to configure the select statement. Here we can choose the
table as well as configure the select statement as we need to display the data on web page.
13. In next screen we can test our query to check the output. Then Click on finish.
After successful steps form the Datalist controls option wizard our web page design and output
will look like following.
Practical 7 (a): Create a web application to display Databinding using Dropdownlist
control.
1. Create a web page with DropDownList control, one Button and one Label control.
2. Use code to bind the data to DropDownList.
Create a web page with DropDownList, Button and with Label control as shown below.
Code of C# Code behind file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class PostalCodeByCity : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = ListBox1.SelectedValue;
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("Select Distinct POSTAL_CODE from Customer", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
ListBox1.DataSource = reader;
ListBox1.DataTextField = "City";
ListBox1.DataValueField = "POSTAL_CODE";
ListBox1.DataBind();
reader.Close();
con.Close();
}
}
}
Output:
Practical 7 (c): Create a web application for inserting and deleting record from a
database. (Using Execute-Non Query).
Create a web page with TextBox, and Two Button and one Label control as shown below.
And follow the database related steps same as it is in previous examples.
Practical 9
->
Choose Data Source-> New Data Source option From GridView Tasks.
Add new column option form GridView Tasks
Add field CommandField, Header Text= Edit,Button type = Button also
Select Edit/Update Checkbox with show cancel Button checkbox.
Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
o/p:-
Practical 10
a. Create a web application to demonstrate reading and writing operation with xml.
Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlTextWriter write=new
XmlTextWriter(@"C:\\Users\\exam_2\\Documents\\Visual Studio
2008\\WebSites\\WebSite5\\WriteXML.xml",null);
write.WriteStartElement("Details","");
write.WriteElementString("ID","1");
write.WriteElementString("FirstName","ABC");
write.WriteElementString("LastName","XYZ");
write.WriteElementString("Salary","50000");
write.WriteEndElement();
//write.WriteEndDocument();
write.Close();
Label1.Text = "Data Write Successfully.";
}
protected void Button2_Click(object sender, EventArgs e)
{
String xmlNode = @"C:\\Users\\exam_2\\Documents\\Visual Studio
2008\\WebSites\\WebSite5\\WriteXML.xml";
XmlReader xReader = XmlReader.Create(xmlNode);
while (xReader.Read())
{
switch (xReader.NodeType)
{
case XmlNodeType.Element:
ListBox1.Items.Add("<"+xReader.Name+">");
break;
case XmlNodeType.Text:
ListBox1.Items.Add(xReader.Value);
break;
case XmlNodeType.EndElement:
ListBox1.Items.Add("<"+xReader.Name+">");
break;
}
}
}
}
o/p:-
Practical 11
namespace ClassLibrary1
{
public class Class1
{
public int add(int a, int b)
{
int c = a + b;
return c;
}
}
}
Create a new consoleApplication Program File and then add then Add the Refrences of project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary1;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ClassLibrary1.Class1 c = new Class1();
int t = c.add(1,2);
Console.WriteLine("Addition={0}",t);
Console.ReadKey();
}
}
}
Right Click on the ConsoleApplication1 file and then go to Debug and select “Start new Instance”