0% found this document useful (0 votes)
59 views45 pages

1-06-04-2021-Calculatrice-Changment Color: Rachid Tounsy

This document contains code for several ASP.NET web forms projects. It includes code to change the background color of a panel based on the selected radio button, code for a basic calculator application, and code for an exercise involving labels, literals, and buttons.

Uploaded by

said
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)
59 views45 pages

1-06-04-2021-Calculatrice-Changment Color: Rachid Tounsy

This document contains code for several ASP.NET web forms projects. It includes code to change the background color of a panel based on the selected radio button, code for a basic calculator application, and code for an exercise involving labels, literals, and buttons.

Uploaded by

said
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/ 45

RACHID TOUNSY

1-06-04-2021-Calculatrice-changment color
TP-1 : changment color- ListItem

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication_TP2.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
#Panel1
{
height:200px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True"


OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem>rouge</asp:ListItem>
<asp:ListItem>bleu</asp:ListItem>
<asp:ListItem>vert</asp:ListItem>

</asp:RadioButtonList>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />


<br />

<asp:Panel ID="Panel1" runat="server">


<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://google.com">lien Vers
google</asp:HyperLink>
</asp:Panel>

</div>
</form>
</body>
</html>
-------
namespace WebApplication_TP2
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

1
protected void Button1_Click(object sender, EventArgs e)
{
if(RadioButtonList1.SelectedIndex==0)
{
Panel1.BackColor = Color.Red;
}
if (RadioButtonList1.SelectedIndex == 1)
{
Panel1.BackColor = Color.Blue;
}
if (RadioButtonList1.SelectedIndex == 2)
{
Panel1.BackColor = Color.Green;
}
}

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)


{
if (RadioButtonList1.SelectedIndex == 0)
{
Panel1.BackColor = Color.Red;
}
if (RadioButtonList1.SelectedIndex == 1)
{
Panel1.BackColor = Color.Blue;
}
if (RadioButtonList1.SelectedIndex == 2)
{
Panel1.BackColor = Color.Green;
}
}

protected System.Void Button1_Click(System.Object sender, System.EventArgs e)


{

protected System.Void RadioButtonList1_SelectedIndexChanged(System.Object sender, System.EventArgs e)


{

}
}
}

TP2 : Calculatrice

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs"


Inherits="WebApplication1.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
.c1{
background-color:aqua;
width:80%;
}
.c2{
font-size:20px;
font-weight:bold:
}
.auto-style1 {

2
width: 100%;
}
.auto-style2 {
width: 331px;
}
.auto-style3 {
width: 331px;
height: 22px;
}
.auto-style4 {
height: 22px;
}
.auto-style5 {
width: 331px;
height: 23px;
}
.auto-style6 {
height: 23px;
}
.auto-style7 {
width: 331px;
height: 25px;
}
.auto-style8 {
height: 25px;
}
.auto-style9 {
width: 331px;
height: 24px;
}
.auto-style10 {
height: 24px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h1> Calculatrice</h1>
<div class="c1">

<table class="auto-style1">
<tr>
<td class="auto-style2">
<asp:Label ID="Label1" runat="server" Text="Donner a"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" style="margin-bottom:
0px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label2" runat="server" Text="Donner b"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" style="margin-bottom:
0px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style5"></td>
<td class="auto-style6">

3
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" Width="84px" />
</td>
</tr>
<tr>
<td class="auto-style3">
<asp:Label ID="Label3" runat="server" CssClass="c2" Text="+"></asp:Label>
</td>
<td class="auto-style4">
<asp:Label ID="Label7" runat="server" Text="Rersultat"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style9">
<asp:Label ID="Label4" runat="server" BorderStyle="None" ForeColor="Black" CssClass="c2" Text="-"
Width="60px" Height="19px"></asp:Label>
</td>
<td class="auto-style10">
<asp:Label ID="Label8" runat="server" Text="Rersultat"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style7">
<asp:Label ID="Label5" runat="server" BorderStyle="None" ForeColor="Black"
CssClass="c2" Text="*" Width="60px" Height="19px"></asp:Label>
</td>
<td class="auto-style8">
<asp:Label ID="Label9" runat="server" Text="Rersultat"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label6" runat="server" BorderStyle="None" ForeColor="Black"
CssClass="c2" Text="/" Width="60px" Height="19px"></asp:Label>
</td>
<td>
<asp:Label ID="Label10" runat="server" Text="Rersultat"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td>
<asp:Label ID="Label11" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>

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

namespace WebApplication1
{
public partial class WebForm2 : System.Web.UI.Page

4
{
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
try
{
int a = int.Parse(TextBox1.Text);
int b= int.Parse(TextBox2.Text);
int Sum = a + b;
int D = a / b;

int M = a * b;
int S = a - b;
Label7.Text = "Somme est " + Sum.ToString();
Label8.Text = "Soustraction est " + S.ToString();
Label9.Text = "Multiplicatin est " + M.ToString();
Label10.Text = "Division est " + D.ToString();

}
catch(Exception ex)
{
Label11.Text= ex.Message;
}
}

protected System.Void Button1_Click(System.Object sender, System.EventArgs e)


{
}
}
}

3-07-04-2021-Activity_Apprentissage_2_asp.net
Exercices 1

5
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Exercices1.aspx.cs"
Inherits="Exercices1.Exercices1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
height: 26px;
}
.auto-style3 {
height: 25px;
}
.auto-style4 {
height: 27px;
}
table
{
background-color:aqua;
}
#Button1
{
width:200px;
height:30px;
color:black;
background-color:darkgreen;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h1> Text </h1>

<asp:Label ID="Label7" runat="server" Text="Je suis le Label simple"></asp:Label>


<br />
<asp:Label ID="Label8" runat="server" Text="Un label Sophistique:"></asp:Label>
&nbsp;<asp:Label ID="Label9" runat="server" Text="Je suis le label
Sophistique"></asp:Label>
<br />
<asp:Label ID="Label10" runat="server" Text="Un lireal :"></asp:Label>
<asp:Literal ID="Literal1" runat="server" Text="Je suis le Literal"></asp:Literal>
<br />
<asp:Label ID="Label11" runat="server" Text="U auter Lueral :"></asp:Label>
<asp:Literal ID="Literal2" runat="server" Text="Je suis le&lt;b&gt; Literal
rusé&lt;/b&gt;"></asp:Literal>
<br />
<br />

<table class="auto-style1">
<tr>
<td class="auto-style3">
<asp:Label ID="Label1" runat="server" Text="Tapez u texte
(AutoPostBack=False)"></asp:Label>
</td>

6
<td class="auto-style3">
<asp:TextBox ID="TextBox1" runat="server" Height="22px"
OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
</td>
<td class="auto-style3">
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Tapez u texte
(AutoPostBack=True)"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True"
OnTextChanged="TextBox2_TextChanged"></asp:TextBox>
</td>
<td>
<asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label3" runat="server" Text="Mod de passe"></asp:Label>
</td>
<td class="auto-style4">
<asp:TextBox ID="TextBox3" runat="server" AutoPostBack="True"
TextMode="Password"></asp:TextBox>
</td>
<td class="auto-style4"></td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label4" runat="server" Text="Plusieurs Lignes"></asp:Label>
</td>
<td class="auto-style2">
<asp:TextBox ID="TextBox4" runat="server" AutoPostBack="True"
TextMode="MultiLine" Height="78px" OnTextChanged="TextBox4_TextChanged"></asp:TextBox>
</td>
<td class="auto-style2">
<asp:CheckBox ID="CheckBox1" runat="server"
OnCheckedChanged="CheckBox1_CheckedChanged" Text="Wrap" />
<br />
<asp:CheckBox ID="CheckBox2" runat="server" Text="ReadOnly"
OnCheckedChanged="CheckBox2_CheckedChanged" />
</td>
</tr>
</table>

<div>
<asp:Button ID="Button1" runat="server" Text="OK" Width="782px" />
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if(this.IsPostBack==false)
{
CheckBox1.Checked = TextBox4.ReadOnly;
CheckBox2.Checked = TextBox4.Wrap;
}

7
}

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)


{
TextBox4.Wrap = CheckBox1.Checked;
}

protected void TextBox1_TextChanged(object sender, EventArgs e)


{
Label5.Text = TextBox1.Text;
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
TextBox4.ReadOnly = CheckBox2.Checked;
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
Label6.Text = TextBox2.Text;
}

protected void TextBox4_TextChanged(object sender, EventArgs e)


{}

protected System.Void TextBox1_TextChanged(System.Object sender, System.EventArgs e)


{

TP_dropdwonlist_manipulation

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DropDownList.aspx.cs"


Inherits="TP_dropdwonlist_manipulation.DropDownList" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Ville"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" style="margin-bottom: 0px">
</asp:DropDownList>
</div>

8
<br />
<asp:Button ID="Button1" runat="server" Text="Afficher la valeur selectionner"
OnClick="Button1_Click" />
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{if(this.IsPostBack==false) {
ListItem x = new ListItem("Rabat", "1");
ListItem y = new ListItem("Beni Mellal", "2");
ListItem z = new ListItem("Casa", "3");
ListItem E = new ListItem("Azillal", "4");
DropDownList1.Items.Add(x);
DropDownList1.Items.Add(y);
DropDownList1.Items.Add(z);
DropDownList1.Items.Add(E); }
}
protected void Button1_Click(object sender, EventArgs e)
{
int i = DropDownList1.SelectedIndex;
string ville = DropDownList1.Items[i].Text;
string Valauer_ville = DropDownList1.Items[i].Value;
Response.Write("indice selectionner : " + i.ToString() + " " + ville + " " +
Valauer_ville);
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)


{
int i = DropDownList1.SelectedIndex;
string ville = DropDownList1.Items[i].Text;
string Valauer_ville = DropDownList1.Items[i].Value;
Response.Write("indice selectionner : " + i.ToString() + " " + ville + " " +
Valauer_ville);
}

protected System.Void Button1_Click(System.Object sender, System.EventArgs e)


{

4-09-04-2021- Exercice_2_les controles_DropDownList_et_Button

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

namespace Exercice_2_les_controles_DropDownList_et_Button
{
public partial class Exercice_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DropDownList1.Items.Add(new ListItem("Euros", "0,7436"));
DropDownList1.Items.Add(new ListItem("Yen Japonais ", "81,0"));
DropDownList1.Items.Add(new ListItem("Dollars Canadien", "0,9996"));
Image1.Visible = false;
}
}

protected void Button1_Click(object sender, EventArgs e)


{
try
{
decimal montantUs = decimal.Parse(TextBox1.Text);
if(montantUs>=0)
{

TextBox1.BackColor = Color.White;
int i = DropDownList1.SelectedIndex;
decimal valuesMonnaie = decimal.Parse(DropDownList1.Items[i].Value);
decimal MontantTotale = montantUs * valuesMonnaie;
string nomMonnaie = DropDownList1.Items[i].Text;
ListItem element = DropDownList1.Items[DropDownList1.SelectedIndex];
Label3.Text = montantUs.ToString() + " Dollars U.S= ";
Label3.Text += MontantTotale.ToString() + " " + element.Text;
}
else
{
TextBox1.BackColor = Color.Red;
throw new Exception("montant ne doit pas etre negatifs");
}
}
catch(Exception ex)
{
Label3.Text = ex.Message;
}

protected void Button2_Click(object sender, EventArgs e)


{
int i = DropDownList1.SelectedIndex+1;

string nomiImage = "pic" + i.ToString() + ".JPG";


Image1.Visible = true;
Image1.ImageUrl = "IMAGE/" + nomiImage;

} }}

10
5-13-04-2021 DropdowList ville –carier

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TP1_DropdowList.aspx.cs"


Inherits="TP1_DropdowList.TP1_DropdowList" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Ville"></asp:Label>
&nbsp;
<asp:DropDownList ID="DropDownList1" runat="server" Height="16px"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" style="margin-bottom: 0px"
AutoPostBack="True">
</asp:DropDownList>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Quartier"></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server"
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" style="margin-bottom: 0px">
</asp:DropDownList>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Envoyer" OnClick="Button1_Click" />
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="message"></asp:Label>
<br />

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

using System;
using System.Collections.Generic;
using System.Linq;

11
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TP1_DropdowList
{
public partial class TP1_DropdowList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if(this.IsPostBack==false)
{
ListItem a = new ListItem("Choisir une Ville", "0");

ListItem x = new ListItem("Beni Mellal", "1");


ListItem y = new ListItem("Rabat", "2");
ListItem z = new ListItem("Casa", "3");
ListItem K = new ListItem("Eljadida", "4");
DropDownList1.Items.Add(a);

DropDownList1.Items.Add(x);
DropDownList1.Items.Add(y);
DropDownList1.Items.Add(z);
DropDownList1.Items.Add(K);
}
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)


{

int i = DropDownList1.SelectedIndex;
if(i==1)
{
DropDownList2.Items.Clear();
ListItem x1 = new ListItem("Takadom", "1");
ListItem x2 = new ListItem("ELHoda", "2");
ListItem x3= new ListItem("Riyad salam", "4");
DropDownList2.Items.Add(x1);
DropDownList2.Items.Add(x2);
DropDownList2.Items.Add(x3);
}
if (i == 2)
{
DropDownList2.Items.Clear();
ListItem a1 = new ListItem("Agdal", "1");
ListItem a2 = new ListItem("Hassan 6", "2");
ListItem a3 = new ListItem("Souissi", "3");
DropDownList2.Items.Add(a1);
DropDownList2.Items.Add(a2);
DropDownList2.Items.Add(a3);

}
if (i == 3)
{
DropDownList2.Items.Clear();
ListItem z1 = new ListItem("Hassan 2", "1");
ListItem z2 = new ListItem("Riyad", "2");
DropDownList2.Items.Add(z1);
DropDownList2.Items.Add(z2);

12
}
if (i == 4)
{
DropDownList2.Items.Clear();
ListItem K1 = new ListItem("Elsaada", "1");
ListItem K2 = new ListItem("Hawziya", "2");
ListItem K3 = new ListItem("Narjis", "3");
DropDownList2.Items.Add(K1);
DropDownList2.Items.Add(K2);
DropDownList2.Items.Add(K3);
}
}

protected void Button1_Click(object sender, EventArgs e)


{
int i = DropDownList1.SelectedIndex;
string ville = DropDownList1.Items[i].Text;
int x = DropDownList1.SelectedIndex;
string Quartier = DropDownList2.Items[i].Text;
Label3.Text = "ville : " + ville + " Quartier " + Quartier;

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)


{

}
}
}

6-14-04-2021-Controle de Validation

RequiredFieldValidator- ValidationSummary- CompareValidator

13
RegularExpressionValidator

7-20-04-2021- Cookies
Page 1

Page 2

14
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TP1.aspx.cs" Inherits="TP_Cookie.TP1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
.Panel1
{
height:250px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Panel ID="Panel1" runat="server">


<br />
<asp:Label ID="Label1" runat="server" Text="lange "></asp:Label>
&nbsp;
<asp:TextBox ID="TextBox1" runat="server" Width="309px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<br />
&nbsp;
<br />
<br />
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
</asp:RadioButtonList>
<br />
<br />
<br />
</asp:Panel>

</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;

namespace TP_Cookie
{
public partial class TP1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)

15
{
if (!IsPostBack) {
RadioButtonList1.Items.Add(new ListItem("Red", "0"));
RadioButtonList1.Items.Add(new ListItem("Blue", "1"));
RadioButtonList1.Items.Add(new ListItem("Green", "2"));

}
}

protected void Button1_Click(object sender, EventArgs e)


{
Response.Cookies["c1"].Value = "Bonjour Rachid Tounsy";
Response.Cookies["c1"].Expires = DateTime.Now.AddMinutes(2);
Response.Cookies["c2"].Value = TextBox1.Text;
Response.Cookies["c2"].Expires = DateTime.Now.AddMinutes(2);
//string color = RadioButtonList1.Items[RadioButtonList1.SelectedIndex].Text;
//Response.Cookies["c3"].Value = color;
Response.Redirect("afficherCokies.aspx");

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)


{

}
}
}

Page 2 afficherCookies
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace TP_Cookie
{
public partial class afficherCokies : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if ( Request.Cookies["c2"]!=null)
{
Label1.Text =Request.Cookies["c1"].Value.ToString();
Label2.Text = Request.Cookies["c2"].Value.ToString();

string langue = Label2.Text;


if(langue=="ar")
{
Response.Write("site en langue arabe");

}
else
{
Response.Redirect("TP1.aspx");
}

16
}
}
}
}

8-21-04-2021 Request_QueryString
TP1

Page 1
protected void Button1_Click(object sender, EventArgs e)
{
string url = "page2.aspx?nom=" + TextBox1.Text+
"&tel1="+TextBox3.Text+"&prenom="+TextBox2.Text;

Response.Redirect(url);
}
}

Page2

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

namespace Request_QueryString
{
public partial class page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

17
if (Request.QueryString["tel1"] != null)
{
string telFrompage1 = Request.QueryString["nom"].ToString();
string prenom = Request.QueryString["prenom"].ToString();
string tel = Request.QueryString["tel1"].ToString();

Label1.Text = "The name is : " + telFrompage1;


Label1.Text += "<br/> prenom is : " + prenom;
Label1.Text += " <br/> tel1 is : " + tel;

}
else
{
Response.Redirect("page1.aspx");
}
}
}
}

TP2 : cookie_couleur

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

namespace cookie_couleur
{
public partial class page1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
string couleur = "";
if (RadioButton1.Checked)
{
couleur = "R";
}
if (RadioButton2.Checked)
{
couleur = "G";
}

18
if (RadioButton3.Checked)
{
couleur = "B";
}
if (couleur == "")
{
Response.Write("veullez cocher une couleur");
}
else
{

//HttpCookie C1 = new HttpCookie("C1", couleur);


//Response.Cookies.Add(C1);
Response.Cookies["cookie_couleur"].Value = couleur;
Response.Cookies["cookie_couleur"].Expires = DateTime.Now.AddMinutes(3);
}
}
}
}

Page 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace cookie_couleur
{
public partial class page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (Request.Cookies["cookie_couleur"] != null)
{

string couleur = Request.Cookies["cookie_couleur"].Value.ToString();

if (couleur == "R")
{
Panel1.BackColor = Color.Red;
}
if (couleur == "G")
{
Panel1.BackColor = Color.Green;
}
if (couleur == "B")
{
Panel1.BackColor = Color.Blue;
}
}
else
{
Panel1.BackColor = Color.DarkRed;
}

protected void Button1_Click(object sender, EventArgs e)

19
{

}
}
}

9-23-04-2021 session_et_database_table_aspnet_

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

namespace TP1_session
{
public partial class TP_2_login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Request.QueryString["message"]!=null)
{
Label4.Text = Request.QueryString["message"];

}
}

protected void Button2_Click(object sender, EventArgs e)


{
/* cas ou une bd existe connection vers bd
* select * from user where=nom=@a and password=@pass
* retour ligne ok stocker session aller vers menu(verfier session)
* si non aucun ligin afficher message d erreur
*/

//on accpete pour instant nom="Tounsy" et password="1234";

if(TextBox1.Text.ToLower()=="tounsy" && TextBox2.Text=="1234")


{
Session["nom"] = TextBox1.Text;
Session["password"] =TextBox2.Text;
Response.Redirect("Menu.aspx");
}
else
{
Label3.Text = "login incorrect nom ou passsword erron";
}

20
}

protected void Button1_Click(object sender, EventArgs e)


{
TextBox1.Text = "";
TextBox2.Text = "";

}
}
}

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

namespace TP1_session
{
public partial class Menu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["nom"]!=null)
{
Label1.Text = Session["nom"].ToString();
Label2.Text = "Bonjour "+ Session["nom"].ToString()+",si mon TP Session 24-04-2021";
}
else
{
//Response.Redirect("TP_2_login.aspx");
Response.Redirect("TP_2_login.aspx?message= pas autorise d'entre dans menu !!");

protected void Button1_Click(object sender, EventArgs e)


{
Session["nom"] =null;
Session["password"] = null;
Response.Redirect("TP_2_login.aspx?message=merci pour votre visite ");

}
}
}

21
10-24-04-2021 session_et_database_table

Page login
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;
namespace TP1_session
{
public partial class TP_2_login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Request.QueryString["message"]!=null)
{
Label4.Text = Request.QueryString["message"];

}
}

protected void Button2_Click(object sender, EventArgs e)


{
try
{
string chainecon = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True";
string sql = "SELECT code,nom,password FROM client where nom like @nom and password like
@password";
SqlConnection con = new SqlConnection(chainecon);
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
SqlParameter P1 = new SqlParameter("@nom",TextBox1.Text);
cmd.Parameters.Add(P1);
SqlParameter P2 = new SqlParameter("@password",TextBox2.Text);

22
cmd.Parameters.Add(P2);
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read()==true)
{
string code = dr.GetValue(0).ToString();
string nom = dr.GetValue(1).ToString();
string password = dr.GetValue(2).ToString();
Session["code"] = code;
Session["nom"] = nom;
Session["password"] = password;
Response.Redirect("Menu.aspx");

}
else
{
//aucun lign dans la table client (base de donnee TP_ASP)
Label3.Text = "erreur de nom ou password";
}
con.Close();

}
catch(Exception ex )
{
Label4.Text = ex.Message;
}
}

protected void Button1_Click(object sender, EventArgs e)


{
TextBox1.Text = "";
TextBox2.Text = "";

}
}
}

Page inscription
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;
using System.Data;
namespace TP1_session
{
public partial class iscription : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
try
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True");
con.Open();

23
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into client values(@code,@nom,@password)";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@code", SqlDbType.Int).Value = TextBox1.Text;
cmd.Parameters.AddWithValue("@nom", SqlDbType.VarChar).Value = TextBox2.Text;
cmd.Parameters.AddWithValue("@password", SqlDbType.VarChar).Value = TextBox3.Text;
int dr = cmd.ExecuteNonQuery();
if(dr==0)
{
Label5.Text = " Atansion !!!!";
Response.Redirect("iscription.aspx");
}
else
{
Label5.Text = " bien Ajouter ";
Response.Redirect("Menu.aspx");

}
con.Close();
}
catch(Exception ex)
{
Label5.Text = ex.Message;
}

}
}
}

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

namespace TP1_session
{
public partial class Menu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["nom"]!=null)
{
Label1.Text = " " +Session["nom"].ToString();

Label2.Text = "Bonjour "+ Session["nom"]+


" votre code est : " + Session["code"]+
", si mon TP 2 Session 24-04-2021";
}
else
{
//Response.Redirect("TP_2_login.aspx");
Response.Redirect("TP_2_login.aspx?message= pas autorise d'entre dans menu !!");

24
}

protected void Button1_Click(object sender, EventArgs e)


{
Session["nom"] =null;
Session["code"] = null;
Session["password"] = null;
Response.Redirect("TP_2_login.aspx?message=merci pour votre visite ");

}
}
}

Page Rechercher
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;
using System.Data;
namespace TP1_session
{
public partial class rechercheclient : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
try
{

string chainecon = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial


Catalog=TP_ASP;Integrated Security=True";
string sql = "SELECT code,nom,password FROM client where code =@code ";
SqlConnection con = new SqlConnection(chainecon);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);

cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@code", SqlDbType.Int).Value = TextBox1.Text;

SqlDataReader dr = cmd.ExecuteReader();

if (dr.Read()==true)
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";

TextBox2.Text = dr.GetValue(0).ToString();
TextBox3.Text = dr.GetValue(1).ToString();
TextBox4.Text = dr.GetValue(2).ToString();

25
}
else
{
Label2.Text = " ce client nexiste pas";

}
dr.Close();
con.Close();
}
catch (Exception ex)
{
Label2.Text = ex.Message;
}

}
}
}

Base de donnee
create database TP_ASP
USE TP_ASP
go

/*TP 1 23-04-2021*/
GO
create table client(code int primary key,nom varchar(20),password varchar(30))
GO
insert into client values(1,'ahmed','123'),
(2,'amine','456'),
(3,'khalid','789'),
(4,'fouad','4546'),
(5,'khadiga','756'),
(6,'aicha','897'),
(7,'ahmed','123'),
(8,'nabila','123')

insert into client values(9,'tounsy','123')


--delete client where code=7
SELECT * FROM client
SELECT code,nom,password FROM client where nom like 'ahmed' and password like '123'

11-27-04-2021

26
Page Rechercherparnom
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;
namespace TP1_session
{
public partial class Rechercherparnom : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(this.IsPostBack==false)
{
string chain = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True";
string sql = " SELECT * FROM client";
SqlConnection con = new SqlConnection(chain);
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader dr = cmd.ExecuteReader();
DataTable t = new DataTable();
t.Load(dr);
DropDownList1.DataTextField = "nom";
DropDownList1.DataValueField = "code";
DropDownList1.DataSource = t;
DropDownList1.DataBind();

con.Close();
}
}

protected void Button2_Click(object sender, EventArgs e)


{
Label3.Text = " <h1> Nom client : " +
DropDownList1.Items[DropDownList1.SelectedIndex].Text + " Code client :" +
DropDownList1.Items[DropDownList1.SelectedIndex].Value+ "<h1/>";
}

protected void Button1_Click(object sender, EventArgs e)

27
{
try
{
string chainecon = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True";
string sql = "SELECT * FROM client where code = @code";
SqlConnection con = new SqlConnection(chainecon);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlParameter p1 = new SqlParameter("@code",int.Parse(DropDownList1.SelectedValue));
cmd.Parameters.Add(p1);
SqlDataReader dr = cmd.ExecuteReader();
DataTable t = new DataTable();
t.Load(dr);
GridView1.DataSource = t;
GridView1.DataBind();
con.Close();

}
catch (Exception ex)
{
Label3.Text = ex.Message;
}
}
}
}

11-27-04-2021

Rechercherparnom
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;
namespace TP1_session
{
public partial class Rechercherparnom : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(this.IsPostBack==false)
{
string chain = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True";
string sql = " SELECT * FROM client";

28
SqlConnection con = new SqlConnection(chain);
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader dr = cmd.ExecuteReader();
DataTable t = new DataTable();
t.Load(dr);
DropDownList1.DataTextField = "nom";
DropDownList1.DataValueField = "code";
DropDownList1.DataSource = t;
DropDownList1.DataBind();

con.Close();
}
}

protected void Button2_Click(object sender, EventArgs e)


{
Label3.Text = " <h1> Nom client : " +
DropDownList1.Items[DropDownList1.SelectedIndex].Text + " Code client :" +
DropDownList1.Items[DropDownList1.SelectedIndex].Value+ "<h1/>";
}

protected void Button1_Click(object sender, EventArgs e)


{
try
{
string chainecon = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True";
string sql = "SELECT * FROM client where code = @code";
SqlConnection con = new SqlConnection(chainecon);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlParameter p1 = new SqlParameter("@code",int.Parse(DropDownList1.SelectedValue));
cmd.Parameters.Add(p1);
SqlDataReader dr = cmd.ExecuteReader();
DataTable t = new DataTable();
t.Load(dr);
GridView1.DataSource = t;
GridView1.DataBind();
con.Close();

}
catch (Exception ex)
{
Label3.Text = ex.Message;
}
}
}
}

29
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;
namespace TP1_session
{
public partial class SupprimerClient : System.Web.UI.Page
{
void DropDownList()
{
if (this.IsPostBack == false)
{
string chain = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True";
string sql = " SELECT * FROM client";
SqlConnection con = new SqlConnection(chain);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
DataTable t = new DataTable();
t.Load(dr);
DropDownList1.DataTextField = "nom";
DropDownList1.DataValueField = "code";
DropDownList1.DataSource = t;
DropDownList1.DataBind();

charger();
//Response.Redirect("SupprimerClient.aspx");
}
}
void charger()
{
string chainecon = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True";
string sql = "SELECT * FROM client";
SqlConnection con = new SqlConnection(chainecon);
con.Open();

30
SqlCommand cmd = new SqlCommand(sql, con);

SqlDataReader dr = cmd.ExecuteReader();
DataTable t = new DataTable();
t.Load(dr);
GridView1.DataSource = t;
GridView1.DataBind();
con.Close();
}

protected void Page_Load(object sender, EventArgs e)


{
if (this.IsPostBack == false)
{
DropDownList();
}
charger();

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

Panel1.Visible = true;
}

protected void Button4_Click(object sender, EventArgs e)


{
try
{
string chainecon = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True";
string sql = "DELETE FROM client where code = @code";
SqlConnection con = new SqlConnection(chainecon);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlParameter p1 = new SqlParameter("@code", int.Parse(DropDownList1.SelectedValue));
cmd.Parameters.Add(p1);
cmd.ExecuteNonQuery();

con.Close();
charger();
DropDownList();
Response.Redirect("SupprimerClient.aspx");
}
catch (Exception ex)
{
Label3.Text = ex.Message;
}
}

protected void Button5_Click(object sender, EventArgs e)


{
Panel1.Visible = false;
Response.Redirect("SupprimerClient.aspx");

31
}
}
}

TP 2 : site master

Page SupprimerClient
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;
namespace TP_site_master
{
public partial class SupprimerClient : System.Web.UI.Page
{
private void charger_les_clients()
{
string chaincon = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True";
string sql = " SELECT * FROM client";
SqlConnection cone = new SqlConnection(chaincon);
cone.Open();
SqlCommand comande = new SqlCommand(sql, cone);
SqlDataReader dr = comande.ExecuteReader();
DataTable t = new DataTable();
t.Load(dr);
GridView1.DataSource = t;
GridView1.DataBind();
cone.Close();

}
protected void Page_Load(object sender, EventArgs e)
{
charger_les_clients();
}
private bool existteClient( int code)
{
bool test = false;
string chaincon = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True";
string sql = " SELECT * FROM client where code=@code";

32
SqlConnection cone = new SqlConnection(chaincon);
cone.Open();
SqlCommand comande = new SqlCommand(sql, cone);
SqlParameter parcod = new SqlParameter("@code", code);
comande.Parameters.Add(parcod);
SqlDataReader dr = comande.ExecuteReader();
if(dr.Read()==true)
{
test = true;
}
cone.Close();
return test;
}
private void supprimerparcode(int code)
{
string chaincon = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True";
string sql = " DELETE FROM client where code=@code";
SqlConnection cone = new SqlConnection(chaincon);
cone.Open();
SqlCommand comande = new SqlCommand(sql, cone);
SqlParameter parcod = new SqlParameter("@code", code);
comande.Parameters.Add(parcod);
SqlDataReader dr = comande.ExecuteReader();
charger_les_clients();

}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
int code = int.Parse(TextBox1.Text);
bool test = existteClient(code);
if(test==false)
{
Label2.Text = "Client avec le code " + code.ToString() + " n existe pas";
}
else
{
supprimerparcode(code);
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}

}
}
}

33
Page listClient
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;

namespace TP_site_master
{
public partial class listClient : System.Web.UI.Page
{
private void charger_les_clients()
{
string chaincon = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=TP_ASP;Integrated Security=True";
string sql= " SELECT code,nom,len(nom) as 'nomber caractere' FROM client";
SqlConnection cone= new SqlConnection(chaincon);
cone.Open();
SqlCommand comande = new SqlCommand(sql, cone);
SqlDataReader dr = comande.ExecuteReader();
DataTable t = new DataTable();
t.Load(dr);
GridView1.DataSource = t;
GridView1.DataBind();
cone.Close();

protected void Page_Load(object sender, EventArgs e)


{

protected void Button1_Click(object sender, EventArgs e)


{
charger_les_clients();
}
}
}

34
12-28-04-2021-Mercredi_seance__control_utilisateur_ (à distance)

Exercices 1 : Création d'un contrôle Utilisateur web

using System;

35
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace Exercices_1_controle_Utilisateur_web
{
public partial class MenuCtrl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
AddMenuItem("Facebook", "http://www.facebook.com");
AddMenuItem("github.", "https://github.com/Rachidtounsy");
AddMenuItem("ofppt", "http://ofppt.info/");
AddMenuItem("Page Ajouter Client", "AjouterClient.aspx");

}
private void AddMenuItem(string linkName, string linkURL)
{
TableRow menuRow = new TableRow();
Table1.Rows.Add(menuRow);
TableCell menuCell = new TableCell();
BuildLink(menuCell, linkName, linkURL);
menuRow.Cells.Add(menuCell);
}
private void BuildLink(TableCell menuCell, string linkCaption, string
linkHRef)
{
HyperLink menuLink = new HyperLink();
menuLink.Text = linkCaption;
menuLink.NavigateUrl = linkHRef;
menuCell.Controls.Add(menuLink);
}
public Color BackGroundColor
{
get
{
return Table1.BackColor;
}
set
{
Table1.BackColor = value;
}
}
public string cssStyle
{
get
{ return Table1.BackColor; }
set
{ Table1.BackColor = value; }
}

}
}

----

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

36
using System.Web.UI.WebControls;
using System.Drawing;
namespace Exercices_1_controle_Utilisateur_web
{
public partial class AjouterClient : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// MenuCtrl1.couleurDuTableau = Color.Aqua;
MenuCtrl1.BackGroundColor = Color.Azure;
MenuCtrl1.cssStyle = "c1";
}
}
}

13-29-04-2021-TP_procedure (à distance)

1-procédure stocker pc1:


create procedure pc1
@code int ,@nom varchar(20) output
as
begin
if(exists(select nom from client where code=@code))
begin
set @nom=(select nom from client where code=@code)
end
else
begin
set @nom='aucune correspondance'
end
end
declare @s varchar(20)
exec pc1 2,@s output
print @s

Page ApppeProc

using System;
using System.Data;
using System.Data.SqlClient;
namespace TP_procedure
{

37
public partial class ApppeProc : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{ try
{

string chain = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial


Catalog=TP_ASP;Integrated Security=True";
SqlConnection con = new SqlConnection(chain);
SqlCommand cmd = new SqlCommand("pc1", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@code",SqlDbType.Int);
p1.Direction = ParameterDirection.Input;
SqlParameter p2 = new SqlParameter("@nom", SqlDbType.VarChar, 20);
p2.Direction = ParameterDirection.Output;
p1.Value = int.Parse(TextBox1.Text);
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
con.Open();
cmd.ExecuteNonQuery();
Label2.Text = "nom output =" + p2.Value.ToString();
con.Close();

}
catch (Exception ex)
{
Label2.Text = ex.Message;
}

}
}
}

14-04-05-2021-TP_procedure2 (à distance)
Appel de procedure stockee en c#(windowsform+webform)

1-procedure PC2

create procedure pc2


@a int
as
begin
select * from client where code=@a
end

exec pc2 5

38
webform a realise

Code button

protected void Button1_Click(object sender, EventArgs e)


{
String chaine = @"Data Source=.\SQLEXPRESS;Initial Catalog=clients;Integrated
Security=True";
SqlConnection con = new SqlConnection(chaine);
con.Open();
SqlCommand cmd = new SqlCommand("pc2", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter codeparameter = new SqlParameter("@a", SqlDbType.Int);
codeparameter.Direction = ParameterDirection.Input;
codeparameter.Value = Convert.ToInt32(TextBox1.Text);
cmd.Parameters.Add(codeparameter);
SqlDataReader dr = cmd.ExecuteReader();

DataTable t = new DataTable();


t.Load(dr);
GridView1.DataSource = t;
GridView1.DataBind();

con.Close();

2-procedure PC3

Create Procedure pc3(


@nomclient varchar(20),
@n Integer OUTPUT
)
AS
begin

39
select * from client
where nom=@nomclient

set @n = @@Rowcount
--nombre de ligne de la derniere requete
return convert(int ,getdate())

end

declare @x int
declare @r int
exec @r=pc3 'fouad',@x output
print @x
print @r

Interface graphique du webform :

Code button :

protected void Button1_Click(object sender, EventArgs e)


{
String chaine = @"Data Source=.\SQLEXPRESS;Initial Catalog=clients;Integrated
Security=True";
SqlConnection con = new SqlConnection(chaine);
con.Open();
SqlCommand cmd = new SqlCommand("pc3", con);
cmd.CommandType = CommandType.StoredProcedure;
//input
SqlParameter p1 = new SqlParameter("@nomclient", SqlDbType.VarChar, 20);
p1.Value = TextBox1.Text;
p1.Direction = ParameterDirection.Input;

//output
SqlParameter p2 = new SqlParameter("@n", SqlDbType.Int);
p2.Direction = ParameterDirection.Output;
//retvalue
SqlParameter valeurRetparametre= new SqlParameter("RetVal", SqlDbType.Int);
valeurRetparametre.Direction = ParameterDirection.ReturnValue;

cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(valeurRetparametre);
SqlDataReader dr=cmd.ExecuteReader();

while (dr.Read() == true)


{//select
string code = dr.GetValue(0).ToString();
string nom = dr.GetValue(1).ToString();
string password = dr.GetValue(2).ToString();
Label4.Text += " code :" + code + " | nom=" + nom + " | password: " + password;
}
dr.Close();
cmd.ExecuteNonQuery();

Label2.Text += " valeur =" + p2.Value.ToString();


Label3.Text += "retour=" + valeurRetparametre.Value.ToString() + " jours depuis 1900";

40
con.Close();

15-19-05-2021 : XML

TP 1 :
--19-05-2021
create table city (
id int primary key identity,
nameC varchar(50),
countryCode varchar(50),
poopulation float
)

--drop table city

insert into city values ('beni mellal', 'Mar', 500000)


insert into city values ('casablanca', 'Mar', 3000000)
insert into city values ('madrid', 'esp', 600000)

SELECT * FROM city

16-26-05-2021
TP 1 :

41
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;

namespace appGridViewSimple
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
chargegrid();
}

}
public void chargegrid()
{
String sql = "SELECT * FROM client ";

42
String ch = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=Db_client_ParGeidViewSimple;Integrated Security=True";
SqlConnection con = new SqlConnection(ch);
SqlDataAdapter dt = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
dt.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = GridView1.SelectedRow.Cells[3].Text;
TextBox2.Text = GridView1.SelectedRow.Cells[4].Text;
TextBox3.Text = GridView1.SelectedRow.Cells[5].Text;
TextBox4.Text = GridView1.SelectedRow.Cells[6].Text;

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)


{
String code = GridView1.Rows[e.RowIndex].Cells[3].Text;

String ch = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial


Catalog=Db_client_ParGeidViewSimple;Integrated Security=True";
String sql = "delete from client where code =@code ";
SqlConnection con = new SqlConnection(ch);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlParameter p1 = new SqlParameter("@code", int.Parse(code));
cmd.Parameters.Add(p1);
cmd.ExecuteNonQuery();
con.Close();
chargegrid();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)


{
GridView1.EditIndex = e.NewEditIndex;

chargegrid();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)


{
GridView1.EditIndex = -1;

chargegrid();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)


{
try
{

//Recupere les valeurs nouveaus des textbox temporaire


TextBox texCode = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
TextBox texNom = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
TextBox texPrenom = (TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0];
TextBox texsalaire = (TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0];

43
String ch = "Data Source=DESKTOP-R59K5PE\\SQLEXPRESS01;Initial
Catalog=Db_client_ParGeidViewSimple;Integrated Security=True";
String sql = "update client set nom=@nom , prenom=@prenom ,salaire=@salaire where code
=@code ";
SqlConnection con = new SqlConnection(ch);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlParameter p1 = new SqlParameter("@nom",texNom.Text);
SqlParameter p2 = new SqlParameter("@prenom", texPrenom.Text);
SqlParameter p3 = new SqlParameter("@salaire", float.Parse(texsalaire.Text));
SqlParameter p4 = new SqlParameter("@code", int.Parse(texCode.Text));
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(p3);
cmd.Parameters.Add(p4);

cmd.ExecuteNonQuery();
con.Close();
//
GridView1.EditIndex = -1;
chargegrid();
}
catch (Exception mess)
{
Label5.Text = mess.Message;
}
}

protected void Button1_Click(object sender, EventArgs e)


{
try
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-
R59K5PE\\SQLEXPRESS01;Initial Catalog=Db_client_ParGeidViewSimple;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into client values(@code,@nom,@prenom,@salaire)";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@code", SqlDbType.Int).Value = TextBox1.Text;
cmd.Parameters.AddWithValue("@nom", SqlDbType.VarChar).Value = TextBox2.Text;
cmd.Parameters.AddWithValue("@prenom", SqlDbType.VarChar).Value = TextBox3.Text;
cmd.Parameters.AddWithValue("@salaire", SqlDbType.Float).Value = TextBox4.Text;

int dr = cmd.ExecuteNonQuery();
if (dr == 0)
{
Label5.Text = " Atansion !!!!";

}
else
{
Label5.Text = " bien Ajouter ";
chargegrid();

}
con.Close();
}
catch (Exception ex)

44
{
Label5.Text = ex.Message;
}

}
}
}

45

You might also like