Mar.23
TP11 : Manipulation de plusieurs tables simultanément en mode connecté
Objectif
Manipulation de plusieurs tables simultanément et l’utilisation de plusieurs objets commande et datareader en mode connecté.
Questions
Partie serveur :
- Dans la base Gestion, ajoute les tables : Commande et Detail.

Partie client :
Pour terminer l’application Gestion Commerciale, Il reste à créer un formulaire pour une consultation totale des commandes d’un client en mode connecté.
- Créer l’interface suivante :

- Créer les objets nécessaires et le code réalisant le travail demandé.
- Au choix de ID d’un client :
- Le Nom et la Ville s’affichent automatiquement.
- La liste de toutes ses commandes s’affiche dans le 1er DataGridView.
- Son Chiffre d’affaires est calculé et affiché dans un TextBox. (Chiffre d’affaires d’un client = Somme des totaux de ses commandes).
- Lorsqu’une commande est sélectionnée (évènement double clic) :
- Ses détails sont affichés dans le 2eme DataGridView.
- Le Total HT de la commande, la TVA (20%) et le TTC sont calculés et affichés.

- Le bouton « Supprimer la commande » permet de supprimer une commande sélectionnée dans le 1er DataGridView. Le Chiffre d’affaires du client est recalculé.
- Au choix de ID d’un client :
Correction
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace TP11_Consultation_Vente
{
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection("server = DESKTOP-EIALG0J\\SQLEXPRESS; database = gestion; integrated security = SSPI");
SqlDataReader dr;
SqlCommand cmd;
BindingSource source = new BindingSource();
float chiffre;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listeClient();
nomClient.Enabled = false;
villeClient.Enabled = false;
chiffreAffaire.Enabled = false;
total.Enabled = false;
tva.Enabled = false;
ttc.Enabled = false;
}
private void listeClient()
{
try
{
cmd = new SqlCommand("select ldClient from client", conn);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
idClient.Items.Add(dr[0]);
}
idClient.SelectedIndex = -1;
dr.Close();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void idClient_SelectedIndexChanged(object sender, EventArgs e)
{
dataGridView2.Rows.Clear();
total.Text = "";
tva.Text = "";
ttc.Text = "";
if (idClient.SelectedIndex != -1)
{
int id = int.Parse(idClient.Text);
try
{
cmd = new SqlCommand("select * from client where ldClient='" + id + "' ", conn);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
nomClient.Text = dr[1].ToString();
villeClient.Text = dr[2].ToString();
}
dr.Close();
dataGridView1.Rows.Clear();
cmd = new SqlCommand("select idCommande as 'id', CONVERT(VARCHAR(10), CAST(dateCommande AS DATE), 105) AS 'date' from commande where ldClient='" + id + "'", conn);
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
dataGridView1.Rows.Add(dr[0],dr[1]);
}
}
dr.Close();
cmd = new SqlCommand("select prixProduit, quantite from detail, Commande,produit where Detail.idProduit=produit.idProduit and detail.idCommande=Commande.idCommande and ldClient=" + id + "", conn);
dr = cmd.ExecuteReader();
chiffre = 0;
if (dr.HasRows)
{
while (dr.Read())
{
chiffre = chiffre + float.Parse(dr[0].ToString()) * int.Parse(dr[1].ToString());
}
}
chiffreAffaire.Text = chiffre.ToString();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
int i = dataGridView1.CurrentCell.RowIndex;
int id = int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
try
{
cmd = new SqlCommand("select detail.idProduit, nomProduit, prixProduit, quantite from detail, produit where detail.idProduit=produit.idProduit and idcommande=" + id + " ", conn);
conn.Open();
dr = cmd.ExecuteReader();
dataGridView2.Rows.Clear();
float montant = 0;
if (dr.HasRows)
{
while (dr.Read())
{
float mq = float.Parse(dr[2].ToString()) * int.Parse(dr[3].ToString());
dataGridView2.Rows.Add(dr[0], dr[1], dr[2], dr[3],mq);
montant = montant + mq;
}
total.Text = montant.ToString();
//Exemple, avec une TVA à 20 % :
tva.Text = (montant * 0.2).ToString();
ttc.Text = (montant * 1.2).ToString();
}
dr.Close();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count > 0) {
int i = dataGridView1.CurrentCell.RowIndex;
int id = int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
try
{
cmd = new SqlCommand("delete from detail where idcommande =" + id + "", conn);
conn.Open();
cmd.ExecuteNonQuery();
cmd = new SqlCommand("delete from commande where idcommande =" + id + "", conn);
cmd.ExecuteNonQuery();
dataGridView2.Rows.Clear();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
idClient_SelectedIndexChanged(null, null);
MessageBox.Show("La suppression a été effectuée avec succè");
}
}
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
}
}
}
