Anda di halaman 1dari 18

WARMUP EXCERCISES:

What is Internet,web server,browser?


What is client side scripting and serverside scripting?
What is DOM(Document Object Modelling).
.NET Framework,namespaces,page life cycle, HTML and web controls.
State management techniques.
What are View state,Cookies,Hidden,Querystring?
Request and Response objects.
Difference Session and Application state management technique.
Validation controls.
Rich controls.
What is ADO.NET. Different types of ADO.NET Objects?
Difference dataset and data reader.
Connected and Disconnected architecture.
What are Web services?
What is SOAP,WSDL,UDDI,DISCO.
Difference Server.Transer() and Response.Redirect().
1. Lab Exercises:
[Purpose this exercise is to introduce Client side scripting. Differentiate between client side
scripting and server side scripting. Steps:
1. Write a program in either NOTEPAD or VISUAL STUDIO.NET environment.
2. If VISUAL STUDIO .NET environment then
(i) Right click on Solution Explorer and Select Add New Item.
(ii) Select HTMLPage.htm.
(iii) Write the code (only use HTML Controls from Toolbox).
(iv) Select HTML page in Solution Explorer =>Right click=>Select View in Browser
to execute.
OR
Press F5 button for execution.
3. If NOTEPAD then
(i) Save the file with extension .HTML.
(ii) Click the HTML file for execution.
Exercise No1: ( 2 Hours) – 1 Practical
<HTML>
<HEAD>
<TITLE> Greetings from the web</TITLE>
<script language="javascript" type="text/javascript">
function CheckLogin() {
if ( form1.txtUname .value =="" && form1 .txtPwd.value =="")
{
alert ("Please Enter username and password")
form1.txtUname .focus ();
}else if ( form1.txtUname .value =="JNEC" && form1.txtPwd.value =="MCA")
{
alert ("Login Success")
}
else
{
alert ("Login failed please try again..")
form1 .txtUname .value="";
form1.txtPwd .value ="";
form1 .txtUname .focus ();
}
}
</script>
</Head>
<body>
<form id="form1" name="f1" action ="">
<table style="width: 34%; height: 220px;">
<tr>
<td class="style2">
Username :
</td>
<td>
<input id="txtUname" type="text" />
</td>
</tr>
<tr>
<td class="style2">
Password :
</td>
<td>
<input id="txtPwd" type="password" />
</td>
</tr>
<tr>
<td class="style1" colspan="2">
<input id="Button1" type="Button" value="Login" onclick="CheckLogin()" />
</td>
</tr>
</table>
</form>
</body>
<HTML>
2. Lab Exercises:
[Purpose of this exercise is to introduce ASP.NET page life cycle. You can use Visual Studio .NET
environment to type the program. Execution of program in two ways.
1. Press F5 button OR
2. Right click on ASP.NET page (for e.g.: Default.aspx) and Select “View Browser” option and
select any browser for execution.
Exercise No2: ( 2 Hours) – 1 Practical
Lab Assignment 2: Page Life cycle( different page events)
Program:
'Inline Code ( Default.aspx.vb)
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Init
lblInfo.Text += "Page.Init event handled.<br />"
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
lblInfo.Text += "Page.Load event handled.<br />"
If (Page.IsPostBack) Then
lblinfo.Text += "<b>This is the second time you've seen this page.</b><br />"
End If
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.PreRender
lblinfo.Text += "Page.PreRender event handled.<br />"
End Sub
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Unload
' This text never appears because the HTML is already
' rendered for the page at this point.
lblinfo.Text += "Page.Unload event handled.<br />"
End Sub
End Class
3. Lab Exercises:
[Purpose of this exercise is to introduce ASP.NET State management techniques. You can use
Visual Studio .NET environment to type the program. Execution of program in two ways.
1. Press F5 button OR
2. Right click on ASP.NET page (for eg: Default.aspx) and Select “View Browser” option and
select any browser for execution.
Exercise No3: ( 2 Hours) – 1 Practical
Lab Assignment 3: State management techniques
Program:
Steps:
1. Start a new web site with two web forms. Let Default.aspx and Success.aspx are two web
forms.
2. Design Default.aspx page with fields Employee name, address, contact no, email id and
a submit button.
3. After filling all information, it should be displayed on Success.aspx page using Query String
or Session state management technique.
4. Code using Query String as shown below:
( Default.aspx)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<!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>Registration</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
Employee Name :
</td>
<td>
<asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Address :
</td>
<td>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Phone No:
</td>
<td>
<asp:TextBox ID="txtPhNo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email Id:
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<asp:Button ID="Button1" runat="server" Text="Submit" Style="top: 157px; left: 118px;
position: absolute; height: 26px; width: 61px" />
</form>
</body>
</html>
Inline Code (Default.aspx.vb)
Partial Class _Default
Inherits System.Web.UI.Page
Public imgString As String
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Button1.Click
Response.Redirect("Success.aspx?name=" & txtEmpName.Text & "&address=" &
txtAddress.Text & "&phno=" & txtPhNo.Text & "&email=" & txtEmail.Text & "&photo=" &
Image1.ImageUrl)
End Sub
End Class
(Success.aspx)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Success.aspx.vb"
Inherits="Succcess" %>
<!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>Second Page</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
Name:
</td>
<td>
<%=Request.QueryString("name")%>
</td>
</tr>
<tr>
<td>
Address :
</td>
<td>
<%=Request.QueryString("address")%>
</td>
</tr>
<tr>
<td>
Phno:
</td>
<td>
<%=Request.QueryString("phno")%>
</td>
</tr>
<tr>
<td>
Email :
</td>
<td>
<%=Request.QueryString("email")%>
</td>
</tr>
</table>
</form>
</body>
</html>
4. Lab Exercises:
[Purpose of this exercise is to use advance web controls. You can use Visual Studio .NET
environment to type the program. Execution of program in two ways.
1. Press F5 button OR
2. Right click on ASP.NET page (for eg: Default.aspx) and Select “View Browser” option and
select any browser for execution.
Exercise No4: ( 2 Hours) – 1 Practical
Lab Assignment 4:
Program:
Steps:
1. Start a new web site with two web forms. Let Default.aspx and Default2.aspx are two
web forms.
2. Take Hyperlink, Linkbutton and Fileupload controls from toolbox.
3. Follow the code:
( Default.aspx)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<!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>Advance web controls</title>
</head>
<body>
<form id="form1" runat="server">
<table border="1">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Relative URL HyperLink"></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</td>
<td>
<asp:Label ID="Label3" runat="server" Text="Select the file to upload"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
</td>
<td>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://www.google.com"
Target="_blank" ToolTip="Visit Google.com">Visit Google.com</asp:HyperLink>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/Default2.aspx"
Target="_self">Click here to open Default2.aspx</asp:HyperLink>
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="Button1"
runat="server"
Text="Upload File" />
</td>
</tr>
</table>
</form>
</body>
</html>
( Default.aspx.vb)
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles LinkButton1.Click
Label1.Text = "PostBack operation performed on clicking link button"
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
If Not Page.IsPostBack Then
Label2.Text = "Page loaded first time."
Else
Label2.Text = "PostBack operation is completed."
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Button1.Click
If FileUpload1.HasFile Then
FileUpload1.SaveAs("C:\" & FileUpload1.FileName)
Label3.Text = "File Uploaded"
Else
Label3.Text = "No uploaded file"
End If
End Sub
End Class
5. Lab Exercises:
[Purpose of this exercise is to use Rich control i.e. AD ROTATOR Control. You can use Visual
Studio .NET environment to type the program. Execution of program in two ways.
1. Press F5 button OR
2. Right click on ASP.NET page (for e.g.: Default.aspx) and Select “View Browser” option and
select any browser for execution.
3. Refresh the page to see the different images.
Exercise No5: ( 2 Hours) – 1 Practical
Lab Assignment 5: Using Ad Rotator control
Program:
Steps:
1. Start a new web site with web forms Default.aspx and Add XMLFile.xml file by right
clicking on Solution Explorer and Select Add New Item and Select XMLFile.xml.
2. Add Ad rotator control in Default.aspx page.
3. Add some images with extension .JPG or .JPEG in application folder.
3. Write the code in Default.aspx and XMLFile.xml as shown below:
( Default.aspx)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<!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>Adrorator control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:AdRotator ID="AdRotator1" AdvertisementFile ="~/XMLFile.xml" runat="server" Target
="_blank " />
</form>
</body>
</html>
(XMLFile.xml)
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
<ImageUrl>f2f53cf6.jpg</ImageUrl>
<NavigateUrl>www.gmail.com</NavigateUrl>
<Impressions>2</Impressions>
</Ad>
<Ad>
<ImageUrl>3-idiots-13h.jpg</ImageUrl>
<NavigateUrl>www.yahoo.com</NavigateUrl>
<Impressions>2</Impressions>
</Ad>
</Advertisements>
6. Lab Exercises:
[Purpose of this exercise is to use Validation controls .You can use Visual Studio .NET
environment to type the program. Execution of program in two ways.
1. Press F5 button OR
2. Right click on ASP.NET page (for e.g.: Default.aspx) and Select “View Browser” option and
select any browser for execution.
Exercise No6: ( 2 Hours) – 1 Practical
Lab Assignment 6: Using Validation controls
Program:
Steps:
1. Design a Registration form Default.aspx page with fields Username, choose password,
confirm password, age, email id, city and a submit button.
2. Use:
a) RequiredFieldValidator - for Username, Choose password, Age, Email Id fields.
b) CompareValidator - for Confirm password field.
c) RangeValidator - for Age field.
d) RegularExpressionValidator - for Email field.
e) CustomValidator - for City field.
3. Set the properties for:
a) RequiredFieldValidator - ControlToValidate, ErrorMessage.
b) CompareValidator - ControlToCompare, ControlToValidate, ErrorMessage.
c)RangeValidator - ControlToValidate, ErrorMessage, MaximumValue, MinimumValue,
Type.
d) RegularExpressionValidator - ControlToValidate, ErrorMessage ,ValidationExpression.
e) CustomValidator - ControlToValidate, ErrorMessage, ClientValidationFunction.
4. Write a Client side script to use CustomValidator control (either JavaScript or VBScript).
5. Display message “Successfully Submitted” after Clicking Submit button i.e after successful
validation of registration page.
( Default.aspx)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<!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>Registration</title>
<script language="JavaScript" type="text/javascript">
function CityValidate(ctl, args)
{
args.IsValid=(args.Value != "Select");
}
function window_onload() {
CityValidate ();
}
</script>
</head>
<body onload="return window_onload()">
<form id="form1" runat="server">
<table>
<tr>
<td>
UserName :
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtUserName" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Choose Password :
</td>
<td>
<asp:TextBox ID="txtChoosePwd" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtChoosePwd" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Confirm Password :
</td>
<td>
<asp:TextBox ID="txtConfirmPwd" runat="server"></asp:TextBox>
</td>
<td>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="txtChoosePwd" ControlToValidate="txtConfirmPwd"
ErrorMessage="Password Mismatch"></asp:CompareValidator>
</td>
</tr>
<tr>
<td>
Age:
</td>
<td>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtAge" ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="txtAge" ErrorMessage="18-40" MaximumValue="40"
MinimumValue="18" Type="Integer"></asp:RangeValidator>
</td>
</tr>
<tr>
<td>
Email Id:
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="txtEmail" ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtEmail" ErrorMessage="Invalid email"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-
.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
City:
</td>
<td>
<asp:DropDownList ID="CityDropDown" runat="server"
CausesValidation="True">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Pune</asp:ListItem>
<asp:ListItem>Mumbai</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Please select the city" ClientValidationFunction="CityValidate"
ControlToValidate="CityDropDown"></asp:CustomValidator>
</td>
</tr>
</table>
<asp:Button ID="Button1" runat="server" Text="Submit" Style="top: 211px; left: 125px;
position: absolute; height: 26px; width: 61px" />
</form>
</body>
</html>
‘Inline code( Default.aspx.vb)
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Button1.Click
Response.Write("Submitted successfully...")
End Sub
End Class
7. Lab Exercises:
[Purpose of this exercise is to use ADO.NET technology for showing records in Grid view from
Database .You can use Visual Studio .NET environment to type the program. Execution of
program in two ways.
1. Press F5 button OR
2. Right click on ASP.NET page (for e.g.: Default.aspx) and Select “View Browser” option and
select any browser for execution.
Exercise No7: ( 2 Hours) – 1 Practical
Lab Assignment 7: ADO.NET code to show records in Gridview control
Program:
Steps:
1. Start a new website with Default.aspx web page.
2. Add Grid view control from tool box.
3. Steps for connecting to database:
a) Import the namespaces System.data and System.Data.SqlClient.
b) Establish the connection using connection object.
c) Execute the SQL Query using Sqldataadapter object.
d) Create a new dataset object.
e) Fill the dataset with table using Dataadapters Fill() method.
f) Bind the filled dataset with Grid view control.
( Default.aspx)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="Default"
%>
<!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>Grid view control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#E3EAEB" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</form>
</body>
</html>
‘Inline code( Default.aspx.vb)
Imports System.Data
Imports System.Data.SqlClient
Partial Class Default
Inherits System.Web.UI.Page
Dim con As String
Dim conn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As New DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
Try
con =
System.Configuration.ConfigurationManager.ConnectionStrings("myConnection").ConnectionStri
ng
' Taking connectionstring from Web.config file
conn = New SqlConnection(con) 'Establishing Connection with database
da = New SqlDataAdapter("Select * from Item", conn) 'Executing SQL query
da.Fill(ds, "Item") 'Filling dataset with Item table
GridView1.DataSource = ds
GridView1.DataBind() ' Binding dataset with Grid view control
Catch ex As Exception
Response.Write("Error:" & ex.Message)
End Try
End Sub
End Class
(Web.config)
<connectionStrings>
<add name="myConnection" connectionString="Data Source=JNEC02;Initial Catalog=JNEC;User
ID=sa;Password=sa"/>
</connectionStrings>
8. Lab Exercises:
[Purpose of this exercise is to use ADO.NET technology for inserting, updating and deleting
records from Database .You can use Visual Studio .NET environment to type the program.
Execution of program in two ways.
1. Press F5 button OR
2. Right click on ASP.NET page (for e.g.: Default.aspx) and Select “View Browser” option and
select any browser for execution.
Exercise No8: ( 2 Hours) – 1 Practical
Lab Assignment 8: ADO.NET code to insert,update and delete records from database
Program:
Steps:
1. Start a new website with Default.aspx web page.
2. Add textboxes for username, password, date of birth, email, gender, contact no and
Button controls for Insert, Update and Delete.
3. Steps for connecting to database:
a) Import the namespaces System.data and System.Data.SqlClient.
b) Establish the connection using connection object.
c) Open the connection.
d) Write SQL query in lCommand object.
e) Use Command methods for executing sql query.
f) Close the connection.
4. Use the validation controls for validation.
( Default.aspx)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title>Registration</title>
</head>
<body>
<form id="form1" runat="server">
<table border="1" style="width: 408px">
<tr>
<td>
UserName:
</td>
<td>
<asp:TextBox ID="txtName" runat="server" Width="269px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPwd" runat="server" Width="269px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Date of Birth(mm/dd/yyyy) :
</td>
<td>
<asp:TextBox ID="txtDOB" runat="server" Width="269px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Contact no:
</td>
<td>
<asp:TextBox ID="txtContact" runat="server" Width="269px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email Id :
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server" Width="269px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Gender :
</td>
<td>
<asp:RadioButtonList ID="RBL1" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<asp:Button ID="btnInsert" runat="server" Text="Insert" />
<asp:Button ID="btnUpdate" runat="server" Text="Update" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" />
<asp:Button ID="btnSearch" runat="server" Text="Search" />
</td>
</tr>
</table>
</form>
</body>
</html>
'Inline Code (Default.aspx.vb)
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim con As SqlConnection
Dim da As SqlDataAdapter
Dim cmd As SqlCommand
Dim dr As SqlDataReader
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
Try
con = New
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("conn").Connecti
onS
tring)
Catch ex As Exception
Response.Write("Error1:" & ex.Message)
End Try
End Sub
'Inserting Records
Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
btnInsert.Click
Dim inQuery, gender As String
Try
con.Open()
If RBL1.SelectedIndex = 0 Then
gender = "M"
Else
gender = "F"
End If
inQuery = "Insert into Registration values('" & txtName.Text & "','" & txtPwd.Text & "','"
& Convert.ToDateTime(txtDOB.Text) & "'," & Convert.ToInt64(txtContact.Text) & " , '" &
txtEmail.Text & "' , '" & gender & "')"
cmd = New SqlCommand(inQuery, con)
cmd.ExecuteNonQuery()
Response.Write("Saved Successfully...")
con.Close()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
'Deleting the record
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
btnDelete.Click
Dim deleteQuery As String
Try
con.Open()
deleteQuery = "Delete from Registration where userName='" & txtName.Text & "'"
cmd = New SqlCommand(deleteQuery, con)
cmd.ExecuteNonQuery()
Response.Write("Record deleted...")
con.Close()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
'Updating the records
Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles btnUpdate.Click
Dim updateQuery, gender As String
Try
con.Open()
If RBL1.SelectedIndex = 0 Then
gender = "M"
Else
gender = "F"
End If
updateQuery = "Update Registration set password='" & txtPwd.Text & "',dateofbirth='" &
Convert.ToDateTime(txtDOB.Text) & "',contactno=" & Convert.ToInt64(txtContact.Text) &
",email='" & txtEmail.Text & "',gender='" & gender & "'where userName='" & txtName.Text & "'"
cmd = New SqlCommand(updateQuery, con)
cmd.ExecuteNonQuery()
Response.Write("Updated Successfully...")
con.Close()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
'Searching the record
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles btnSearch.Click
Dim selectQuery As String
Try
con.Open()
selectQuery = "Select * from Registration where userName='" & txtName.Text & "'"
cmd = New SqlCommand(selectQuery, con)
dr = cmd.ExecuteReader
While dr.Read
txtPwd.Text = dr(1)
txtDOB.Text = dr(2).ToString
txtContact.Text = dr(3).ToString
txtEmail.Text = dr(4)
If (dr(5) = "M") Then
RBL1.SelectedIndex = 0
Else
RBL1.SelectedIndex = 1
End If
End While
con.Close()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
End Class
9. Lab Exercises:
[Purpose of this exercise is to use data list and repeater controls .You can use Visual Studio .NET
environment to type the program. Execution of program in two ways.
1. Press F5 button OR
2. Right click on ASP.NET page (for e.g.: Default.aspx) and Select “View Browser” option and
select any browser for execution.
Exercise No9: ( 2 Hours) – 1 Practical
Lab Assignment 9: Using Data List and repeater controls
Program:
Steps:
1. Start a new website with Default.aspx web page.
2. Add data list and repeater controls.
3. Add XML file (Player.xml) and use it as data source.
4. If Not Page.IsPostBack Then
a) Take new data set objects.
b) Use ReadXml method of data set object to map XML file.
c) Bind the new dataset objects with both DataList and Repeater control.
End if
( Default.aspx)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<!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>DataList and Repeater control</title>
</head>
<body>
<form id="form1" runat="server">
Data List control :
<asp:DataList ID="Player" runat="server">
<HeaderTemplate>
Team Ranking</HeaderTemplate>
<ItemTemplate>
<%#Container.DataItem("Name")%>
= ><%#Container.DataItem("Rank")%>
</ItemTemplate>
<FooterTemplate>
Copywrite 2010</FooterTemplate>
</asp:DataList>
<br />
Repeater control:
<asp:Repeater ID="Game" runat="server">
<HeaderTemplate>
<table border="1" width="40%">
<tr>
<th>
Name
</th>
<th>
Rank
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Container.DataItem("Name")%>
</td>
<td>
<%#Container.DataItem("Rank")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
‘Inline code( Default.aspx.vb)
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
If Not Page.IsPostBack Then
Dim info = New DataSet
info.ReadXml(MapPath("Player.xml"))
Player.DataSource = info
Player.DataBind()
End If
If Not Page.IsPostBack Then
Dim info1 = New DataSet
info1.ReadXml(MapPath("Player.xml"))
Game.DataSource = info1
Game.DataBind()
End If
End Sub
End Class
( Player.xml)
<?xml version="1.0" encoding="utf-8" ?>
<Cricket>
<Team>
<Name>India</Name>
<Rank>1</Rank>
</Team >
<Team>
<Name>Australia</Name>
<Rank>2</Rank>
</Team >
</Cricket>
Exercise No10: ( 2 Hours) – 1 Practical
Lab Assignment 10: Web Services Demo.
Program:
Steps:
1. Start a new website with Default.aspx web page.
2. Right click in Solution explorer => Add New item => Select Web Service.
a) Give the name myWebService to the web service.
b) (i) One file gets created in App_Code folder with extension .vb.
(ii) Second gets created in application folder with extension .asmx.
3. Steps for creating web services:
a) Write a user defined methods in myWebService.vb.
For e.g.:
<WebMethod()> _
Public Function myFunction() As String
Return "This myFunction in Webservice"
End Function
b) (i) Right click in solution explorer => Add Web Reference=> Select browser to
option as “Web Services in this solution”.It will display the number of web services in
the solution.
(ii) Select the web service you want to use.
(iii) It will display all the methods.
(iv) Select the method you want to use and Click “Invoke” button to see the xml
message returned by the method.
c) Give the web reference name (by default “localhost”) and click Add Reference
button.
d) Web reference with name local host gets created in App_WebReferences folder.
e) Check if files with extensions .disco and .wsdl gets generated in localhost.
4. In code file (Default.aspx.vb):
a. Import the namespace System.Web.Services.
b. Create an object of web service.(i.e myWebService)
c. Use the methods of respective web service.
5. Click the “Build” menu in application => select “Build web site”.
If “Build succeeded” message comes in status bar.
Execute the application and view the results
Else
Find out the errors and solve the errors.
End if
‘Inline code( Default.aspx.vb)
Imports System.Web.Services
Partial Class _Default
Inherits System.Web.UI.Page
Dim mt As New myWebService 'Creating an object webservice
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
Response.Write(mt.myFunction()) 'Using the method of respective web service
End Sub
End Class
‘Webservice( myWebSerivce.vb)
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class myWebService
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
<WebMethod()> _
Public Function myFunction() As String
Return "This is myFunction in Webservice"
End Function
End Class

Anda mungkin juga menyukai