Anda di halaman 1dari 37

Standard Controls

using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.UI;
System.Web.UI.WebControls;

public partial class Date_Time : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = (Convert.ToInt32(TextBox1.Text) +
Convert.ToInt32(TextBox2.Text)).ToString();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Date-Time.aspx.cs"
Inherits="Date_Time" %>
<!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>
&nbsp;
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Show Current date and Time" Width="174px" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Add" />
&nbsp;&nbsp;&nbsp;

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

System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.UI;
System.Web.UI.WebControls;

public partial class STDControls : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Int32 i;
for (i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected == true)
TextBox1.Text += CheckBoxList1.Items[i].Text;
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked == true)
TextBox1.Font.Bold = true;
else
TextBox1.Font.Bold = false;
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked == true)
TextBox1.Font.Italic = true;
else
TextBox1.Font.Italic = false;
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="STDControls.aspx.cs"
Inherits="STDControls" %>
<!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 style="height: 240px">
<form id="form1" runat="server">
<div>

<asp:CheckBox ID="CheckBox1" runat="server"


oncheckedchanged="CheckBox1_CheckedChanged" Text="Bold" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:CheckBox ID="CheckBox2" runat="server"
oncheckedchanged="CheckBox2_CheckedChanged" Text="Italic" />
&nbsp;&nbsp;
<br />
&nbsp;<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
Height="43px" RepeatDirection="Horizontal" Width="674px">
<asp:ListItem>ASP.NET</asp:ListItem>
<asp:ListItem>C#</asp:ListItem>
<asp:ListItem>VB.NET</asp:ListItem>
<asp:ListItem>Oracle 10 G</asp:ListItem>
<asp:ListItem>C++</asp:ListItem>
<asp:ListItem>PHP</asp:ListItem>
<asp:ListItem>Java</asp:ListItem>
</asp:CheckBoxList>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>

CSS
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CSS.aspx.cs"
Inherits="CSS" %>
<!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">
<style type="text/css">
div
{
padding:10px;
}
.labelstyle
{
color:Red;
background-color:Yellow;
border:solid 2px red;
}
</style>
<title></title>
</head>

<body>
<form id="form1" runat="server">
<p>
<asp:Label ID="Label1" runat="server" BackColor="#FFFF66"
BorderColor="#FF3300"
BorderStyle="Solid" BorderWidth="2px" ForeColor="#FF3300" Text="First
label"></asp:Label>
</p>
<p>
<asp:Label ID="Label2" runat="server" CssClass="labelstyle" Text="Second
label"></asp:Label>
<br />
</p>
<div>
</div>
</form>
</body>
</html>
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.UI;
System.Web.UI.WebControls;

public partial class AddItems : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
Int32 i;
for (i = 0; i < ListBox1.Items.Count; i++)
{
ListBox2.Items.Add(ListBox1.Items[i].Text);
}
ListBox1.Items.Clear();
}
protected void Button3_Click(object sender, EventArgs e)
{
Int16 i;
for (i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected == true)
{
ListBox2.Items.Add(ListBox1.Items[i].Text);
ListBox1.Items.RemoveAt(i);
}
}
}
}

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


Inherits="AddItems" %>
<!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>
<br />
<table style="width:100%;">
<tr>
<td>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"
Height="173px"
Width="152px">
<asp:ListItem>Aanchal Jairath</asp:ListItem>
<asp:ListItem>Aanchal Sekhri</asp:ListItem>
<asp:ListItem>Anish Koul</asp:ListItem>
<asp:ListItem>Kanika Phutela</asp:ListItem>
<asp:ListItem>Puneet Sharma</asp:ListItem>
<asp:ListItem>Pooja Dhaka</asp:ListItem>
<asp:ListItem>Payal</asp:ListItem>
<asp:ListItem>Gourav Sharma</asp:ListItem>
</asp:ListBox>
</td>
<td>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="&gt;&gt;shift All Items" />
<br />
<br />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click"
Text="&gt;&gt; shift Selected Items" Width="140px" />
</td>
<td>
<asp:ListBox ID="ListBox2" runat="server" Height="167px"
Width="146px">
</asp:ListBox>
</td>
</tr>
</table>
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;
</div>
</form>
</body>
</html>

Calculator

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page


{
static float a,c,d;
static char b;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text =="*") ||
(TextBox1.Text == "/"))
{
TextBox1.Text = "";
TextBox1.Text = TextBox1.Text + Button1.Text;
}
else
{
TextBox1.Text = TextBox1.Text + Button1.Text;
}

}
protected void Button2_Click(object sender, EventArgs e)
{
if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text =="*") ||
(TextBox1.Text == "/"))
{
TextBox1.Text = "";
TextBox1.Text = TextBox1.Text + Button2.Text;
}
else
{
TextBox1.Text = TextBox1.Text + Button2.Text;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text =="*") ||
(TextBox1.Text == "/"))
{
TextBox1.Text = "";
TextBox1.Text = TextBox1.Text + Button3.Text;
}
else
{
TextBox1.Text = TextBox1.Text + Button3.Text;
}
}

protected void Button4_Click(object sender, EventArgs e)


{
a = Convert.ToInt32(TextBox1.Text);
TextBox1.Text = "";
b = '+';
TextBox1.Text += b;
}
protected void Button5_Click(object sender, EventArgs e)
{
if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text =="*") ||
(TextBox1.Text == "/"))
{
TextBox1.Text = "";
TextBox1.Text = TextBox1.Text + Button5.Text;
}
else
{
TextBox1.Text = TextBox1.Text + Button5.Text;
}
}
protected void Button6_Click(object sender, EventArgs e)
{
if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text =="*") ||
(TextBox1.Text == "/"))
{
TextBox1.Text = "";
TextBox1.Text = TextBox1.Text + Button6.Text;

}
else
{
TextBox1.Text = TextBox1.Text + Button6.Text;
}
}
protected void Button7_Click(object sender, EventArgs e)
{
if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text =="*") ||
(TextBox1.Text == "/"))
{
TextBox1.Text = "";
TextBox1.Text = TextBox1.Text + Button7.Text;
}
else
{
TextBox1.Text = TextBox1.Text + Button7.Text;
}
}
protected void Button8_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(TextBox1.Text);
TextBox1.Text = "";
b = '-';
TextBox1.Text += b;
}
protected void Button9_Click(object sender, EventArgs e)

{
if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text =="*") ||
(TextBox1.Text == "/"))
{
TextBox1.Text = "";
TextBox1.Text = TextBox1.Text + Button9.Text;
}
else
{
TextBox1.Text = TextBox1.Text + Button9.Text;
}
}
protected void Button10_Click(object sender, EventArgs e)
{
if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text =="*") ||
(TextBox1.Text == "/"))
{
TextBox1.Text = "";
TextBox1.Text = TextBox1.Text + Button10.Text;
}
else
{
TextBox1.Text = TextBox1.Text + Button10.Text;
}
}
protected void Button11_Click(object sender, EventArgs e)
{

if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text =="*") ||


(TextBox1.Text == "/"))
{
TextBox1.Text = "";
TextBox1.Text = TextBox1.Text + Button11.Text;
}
else
{
TextBox1.Text = TextBox1.Text + Button11.Text;
}
}
protected void Button12_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(TextBox1.Text);
b = '*';
TextBox1.Text ="";
TextBox1.Text += b;
}
protected void Button13_Click(object sender, EventArgs e)
{
if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text =="*") ||
(TextBox1.Text == "/"))
{
TextBox1.Text = "";
TextBox1.Text = TextBox1.Text + Button13.Text;
}
else

{
TextBox1.Text = TextBox1.Text + Button13.Text;
}
}
protected void Button14_Click(object sender, EventArgs e)
{
TextBox1.Text ="";
}
protected void Button15_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(TextBox1.Text);
TextBox1.Text = "";
b = '/';
TextBox1.Text += b;
}
protected void Button16_Click(object sender, EventArgs e)
{
c = Convert.ToInt32(TextBox1.Text);
TextBox1.Text = "";
if(b=='/')
{
d = a / c;
TextBox1.Text += d;
a = d;
}
else if (b == '+')
{

d = a + c;
TextBox1.Text += d;
a = d;
}
else if (b == '-')
{
d = a - c;
TextBox1.Text += d;
a = d;
}
else
{
d = a * c;
TextBox1.Text += d;
a = d;
}
}
}

using
using
using
using

System;
System.Configuration;
System.Data;
System.Linq;

using
using
using
using
using
using
using
using

System.Web;
System.Web.Security;
System.Web.UI;
System.Web.UI.HtmlControls;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
System.Xml.Linq;
System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page


{
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString =
ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
if (con.State == ConnectionState.Closed)
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Insert into StudentDetails
values(@Sid,@Sname,@Age,@Email,@Phone)";
cmd.Connection = con;
cmd.Parameters.Add("@Sid", SqlDbType.Int).Value = Convert.ToInt32(TextBox1.Text);
cmd.Parameters.Add("@Sname", SqlDbType.NChar).Value = TextBox2.Text;
cmd.Parameters.Add("@Age", SqlDbType.Int).Value =
Convert.ToInt32(TextBox3.Text);
cmd.Parameters.Add("@Email", SqlDbType.NChar).Value = TextBox4.Text;
cmd.Parameters.Add("@Phone", SqlDbType.NChar).Value = TextBox5.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
TextBox1.Text = " ";
TextBox2.Text = " ";
TextBox3.Text = " ";
TextBox4.Text = " ";
TextBox5.Text = " ";
TextBox1.Focus();
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Delete StudentDetails where Sid=@Sid";
cmd.Connection = con;
cmd.Parameters.Add("@Sid", SqlDbType.Int).Value = Convert.ToInt32(TextBox1.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
TextBox1.Text = " ";
TextBox2.Text = " ";
TextBox3.Text = " ";
TextBox4.Text = " ";
TextBox5.Text = " ";
TextBox1.Focus();
}

protected void Button3_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Update StudentDetails set
Sid=@Sid,Sname=@Sname,Age=@Age,Email=@Email,Phone=@Phone where
Sid=@Sid";
cmd.Connection = con;
cmd.Parameters.Add("@Sid", SqlDbType.Int).Value = Convert.ToInt32(TextBox1.Text);
cmd.Parameters.Add("@Sname", SqlDbType.NChar).Value = TextBox2.Text;
cmd.Parameters.Add("@Age", SqlDbType.Int).Value =
Convert.ToInt32(TextBox3.Text);
cmd.Parameters.Add("@Email", SqlDbType.NChar).Value = TextBox4.Text;
cmd.Parameters.Add("@Phone", SqlDbType.NChar).Value = TextBox5.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
TextBox1.Text = " ";
TextBox2.Text = " ";
TextBox3.Text = " ";
TextBox4.Text = " ";
TextBox5.Text = " ";
TextBox1.Focus();
}
protected void Button4_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select * from StudentDetails";
cmd.Connection = con;
SqlDataReader dr;
dr = cmd.ExecuteReader();
ListBox1.DataTextField = "Sname";
ListBox1.DataValueField = "Sid";
ListBox1.DataSource = dr;
ListBox1.DataBind();
cmd.Dispose();
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from StudentDetails where Sid=@sid";
cmd.Connection = con;
cmd.Parameters.Add("@Sid", SqlDbType.Int).Value =
Convert.ToInt32(ListBox1.SelectedValue);
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
TextBox1.Text = dr["Sid"].ToString();
TextBox2.Text = dr["Sname"].ToString();
TextBox3.Text = dr["Age"].ToString();
TextBox4.Text = dr["Email"].ToString();
TextBox5.Text = dr["Phone"].ToString();
}
dr.Close();
cmd.Dispose();

Repeater

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


Inherits="paging" %>
<!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>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr>
<td style="width:70px"><%#DataBinder.Eval(Container.DataItem, "StRoll")
%></td>
<td style="width:150px"><%#DataBinder.Eval(Container.DataItem, "StName")
%></td>
<td style="width:30px"><%#DataBinder.Eval(Container.DataItem, "StPercent")
%></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<asp:LinkButton ID="lnkBtnPrev" runat="server" Font-Underline="False"
OnClick="lnkBtnPrev_Click" Font-Bold="True"><< Prev </asp:LinkButton>
<input id="txtHidden" style="width: 28px" type="hidden" value="1"
runat="server" />
<asp:LinkButton ID="lnkBtnNext" runat="server" Font-Underline="False"
OnClick="lnkBtnNext_Click" Font-Bold="True">Next >></asp:LinkButton>
</div>
</form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class paging : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)


{
if (!IsPostBack)
{
bindrepeater();
}
}
protected void lnkBtnPrev_Click(object sender, EventArgs e)
{
txtHidden.Value = Convert.ToString(Convert.ToInt16(txtHidden.Value) - 1);
bindrepeater();
}
protected void lnkBtnNext_Click(object sender, EventArgs e)
{
txtHidden.Value = Convert.ToString(Convert.ToInt16(txtHidden.Value) + 1);
bindrepeater();
}
public void bindrepeater()
{
SqlConnection con = new SqlConnection("Your Conection String");
SqlCommand com = new SqlCommand("sp_StudentRec", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@pg", SqlDbType.Int).Value =
Convert.ToInt16(txtHidden.Value);
com.Parameters.Add("@pgSize", SqlDbType.Int).Value = 5;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
DataSet ds = new DataSet();
da.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataBind();
pageno(Convert.ToInt16(ds.Tables[1].Rows[0][0].ToString()));

}
public void pageno(int totItems)
{
// Calculate total numbers of pages
int pgCount = totItems / 5 + totItems % 5;

// Display Next>> button


if (pgCount - 1 > Convert.ToInt16(txtHidden.Value))
lnkBtnNext.Visible = true;
else
lnkBtnNext.Visible = false;
// Display <<Prev button
if ((Convert.ToInt16(txtHidden.Value)) > 1)
lnkBtnPrev.Visible = true;
else
lnkBtnPrev.Visible = false;
}

Datalist
<%@ 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>
</div>
<asp:DataList ID="DataList1" runat="server" DataKeyField="Cid"
oncancelcommand="DataList1_CancelCommand"
oneditcommand="DataList1_EditCommand"
onupdatecommand="DataList1_UpdateCommand"
onselectedindexchanged="DataList1_SelectedIndexChanged">
<ItemTemplate>
<b>Course Id:</b> <%# Eval("Cid") %><br />
<b>Course Name:</b> <%# Eval("Cname") %><br />
<b>Course Teacher:</b> <%# Eval("Teacher") %><br />
<b>Course Duration:</b> <%# Eval("Duration") %> <br />
<asp:LinkButton Id="lk" Text="Edit" CommandName ="edit" runat ="server"
></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<b>Course id:</b><asp:TextBox ID="t1" Text ='<%# bind("Cid") %>' runat
="server" ></asp:TextBox>
<b>Course Name:</b><asp:TextBox ID="t2" Text ='<%# bind("Cname") %>'
runat ="server" ></asp:TextBox>
<b>Course Teacherid:</b><asp:TextBox ID="t3" Text ='<%# bind("Teacher")
%>' runat ="server" ></asp:TextBox>
<b>Course Duration:</b><asp:TextBox ID="t4" Text ='<%# bind("Duration")
%>' runat ="server" ></asp:TextBox>
<asp:LinkButton ID="lk1" Text ="Update" CommandName ="update" runat
="server"></asp:LinkButton><br />
<asp:LinkButton ID="lk2" Text ="Cancel" CommandName ="cancel" runat
="server"></asp:LinkButton>

</EditItemTemplate>
</asp:DataList>
</form>
</body>
</html>

using
using
using
using

System;
System.Configuration;
System.Data;
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.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
listbind();
}
}
private void listbind()
{
SqlDataAdapter adp=new SqlDataAdapter("select * from
Cours",ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
DataSet ds=new DataSet();
adp.Fill(ds);
DataList1.DataSource =ds;
DataList1.DataBind();
}

e)

protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs


{

Int32 Cid;
Cid = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
SqlConnection con = new SqlConnection();
con.ConnectionString =
ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "update Cours set
Cname=@Cname,Teacher=@Teacher,Duration=@Duration where Cid=@Cid";
cmd.Connection = con;
cmd.Parameters.Add("@Cid", SqlDbType.Int).Value = Cid;
cmd.Parameters.Add("@Cname", SqlDbType.VarChar).Value = ((TextBox)
(e.Item.FindControl("t2"))).Text ;
cmd.Parameters.Add("@Teacher", SqlDbType.VarChar ).Value = ((TextBox)
(e.Item.FindControl("t3"))).Text ;
cmd.Parameters.Add("@Duration", SqlDbType.Int).Value = ((TextBox)
(e.Item.FindControl("t4"))).Text;
cmd.ExecuteNonQuery();
DataList1.EditItemIndex = -1;
listbind();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{

DataList1.EditItemIndex = e.Item.ItemIndex;
listbind();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs
e)

{
DataList1.EditItemIndex = -1;
listbind();

}
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}

Gidview
<%@ 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>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" AutoGenerateSelectButton="True"
DataKeyNames="Cid" DataSourceID="SqlDataSource1"
onrowupdating="GridView1_RowUpdating"
onselectedindexchanged="GridView1_SelectedIndexChanged"
AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="Cid" HeaderText="Cid" ReadOnly="True"
SortExpression="Cid" />
<asp:BoundField DataField="Cname" HeaderText="Cname"
SortExpression="Cname" />
<asp:BoundField DataField="Teacher" HeaderText="Teacher"
SortExpression="Teacher" />
<asp:BoundField DataField="Duration" HeaderText="Duration"
SortExpression="Duration" />
</Columns>
</asp:GridView>

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


ConnectionString="<%$ ConnectionStrings:StudentConnectionString2 %>"
SelectCommand="SELECT * FROM [Cours]"
DeleteCommand="DELETE FROM Cours WHERE (Cid = @Cid)"
InsertCommand="INSERT INTO Cours(Cid, Cname, Teacher, Duration) VALUES (@Cid,
@Cname, @Teacher, @Duration)"
UpdateCommand="UPDATE Cours SET Cname = @Cname, Teacher = @Teacher,
Duration = @Duration WHERE (Cid = @Cid)">
<DeleteParameters>
<asp:Parameter Name="Cid" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Cname" />
<asp:Parameter Name="Teacher" />
<asp:Parameter Name="Duration" />
<asp:Parameter Name="Cid" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Cid" />
<asp:Parameter Name="Cname" />
<asp:Parameter Name="Teacher" />
<asp:Parameter Name="Duration" />
</InsertParameters>
</asp:SqlDataSource>
</form>
</body>
</html>

using
using
using
using
using
using
using
using
using
using
using
using

System;
System.Configuration;
System.Data;
System.Linq;
System.Web;
System.Web.Security;
System.Web.UI;
System.Web.UI.HtmlControls;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
System.Xml.Linq;
System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlDataSource1.UpdateParameters["Cid"].DefaultValue = e.Keys[0].ToString();
SqlDataSource1.UpdateParameters["Cname"].DefaultValue
=((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
SqlDataSource1.UpdateParameters["Teacher"].DefaultValue =
((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
SqlDataSource1.UpdateParameters["Duration"].DefaultValue =
((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;

SqlDataSource1.Update();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
TextBox2.Text = GridView1.SelectedRow.Cells[2].Text;
TextBox3.Text = GridView1.SelectedRow.Cells[3].Text;
}
}

DetailView

<%@ 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 id="Head1" runat="server">
<title></title>
<style type="text/css">
.DetailsViewClass
{
font-family: verdana,arial,sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
border-style: solid;
}
.Header
{
background: #b5cfd2;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
.Foter
{
background: #dcddc0;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
.btn
{
background: #ffffff;
border-width: 1px;
padding: 2px;
border-style: solid;
border-color: #999999;

font-family: verdana,arial,sans-serif;
font-size: 11px;
}
.textbox
{
border-width: 1px;
padding: 1px;
border-style: solid;
border-color: #999999;
font-family: verdana,arial,sans-serif;
font-size: 11px;
width: 100px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsViewExample" AllowPaging="true"
AutoGenerateRows="false"
runat="server" Height="50px" CssClass="DetailsViewClass" CellPadding="4"
OnPageIndexChanging="DetailsViewExample_PageIndexChanging"
OnItemCommand="DetailsViewExample_ItemCommand1"
OnItemUpdating="DetailsViewExample_ItemUpdating"
OnModeChanging="DetailsViewExample_ModeChanging"
OnItemInserting="DetailsViewExample_ItemInserting"
Width="270px" OnItemDeleting="DetailsViewExample_ItemDeleting">
<Fields>
<asp:TemplateField HeaderText="Course Id">
<ItemTemplate>
<asp:Label ID="lblCid" Text='<%# Eval("Cid") %>' Visible="true"
runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCid" Text='<%# Eval("Cid") %>' runat="server"
CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course Name">
<ItemTemplate>
<asp:Label ID="lblCName" Text='<%# Eval("CName") %>'
runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCName" Text='<%# Eval("CName") %>'
runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Teacher">
<ItemTemplate>
<asp:Label ID="lblTeacher" Text='<%# Eval("Teacher") %>'
runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>

<asp:TextBox ID="txtTeacher" Text='<%# Eval("Teacher") %>'


runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duration">
<ItemTemplate>
<asp:Label ID="lblDuration" Text='<%# Eval("Duration") %>'
runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDuration" Text='<%# Eval("Duration") %>'
runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField Visible="true" ShowInsertButton="true"
ShowCancelButton="true"
ShowDeleteButton="true" ShowEditButton="true" />
</Fields>
<PagerStyle CssClass="Foter" />
<FieldHeaderStyle Width="80px" CssClass="Header" />
</asp:DetailsView>
</div>
</form>
</body>
</html>

using
using
using
using
using
using
using
using
using
using
using
using
using

System;
System.Configuration;
System.Data;
System.Linq;
System.Web;
System.Web.Security;
System.Web.UI;
System.Web.UI.HtmlControls;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
System.Xml.Linq;
System.Data.Sql;
System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindDetailtView();
}
}
private void bindDetailtView()
{
try
{
DataSet Ds = GetDataSet("Select * from Cours");
DetailsViewExample.DataSource = Ds;

DetailsViewExample.DataBind();
}
catch (Exception ex) { throw ex; }

}
private DataSet GetDataSet(string Query)
{
DataSet Ds = new DataSet();
try
{
SqlConnection Con = new SqlConnection();
Con.ConnectionString =
ConfigurationManager.ConnectionStrings["StudentConnectionString"].ConnectionString;
Con.Open();
SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
Da.Fill(Ds);
}
catch (Exception) { }
return Ds;
}
private void ExecuteQuery(string Query)
{
try
{
SqlConnection Con = new SqlConnection();
Con.ConnectionString =
ConfigurationManager.ConnectionStrings["StudentConnectionString"].ConnectionString;
Con.Open();
SqlCommand cmd = new SqlCommand(Query, Con);
cmd.ExecuteNonQuery();
Con.Close();
cmd.Dispose();
}
catch (Exception) { }
}
protected void DetailsViewExample_PageIndexChanging(object sender,
DetailsViewPageEventArgs e)
{
DetailsViewExample.PageIndex = e.NewPageIndex;
bindDetailtView();
}
protected void DetailsViewExample_ItemCommand1(object sender,
DetailsViewCommandEventArgs e)
{
switch (e.CommandName.ToString())
{
case "Edit":
DetailsViewExample.ChangeMode(DetailsViewMode.Edit);
bindDetailtView();
break;
case "Cancel":
DetailsViewExample.ChangeMode(DetailsViewMode.ReadOnly);
bindDetailtView();
break;
case "New":
DetailsViewExample.ChangeMode(DetailsViewMode.Insert);

bindDetailtView();
break;
}
}
protected void DetailsViewExample_ModeChanging(object sender,
DetailsViewModeEventArgs e)
{
}
protected void DetailsViewExample_ItemUpdating(object sender,
DetailsViewUpdateEventArgs e)
{
TextBox txtCid = (TextBox)DetailsViewExample.FindControl("txtCid") ;
TextBox txtCname = (TextBox)DetailsViewExample.FindControl("txtCname");
TextBox txtTeacher = (TextBox)DetailsViewExample.FindControl("txtTeacher");
TextBox txtDuration = (TextBox)DetailsViewExample.FindControl("txtDuration");
string Query = "Update Cours Set Cname ='" + txtCname.Text + "' ,Teacher ='" +
txtTeacher.Text + "',Duration=" + Convert.ToInt32(txtDuration.Text) +" where Cid=" +
Convert.ToInt32( txtCid.Text) ;
ExecuteQuery(Query);
DetailsViewExample.ChangeMode(DetailsViewMode.ReadOnly);
bindDetailtView();
}
protected void DetailsViewExample_ItemInserting(object sender,
DetailsViewInsertEventArgs e)
{
TextBox txtCid = (TextBox)DetailsViewExample.FindControl("txtCid");
TextBox txtCname = (TextBox)DetailsViewExample.FindControl("txtCname");
TextBox txtTeacher = (TextBox)DetailsViewExample.FindControl("txtTeacher");
TextBox txtDuration = (TextBox)DetailsViewExample.FindControl("txtDuration");
string Query = "Insert into Cours ([Cid] ,[Cname] ,[Teacher] ,[Duration]) values
(" + Convert.ToInt32(txtCid.Text) + " ,'" + txtCname.Text + "' ,'" + txtTeacher.Text + "'," +
Convert.ToInt32(txtDuration.Text) + ")" ;
ExecuteQuery(Query);
DetailsViewExample.ChangeMode(DetailsViewMode.ReadOnly);
bindDetailtView();
}
protected void DetailsViewExample_ItemDeleting(object sender,
DetailsViewDeleteEventArgs e)
{
Label lblCid = (Label)DetailsViewExample.FindControl("lblCid");
string Query = "Delete from Cours where Cid =" + lblCid.Text;
ExecuteQuery(Query);
bindDetailtView();
}
}

FormView
<%@ 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>
</div>
<asp:FormView ID="FormView1" runat="server" AllowPaging="True">
<EditItemTemplate>
<asp:Label ID="lblcid" runat="server" Text="Course Id"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtCid" runat="server" Text='<%# Eval("Cid")
%>'></asp:TextBox>
<br />
<br />
<asp:Label ID="lblCname" runat="server" Text="Course Name"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtCname" runat="server" Text='<%# Eval("Cname")
%>'></asp:TextBox>
<br />
<br />
<asp:Label ID="lblteach" runat="server" Text="Teacher"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtteach" runat="server" Text='<%# Eval("Teacher")
%>'></asp:TextBox>
<br />
<br />
<asp:Label ID="lblDuration" runat="server" Text="Duration"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtDuration" runat="server" Text='<%# Eval("Duration")
%>'></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="btnupdate" runat="server" Text="Update" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
<br />
</EditItemTemplate>
<InsertItemTemplate>
<asp:Label ID="lblcid" runat="server" Text="Course Id"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp; &nbsp;
<asp:TextBox ID="txtCid" runat="server" Text='<%# Eval("Cid")
%>'></asp:TextBox>

<br />
<br />
<asp:Label ID="lblCname" runat="server" Text="Course Name"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtCname" runat="server" Text='<%# Eval("Cname")
%>'></asp:TextBox>
<br />
<br />
<asp:Label ID="lblteach" runat="server" Text="Teacher"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
<asp:TextBox ID="txtteach" runat="server" Text='<%# Eval("Teacher")
%>'></asp:TextBox>
<br />
<br />
<asp:Label ID="lblDuration" runat="server" Text="Duration"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
<asp:TextBox ID="txtDuration" runat="server" Text='<%# Eval("Duration")
%>'></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="btnSave" runat="server" Text="Save" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
<br />
<br />
<br />
<br />
<br />
<br />
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcid" runat="server" Text="Course Id"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp; &nbsp;
<asp:TextBox ID="txtCid" runat="server" Text='<%# Eval("Cid")
%>'></asp:TextBox>
<br />
<br />
<asp:Label ID="lblCname" runat="server" Text="Course Name"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtCname" runat="server" Text='<%# Eval("CName")
%>'></asp:TextBox>
<br />
<br />
<asp:Label ID="lblteach" runat="server" Text="Teacher"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;

<asp:TextBox ID="txtteach" runat="server" Text='<%# Eval("Teacher")


%>'></asp:TextBox>
<br />
<br />
<asp:Label ID="lblDuration" runat="server" Text="Duration"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
<asp:TextBox ID="txtDuration" runat="server" Text='<%# Eval("Duration")
%>'></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="btnSave" runat="server" Text="Save" />
&nbsp;<asp:Button ID="btndelete" runat="server" Text="Delete" />
&nbsp;<asp:Button ID="btnedit" runat="server" Text="Edit" />
<br />
<br />
<br />
</ItemTemplate>
</asp:FormView>
</form>
</body>
</html>

using
using
using
using
using
using
using
using
using
using
using

System;
System.Configuration;
System.Data;
System.Linq;
System.Web;
System.Web.Security;
System.Web.UI;
System.Web.UI.HtmlControls;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
System.Xml.Linq;

using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindDetailtView();
}

}
private void bindDetailtView()
{
try
{
DataSet Ds = GetDataSet("Select * from Cours");
FormView1.DataSource = Ds;
FormView1.DataBind();
}

catch (Exception ex) { throw ex; }


}
private DataSet GetDataSet(string Query)
{
DataSet Ds = new DataSet();
try
{
SqlConnection Con = new SqlConnection();
Con.ConnectionString =
ConfigurationManager.ConnectionStrings["StudentConnectionString"].ConnectionString;
Con.Open();
SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
Da.Fill(Ds);

}
catch (Exception) { }
return Ds;

Linq
<%@ 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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

<asp:Button ID="Insert" runat="server" onclick="Insert_Click" Text="Insert" />


&nbsp;&nbsp;
<asp:Button ID="Update" runat="server" onclick="Update_Click" Text="Update" />
&nbsp;&nbsp;
<asp:Button ID="Delete" runat="server" onclick="Delete_Click" Text="Delete" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StudentClassesDataContext db = new StudentClassesDataContext();
var empall = from p in db.StudentDetails select p;
GridView1.DataSource = empall;
GridView1.DataBind();
}
protected void Insert_Click(object sender, EventArgs e)
{
StudentClassesDataContext db = new StudentClassesDataContext();
StudentDetail mystudent = new StudentDetail();
mystudent.Sid = 6666;
mystudent.Sname="Anita Sharma";
mystudent.Age=21;
mystudent.Email="anita@gmail.com";
mystudent.Phone="65757585";
mystudent.Cname="CJET";
db.StudentDetails.InsertOnSubmit(mystudent);
db.SubmitChanges();
Label1.Text="Inserted successfully";
}
protected void Update_Click(object sender, EventArgs e)
{
StudentClassesDataContext db = new StudentClassesDataContext();
var studentUpdt = from p in db.StudentDetails
where p.Age == 27
select p;
foreach (var students in studentUpdt)
{
students.Age = 28;
}
db.SubmitChanges();
Label1.Text = "Updated successfully";
}
protected void Delete_Click(object sender, EventArgs e)
{

StudentClassesDataContext db = new StudentClassesDataContext();


var studentdel = from p in db.StudentDetails
where p.Sid == 6666
select p;
if (studentdel.Count() > 0)
{
db.StudentDetails.DeleteOnSubmit(studentdel.First());
db.SubmitChanges();
}
Label1.Text = "Deleted Successfully";
}

<%@ 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
{
width: 439px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table style="width:100%;">
<tr>
<td rowspan="3">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
<td class="style1" rowspan="3">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btninsert" runat="server" onclick="btninsert_Click"
Text="Insert" />
<asp:Button ID="btndelete" runat="server" onclick="btndelete_Click"
Text="Delete" />
<asp:Button ID="btnupdate" runat="server" onclick="btnupdate_Click"
Text="Update" />
</td>
<td>
&nbsp;</td>
</tr>

<tr>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
&nbsp;</td>
</tr>
</table>
<br />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</form>
</body>
</html>

using
using
using
using
using
using
using
using
using
using
using

System;
System.Configuration;
System.Data;
System.Linq;
System.Web;
System.Web.Security;
System.Web.UI;
System.Web.UI.HtmlControls;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
System.Xml.Linq;

public partial class _Default : System.Web.UI.Page


{
CoursDataClassesDataContext db = new CoursDataClassesDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var call = from p in db.Cours select p;
GridView1.DataSource = call;
GridView1.DataBind();
}
}

protected void btninsert_Click(object sender, EventArgs e)


{
CoursDataClassesDataContext db = new CoursDataClassesDataContext();
Cour mycours = new Cour();
mycours.Cid =Convert.ToInt32(TextBox1.Text);
mycours.CName = TextBox2.Text;
mycours.Teacher = TextBox3.Text;
mycours.Duration = Convert.ToInt32(TextBox4.Text);
db.Cours.InsertOnSubmit(mycours);
db.SubmitChanges();
Label1.Text="Inserted";
}
protected void btndelete_Click(object sender, EventArgs e)
{
CoursDataClassesDataContext db = new CoursDataClassesDataContext();
var coudel=from p in db.Cours where p.Cid==Convert.ToInt32(TextBox1.Text) select
p;

if (coudel.Count() > 0)
{
db.Cours.DeleteOnSubmit(coudel.First());
db.SubmitChanges();
}
Label1.Text = "Deleted";

}
protected void btnupdate_Click(object sender, EventArgs e)
{
CoursDataClassesDataContext db = new CoursDataClassesDataContext();
var couupdt = from p in db.Cours where p.Cid == Convert.ToInt32(TextBox1.Text)
select p;
foreach (var c in couupdt)
{
c.CName = TextBox2.Text;
c.Duration = Convert.ToInt32(TextBox4.Text);
c.Teacher = TextBox3.Text;
}
db.SubmitChanges();
Label1.Text = "Updated";
}
}

Linqdatasource Control
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Linqdatasource.aspx.cs"
Inherits="Linqdatasource" %>
<!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>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="StudentClassesDataContext" EnableDelete="True"
EnableInsert="True" EnableUpdate="True" EntityTypeName=""
TableName="Cours">
</asp:LinqDataSource>
</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Cid"
DataSourceID="LinqDataSource1">
<Columns>
<asp:BoundField DataField="Cid" HeaderText="Cid" ReadOnly="True"
SortExpression="Cid" />
<asp:BoundField DataField="Cname" HeaderText="Cname"
SortExpression="Cname" />
<asp:BoundField DataField="Teacher" HeaderText="Teacher"
SortExpression="Teacher" />
<asp:BoundField DataField="Duration" HeaderText="Duration"
SortExpression="Duration" />
<asp:BoundField DataField="sid" HeaderText="sid" SortExpression="sid" />
</Columns>
</asp:GridView>
</form>
</body>
</html>

Procedure:
ALTER PROCEDURE logincheck
(
@u varchar(50),
@p varchar(50)
)
AS
declare @ap varchar(50)
select @ap=password from tbuser where username=@u
if @ap is null
return -1
else
if @ap=@p
return 1
else return -2
RETURN

Coding file:
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.UI;

using
using
using
using

System.Web.UI.WebControls;
System.Data;
System.Data.SqlClient;
System.Configuration;

public partial class _Default : System.Web.UI.Page


{
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString =
ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
private Int32 checkuser(string u, string p)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "logincheck";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.Add("@u", SqlDbType.VarChar, 50).Value = u;
cmd.Parameters.Add("@p", SqlDbType.VarChar, 50).Value = p;
SqlParameter p1 = new SqlParameter("@ret", SqlDbType.Int);
p1.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(p1);
cmd.ExecuteNonQuery();
Int32 k;
k = Convert.ToInt32(cmd.Parameters["@ret"].Value);
return k;
}

protected void Button1_Click(object sender, EventArgs e)


{
Int32 d;
d = checkuser(TextBox1.Text, TextBox2.Text);
if (d == -1)
{
Label1.Text = "wrong user";
}
if (d == -2)
{
Label1.Text = "wrong password";
}
if (d == 1)
{
Label1.Text = "login";
}
}

design page
<!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>
LOGIN:-<br />
<br />
USERNAME:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
PASSWORD:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>

Anda mungkin juga menyukai