Sunteți pe pagina 1din 9

T.Y.B.SC(I.T.

)-ADVANCED WEB PROGRAMMING-PAPER(III)


IT-7239
2018-2019

Practical 8
Working with data controls
A) Create a web application to demonstrate various uses and properties of
SqlDataSource.
Program-
8a.aspx-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="8a.aspx.cs" Inherits="_8a" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:Label ID="Label1" runat="server" Text="ID-"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Name-"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Insert" />
&nbsp;
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Delete" />
<br />
<br />
<asp:Label ID="Label3" runat="server"></asp:Label>
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:studentdetailsConnectionString %>"
SelectCommand="SELECT * FROM [stud_dtls]"></asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Page 1|9
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

Design-

8a.aspx.cs-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _8a : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=VICKY-PC;Initial
Catalog=studentdetails;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into stud_dtls values('" + TextBox1.Text +
"','" + TextBox2.Text + "')", cn);
cn.Open();
cmd.ExecuteNonQuery();
Label3.Text = "Data inserted successfully";
}
protected void Button2_Click(object sender, EventArgs e)
{

Page 2|9
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

SqlConnection cn = new SqlConnection("Data Source=VICKY-PC;Initial


Catalog=studentdetails;Integrated Security=True");
SqlCommand cmd = new SqlCommand("delete from stud_dtls where id=" + TextBox1.Text
+ "", cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
Label3.Text = "Data deleted successfully";
}
}

Output-

After inserting data-

Database-

After deleting-

Page 3|9
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

Database-

B) Create a web application to demonstrate data binding using DetailsView


and FormView Control.
Program-

8b.aspx-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="8b.aspx.cs" Inherits="_8b" %>
<!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></title>
</head>
<body>
Page 4|9
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

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


<div align="center">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:studentdetailsConnectionString %>"
SelectCommand="SELECT * FROM [stud_dtls]"></asp:SqlDataSource>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"
DataSourceID="SqlDataSource1" Height="50px" Width="125px">
</asp:DetailsView>
<br />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:studentdetailsConnectionString %>"
SelectCommand="SELECT * FROM [stud_dtls]"></asp:SqlDataSource>
<br />
<asp:FormView ID="FormView1" runat="server" AllowPaging="True"
DataSourceID="SqlDataSource2">
<EditItemTemplate>
id:
<asp:TextBox ID="idTextBox" runat="server" Text='<%# Bind("id") %>' />
<br />
name:
<asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
&nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
id:
<asp:TextBox ID="idTextBox" runat="server" Text='<%# Bind("id") %>' />
<br />
name:
<asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
&nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
id:
<asp:Label ID="idLabel" runat="server" Text='<%# Bind("id") %>' />
<br />
name:
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>' />
<br />
</ItemTemplate>
</asp:FormView>
Page 5|9
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

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

Design-

8b.aspx.cs-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _8b : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
}
Output-

Page 6|9
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

C) Create a web application to display Using Disconnected Data Access


and Databinding using GridView.
Program-
8c.aspx-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="8c.aspx.cs" Inherits="_8c" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
Page 7|9
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

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


AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
</Columns>
</asp:GridView>
<br />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetData" TypeName="DataSet1TableAdapters.stud_dtlsTableAdapter">
<InsertParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="name" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>

Design-

8c.aspx.cs-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _8c : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
Page 8|9
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

}
DataSet1.xsd-

Output-

Page 9|9

S-ar putea să vă placă și