PR 8 To 10
PR 8 To 10
Use Dataset, Data Reader, XML Reader & Data Sources (SQL, Object & XML) with Any
Windows or Web Application.
MyDataBase.mdf
Schema
Table Data
1
MyClass.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
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.Linq;
using System.Data.Sql;
using System.Data.SqlClient;
/// <summary>
/// Summary description for MyClass
/// </summary>
public class MyClass
{
public string connString = "Data
Source=.\\SqlExpress;AttachDbFileName=|DataDirectory|MyDataBase.mdf;Integra
ted Security=True;User Instance=True";
public SqlConnection conn;
public MyClass()
{
conn = new SqlConnection(connString);
conn.Open();
}
public DataTable selectAllStudent()
{
2
public SqlDataReader selectAllNameStudent()
{
SqlCommand comm = new SqlCommand("select Stu_Name from
Student_Table", conn);
SqlDataReader datareader;
datareader = comm.ExecuteReader();
return datareader;
}
}
while (dr.Read())
{
DataRow drow;
drow = table.NewRow();
drow["Stu_Name"] = dr["Stu_Name"].ToString();
table.Rows.Add(drow);
} // while dr.read() ends
myClassObj.conn.Close();
GridView1.DataSource = table;
GridView1.DataBind();
}
BookStore.xml
3
<userComment rating="4"
comment="Best translation I've read." />
<userComment rating="2"
comment="I like other versions better." />
</comments>
</book>
<book ISBN="10-000000-999"
title="Anthology of World Literature"
price="24.95">
<comments>
<userComment rating="3"
comment="Needs more modern literature." />
<userComment rating="4"
comment="Excellent overview of world literature." />
</comments>
</book>
<book ISBN="11-000000-002"
title="Computer Dictionary"
price="24.95" >
<comments>
<userComment rating="3"
comment="A valuable resource." />
</comments>
</book>
<book ISBN="11-000000-003"
title="Cooking on a Budget"
price="23.95" >
<comments>
<userComment rating="4"
comment="Delicious!" />
</comments>
</book>
<book ISBN="11-000000-004"
title="Great Works of Art"
price="29.95" >
</book>
</bookstore>
4
Read xml file using xml data source
5
Practical:-10
Use Data Controls like Data List, Grid View, Detail View, Repeater and List Bound Control
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>
6
<asp:Label ID="Stu_NameLabel" runat="server" Text='<%#
Eval("Stu_Name") %>' />
<br />
Stu_emailID:
<asp:Label ID="Stu_emailIDLabel" runat="server"
Text='<%# Eval("Stu_emailID") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<br />
<hr />
Detail View
<br />
<br />
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px"
Width="125px"
AutoGenerateRows="False" DataKeyNames="Student_ID"
DataSourceID="SqlDataSource1">
<Fields>
<asp:BoundField DataField="Student_ID"
HeaderText="Student_ID"
InsertVisible="False" ReadOnly="True"
SortExpression="Student_ID" />
<asp:BoundField DataField="Stu_Name" HeaderText="Stu_Name"
SortExpression="Stu_Name" />
<asp:BoundField DataField="Stu_emailID"
HeaderText="Stu_emailID"
SortExpression="Stu_emailID" />
</Fields>
</asp:DetailsView>
<hr />
Repeater<br />
<asp:Repeater ID="Repeater1" runat="server"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label ID="RepLabel" runat="server" Text='<%#
Eval("Student_ID")%>'></asp:Label>
<br />
<asp:Label ID="Replabel2" runat="server" Text='<%#
Eval("Stu_Name")%>'></asp:Label>
<br />
<asp:Label ID="Label1" runat="server" Text='<%#
Eval("Stu_EmailID")%>'></asp:Label>
<br />
</ItemTemplate>
</asp:Repeater>
<hr />
List Bound Control
<asp:ListView ID="ListView1" runat="server"
DataSourceID="SqlDataSource1">
<LayoutTemplate>
<asp:Placeholder id="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemSeparatorTemplate>
<hr />
</ItemSeparatorTemplate>
<ItemTemplate>
7
<asp:Label ID="RepLabel" runat="server" Text='<%#
Eval("Student_ID")%>'></asp:Label>
<br />
<asp:Label ID="Replabel2" runat="server" Text='<%#
Eval("Stu_Name")%>'></asp:Label>
<br />
<asp:Label ID="Label1" runat="server" Text='<%#
Eval("Stu_EmailID")%>'></asp:Label>
<br />
</ItemTemplate>
</asp:ListView>
<br />
</div>
</form>
</body>
</html>
MyDataBase.mdf
Table Data
Table Definition
8
Output:-
GridView
Detail View
9
Practical:-9
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{}
.style2
{
width: 21px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
10
<tr>
<td class="style1" colspan="3">
<asp:Label ID="ResultLabel" runat="server"
BorderColor="Red"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Output :-
WebBrowser
11