0% found this document useful (0 votes)
7 views11 pages

PR 8 To 10

dot net

Uploaded by

demo162000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

PR 8 To 10

dot net

Uploaded by

demo162000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Practical:-8

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

string connString= @"Data Source =.\SQLExpress;Integrated


Security=True;AttachDbFileName=|DataDirectory|MyDataBase.mdf;User
Instance=True";
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter da = new SqlDataAdapter("select * from
student_table", conn);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}

2
public SqlDataReader selectAllNameStudent()
{
SqlCommand comm = new SqlCommand("select Stu_Name from
Student_Table", conn);
SqlDataReader datareader;
datareader = comm.ExecuteReader();
return datareader;
}
}

Code under XML Button

protected void Button1_Click(object sender, EventArgs e)


{
GridView1.DataSourceID = XmlDataSource1.ID;

Code under Data Set

protected void Button3_Click(object sender, EventArgs e)


{
GridView1.DataSourceID = ObjectDataSource1.ID;
GridView1.DataBind();
}

Code under Use Data Reader

protected void Button2_Click(object sender, EventArgs e)


{
GridView1.DataSourceID ="";
MyClass myClassObj = new MyClass();
SqlDataReader dr;
dr = myClassObj.selectAllNameStudent();
DataTable table = new DataTable();
table.Columns.Add("Stu_Name");

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

<?xml version="1.0" standalone="yes"?>


<bookstore>
<book ISBN="10-000000-001"
title="The Iliad and The Odyssey"
price="12.95">
<comments>

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

Read using data reader and data source

Using data set.

5
Practical:-10

Use Data Controls like Data List, Grid View, Detail View, Repeater and List Bound Control

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"


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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

Grid View<br />


<br />

<asp:GridView ID="GridView1" runat="server"


AutoGenerateColumns="False"
DataKeyNames="Student_ID" DataSourceID="SqlDataSource1">
<Columns>
<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" />
</Columns>
</asp:GridView>
<hr />
Data List<br />
&nbsp;<asp:DataList ID="DataList1" runat="server" DataKeyField="Student_ID"
DataSourceID="SqlDataSource1">
<ItemTemplate>
Student_ID:
<asp:Label ID="Student_IDLabel" runat="server"
Text='<%# Eval("Student_ID") %>' />
<br />
Stu_Name:

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>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"


ConnectionString="<%$
ConnectionStrings:MyDataBaseConnectionString %>"
SelectCommand="SELECT * FROM
[Student_Table]"></asp:SqlDataSource>
<br />

<br />

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

MyDataBase.mdf

Table Data

Table Definition

8
Output:-

GridView

Detail View

List Bound Control

9
Practical:-9

Implement web application using ASP.NET with web control.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"


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>Untitled Page</title>
<style type="text/css">
.style1
{}
.style2
{
width: 21px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table bgcolor="#FFFFCC" style="width:100%;">


<tr>
<td class="style1">
Name</td>
<td class="style2">
&nbsp;</td>
<td>
<asp:TextBox ID="NameTextBox"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
ID</td>
<td class="style2">
&nbsp;</td>
<td>
<asp:TextBox ID="IdTextBox"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
&nbsp;</td>
<td class="style2">
&nbsp;</td>
<td>
<asp:Button ID="DetailButton" runat="server"
onclick="DetailButton_Click"
Text="Details" />
</td>
</tr>

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy