lab manual asp.net

29
. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR Department of Computer Science and engineering LAB MANUAL For SOFTWARE TECHNOLOGY LAB - 5 CSE/8 th /ST-5 Lab/Prepared by Vivek Kumar Sinha

Upload: vivek-kumar-sinha

Post on 16-Apr-2017

48 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Department of Computer Science and engineering

LAB MANUAL

For

SOFTWARE TECHNOLOGY LAB - 5

Experiment 1

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 2: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Aim: Write a program in ASP.net using drop down list.

Source:

< %@ Page Language= "C#" % >

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< script runat = "server" >

protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)

{

Label1.Text = "You selected: <br /> ItemText: " + DropDownList1.SelectedItem.Text.ToString () + " < br /> Value: " + DropDownList1.SelectedValue.ToString () + " < br / > Index: " +

DropDownList1.SelectedIndex.ToString ();

}

</ script>

< html xmlns = "http://www.w3.org/1999/xhtml" >

< head runat = "server" >

< title >DropDownList Example: AutoPostBack, Index, Item, Value </title >

</ head>

< body>

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

< div >

< asp:Label ID= "Label1" runat = "server" Font- Size= "Large" ForeColor ="Olive" />

< br />

< asp:Label ID= "Label2" runat = "server" Text= "Favorite Control?" AssociatedControlID = " DropDownList1" >

</ asp:Label >

< asp:DropDownList ID= "DropDownList1" runat = "server" OnSelectedIndexChanged= "

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 3: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

DropDownList1_SelectedIndexChanged" AutoPostBack= "true" BackColor= "BurlyWood" >

< asp:ListItem Value= "Navigation ToolBox Control" > TreeView</asp:ListItem >

< asp:ListItem Value= "Standard ToolBox Control" > AdRotator </asp:ListItem >

< asp:ListItem Value= "Data ToolBox Control" > XmlDataSource </asp:ListItem >

< asp:ListItem Value= "Login ToolBox Control" > LoginView</asp:ListItem >

</ asp:DropDownList >

</ div>

</ form>

</ body>

</ html>

Output:

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 4: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Experiment 2

Aim: Write a program in ASP.net using Listbox.

Source:

<head runat="server">

<title> AdvanceListBox</title>

</head>

<body >

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

<div style="background-color:#CCFFCC; width:300px" >

<b style="background-color:Silver;" >ListBox Advance Functionality</b>

<br />

<br />

<asp:ListBox ID="lstBrowser" runat="server"Width="200px" SelectionMode="Multiple" AutoPostBack="True” onselectedindexchanged="lstBrowser_SelectedIndexChanged">

</asp:ListBox>

<br />

<br />

<asp:Label ID="lblFilter" runat="server" Font- Bold="True" Text="Filter: "></asp:Label>

<asp:TextBox ID="txtFilter" runat="server">

</asp:TextBox>

<br />

<br />

<asp:Label ID="lbl" runat="server" Font-Bold="True" Text="Output:"></asp:Label>

<asp:Label ID="lblOutput" runat="server">

</asp:Label>

<br />

<br />

</div>

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 5: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

<asp:Button ID="btnDelete" runat="server"OnClick="btnDelete_Click" Text="Multiple Delete" />

<asp:Button ID="btnSort" runat="server" OnClick="btnSort_Click" Text="Sort" />

<asp:Button ID="btnSelectAll" runat="server" OnClick="btnSelectAll_Click"

Text="Select All" />

<br />

<asp:Button ID="btnSelectNone" runat="server” OnClick="btnSelectNone_Click" Text="Select None" />

<asp:Button ID="btnFindIem" runat="server” OnClick="btnFindIem_Click" Text="Find Item" />

</form>

</body>

Output:

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 6: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Experiment 3

Aim: Write a program in ASP.net using events in textbox.

Source:

<% @Page Language="C#" %>

<script runat ="server" >

private void convertoupper ( object sender ,EventArgs e )

{

string str = mytext . Value;

changed_text . InnerHtml = str . ToUpper ();

}

</script>

<!-- Layout -->

<html>

<head> <title> Change to Upper Case </title> </head>

<body>

<h3> Conversion to Upper Case </h3>

<form runat ="server" >

<input runat = "server" id="mytext" type ="text" />

<input runat = "server" id="button1" type ="submit" value="Enter..."

OnServerClick =" convertoupper " />

<hr />

<h3> Results: </h3>

<span runat ="server" id= "changed_text" />

</form>

</body>

</html>

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 7: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Output:

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 8: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Experiment 4

Aim: Write a program in ASP.net using Radio button.

Source:

<@ Page Language= "C#" % >

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1-transitional.dtd" >

< script runat = "server" >

protected void RadioButton_CheckedChanged(object sender,System.EventArgs e)

{

if ( RadioButton1.Checked == true)

{

Response.Write("You Selected: Asp.Net");

}

else

{

Response.Write("You Selected: ColdFusion");

}

}

</ script>

< html xmlns = "http://www.w3.org/1999/xhtml" >

< head runat= "server" >

< title > RadioButton example: how to use RadioButton control in asp.net </title >

</ head>

< body>

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

< div >

<asp:RadioButton ID="RadioButton1" runat = "server" Text= "Asp.Net" GroupName = "

Software" AutoPostBack= "true" OnCheckedChanged="RadioButton_ CheckedChanged" />

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 9: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

<asp:RadioButton ID="RadioButton2" runat = "server" Text= "ColdFusion"

GroupName ="Software" AutoPostBack= "true" OnCheckedChanged= "RadioButton_

CheckedChanged" />

</ div >

</ form>

</ body>

</ html>

Output:

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 10: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Experiment 5

Aim: Write a program in ASP.Net for validation in textbox.

Source:

<asp:RegularExpressionValidatoR>

Maximum character length Validation (Maximum 8 characters allowed)

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox1" ID="RegularExpressionValidator1" ValidationExpression = "^[\s\S]{0,8}$" runat="server" ErrorMessage="Maximum 8 characters allowed."></asp:RegularExpressionValidator>

Minimum character length Validation (Minimum 8 characters required)

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox2" ID="RegularExpressionValidator2" ValidationExpression = "^[\s\S]{8,}$" runat="server" ErrorMessage="Minimum 8 characters required."></asp:RegularExpressionValidator>

Minimum and Maximum character length Validation

(Minimum 5 and Maximum 8 characters required)

<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox3" ID="RegularExpressionValidator3" ValidationExpression = "^[\s\S]{5,8}$" runat="server" ErrorMessage="Minimum 5 and Maximum 8 characters required."></asp:RegularExpressionValidatoR>

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 11: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Output:

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 12: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Experiment 6

Aim: Write a program in ASP.Net using Data List controls.

Source:

<asp:DataList ID="DataList1" runat="server">

<HeaderTemplate>

<table border="1">

<tr>

<th>Employee Code</th>

<th>Employee Name</th>

<th>Basic</th>

<th>Dept Code</th>

</tr>

</HeaderTemplate>

<ItemTemplate> <tr bgcolor="#0xbbbb">

<td> <%# DataBinder.Eval(Container.DataItem,"EmpCode")%> </td>

<td> <%# DataBinder.Eval(Container.DataItem, "EmpName")%> </td>

<td> <%# DataBinder.Eval(Container.DataItem, "Basic")%> </td>

<td> <%# DataBinder.Eval(Container.DataItem, "DeptCode")%> </td>

</tr>

</ItemTemplate>

<FooterTemplate>

</FooterTemplate>

</asp:DataList>

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 13: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Output:

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 14: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Experiment 7-10

Aim: Write a program in ASP.net for Insertion, Deletion, Updation and searching using ADO.net.

Source:

ADO.NET Entity Framework is an powerful tools to developed applications. This tutorial will help you to insert, update, delete, select, search data into SQL Server by using ADO.NET Entity Framework. For this we will use two tables Students and Countries. We will save Student ID, Name, Address, Phone and Country of a Student in Students table. In Countries table there will be some predefined country data. We will show the entire country list in a DropDownList and student

list in a GridView. Summary of the articles are:

UI Design Database Design

Creation of Entity Data Model

Select Operation Insert Operation Update Operation Delete Operation Search Operation

UI Design

Create a new asp.net, C# project. Design your UI. You can use the following asp.net code.

<table>

<tr> <td style=”width: 120px”> Student ID </td>

<td> <asp:TextBox ID=”txtID” runat=”server” Enabled=”False”></asp:TextBox> </td>

</tr>

<tr> <td style=”width: 120px”> Student Name</td>

<td> <asp:TextBox ID=”txtName” runat=”server” Width=”250px”></asp:TextBox> </td>

</tr>

<tr>

<td> Address </td>

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 15: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

<td> <asp:TextBox ID=”txtAddress” runat=”server” TextMode=”MultiLine” Width=”250px”>

</asp:TextBox>

</td>

</tr>

<tr>

<td> Phone </td>

<td> <asp:TextBox ID=”txtPhone” runat=”server”> </asp:TextBox> </td>

</tr>

<tr>

<td> Country </td>

<td> <asp:DropDownList ID=”ddlCountry” runat=”server” Width=”200px” DataSourceID=”EntityDataSource1″ DataTextField=”country_name” DataValueField=”country_id”>

</asp:DropDownList>

</td>

</tr>

<tr>

<td> &nbsp;</td>

<td> <asp:Button ID=”btnAdd” runat=”server”Text=”Add” Width=”80px” onclick=”btnAdd_Click” />

<asp:Button ID=”btnDelete” runat=”server” onclick=”btnDelete_Click” Text=”Delete”

Width=”80px” />

<asp:Button ID=”btnEdit” runat=”server” onclick=”btnEdit_Click” Text=”Edit” Width=”80px” />

<asp:Button ID=”btnClear” runat=”server” onclick=”btnClear_Click” Text=”Clear”

Width=”80px” />

</td>

</tr>

<tr>

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 16: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

<td> &nbsp;</td>

<td> &nbsp;</td>

</tr>

<tr>

<td> &nbsp; </td>

<td> <asp:TextBox ID=”txtSearch” runat=”server”>

</asp:TextBox>

<asp:Button ID=”btnSearch” runat=”server” onclick=”btnSearch_Click” Text=”Search” Width=”80px” />

</td>

</tr>

</table>

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”

Width=”600px” AutoGenerateSelectButton=”True” onselectedindexchanged=”GridView1_

SelectedIndexChanged”>

<Columns>

<asp:BoundField DataField=”student_id” HeaderText=”ID” />

<asp:BoundField DataField=”student_name” HeaderText=”Name” />

<asp:BoundField DataField=”address” HeaderText=”Address” />

<asp:BoundField DataField=”phone” HeaderText=”Phone” />

<asp:BoundField DataField=”country_id” HeaderText=”Country ID” />

<asp:BoundField DataField=”country_name” HeaderText=”Country Name” />

</Columns>

</asp:GridView>

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 17: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Database Design:

Create two table Students and Countries in your database. Sample scripts for both tables are given below:

CREATE TABLE [dbo].[Students](

[StudentID] [bigint] IDENTITY(1,1) NOT NULL,

[StudentName] [nvarchar](100) NULL,

[Address] [nvarchar](150) NULL,

[Phone] [nvarchar](50) NULL,

[CountryID] [nchar](2) NOT NULL,

CONSTRAINT [PK_student] PRIMARY KEY

CLUSTERED

(

[StudentID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_

NORECOMPUTE = OFF, IGNORE_DUP_KEY =

OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_

LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

CREATE TABLE [dbo].[Countries](

[CountryID] [nchar](2) NOT NULL,

[CountryName] [nvarchar](150) NOT NULL,

[Nationality] [nvarchar](100) NULL,

CONSTRAINT [PK_country] PRIMARY KEY

CLUSTERED

(

[CountryID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_

NORECOMPUTE = OFF, IGNORE_DUP_KEY =

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 18: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_

LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

Select Operation

Make an object of Data Model in .cs page like this

TestDBEntities oTestDBEntities = new TestDBEntities();

Write the following code in page load event.

if (!IsPostBack)

{

Load_Country();

}

To select data form country table write the following code.

public List<Country> LoadCountry()

{

var result = from Con in oTestDBEntities.Countriesselect Con;

return result.ToList();

}

To bind the country DropDownList writes the following code:

public void Load_Country()

{

ddlCountry.DataSource = LoadCountry();

ddlCountry.DataTextField = “CountryName”;

ddlCountry.DataValueField = “CountryID”;

ddlCountry.DataBind();

}

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 19: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

To select data form students table write the following code.

public IList<object> LoadStudent()

{

var result = from std in oTestDBEntities.Students join Con in oTestDBEntities.Countries on

std.CountryID equals Con.CountryID select new { std.StudentID, std.StudentName,

std.Address, std.Phone, std.CountryID, Con.CountryName };

IList<Object> items = new List<Object>();

foreach (var per in result)

{

items.Add(per);

}

return items;

}

To bind the student GridView writes the following code:

public void Load_Student()

{

GridView1.DataSource = LoadStudent();

GridView1.DataBind();

}

Call Load_Student() function in page load event.

Write the following code to show data of selected students in UI.

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

{

GridViewRow row = GridView1.SelectedRow;

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 20: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

txtID.Text = row.Cells[1].Text;

txtName.Text = row.Cells[2].Text;

txtAddress.Text = row.Cells[3].Text;

txtPhone.Text = row.Cells[4].Text;

ddlCountry.Text = row.Cells[5].Text;

}

Insert Operation

To insert or save data in to database write the following code.

oStudent.StudentName = txtName.Text;

oStudent.Phone = txtPhone.Text;

oStudent.Address = txtAddress.Text;

oStudent.CountryID =

ddlCountry.SelectedValue.ToString();

InsertData(oStudent);

Load_Student();

public void InsertData(Student oStudent)

{

oTestDBEntities.Students.AddObject(oStudent);

oTestDBEntities.SaveChanges();

}

Update Operation

To update the record of a student write the following code.

oStudent.StudentID = Convert.ToInt16(txtID.Text) ;

oStudent.StudentName = txtName.Text;

oStudent.Phone = txtPhone.Text;

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 21: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

oStudent.Address = txtAddress.Text;

oStudent.CountryID = ddlCountry.SelectedValue.ToString();

UpdateData(oStudent);

Load_Student();

public void UpdateData(Student oStudent)

{

var result = (from st in oTestDBEntities.Students

where st.StudentID == oStudent.StudentID

select st).Single();

result.StudentName = oStudent.StudentName;

result.Address = oStudent.Address;

result.Phone = oStudent.Phone;

result.CountryID = oStudent.CountryID;

oTestDBEntities.Students.ApplyCurrentValues

(result);

oTestDBEntities.SaveChanges();

}

Delete Operation:

To delete data from the database write the following code.

oStudent.StudentID = Convert.ToInt16(txtID.Text) ;

DeleteData(oStudent);

Load_Student();

public void DeleteData(Student oStudent)

{

var result = (from st in oTestDBEntities.Students

where st.StudentID == oStudent.StudentID

select st).Single();

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 22: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

oTestDBEntities.Students.DeleteObject(result);

oTestDBEntities.SaveChanges();

}

Search Operation

oStudent.StudentID = Convert.ToInt 16(txtSearch.Text);

GridView1.DataSource = SearchStudent (oStudent);

GridView1.DataBind();

public IList<object> SearchStudent(Student oStudent)

{

IList<Object> items = new List<Object>();

var result = from std in oTestDBEntities.Students join Con in oTestDBEntities.Countries on

std.CountryID equals Con.CountryID where std.StudentID == oStudent.StudentID select new { std.StudentID, std.StudentName, std.Address, std.Phone, std.CountryID, Con.CountryName };

foreach (var per in result)

{

items.Add(per);

}

return items;

ON

)

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha

Page 23: Lab manual asp.net

. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR

Output:

CSE/8th/ST-5 Lab/Prepared by Vivek Kumar Sinha