Mar.23
TP10 : Manipulation de plusieurs tables simultanément.
Objectif
Manipulation de plusieurs tables simultanément en mode connecté.
Questions
Partie serveur :
- Dans la base Gestion, ajoute les tables : Commande et Detail.

Partie client :
- 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, ses informations sont affichées.
- Au choix de ID d’un produit, ses informations sont affichées.
- Un clic sur le bouton Ajouter au panier fait descendre la ligne dans le DataGridView.
- Le bouton Ajouter au panier fait aussi le calcule le Montant de la ligne et le Total de la commande à chaque ajout de ligne.
- Le bouton Supprimer une ligne permet de supprimer une ligne sélectionnée dans le DataGridView et recalcule le Total de la commande.

- Lorsqu’il n’y a plus aucune modification à effectuer sur la commande, Un clic sur le bouton Enregistrer la commande permet d’enregistrer la commande et ses lignes (ses détails).
- Le bouton Enregistrer la commande permet aussi d’effacer le formulaire pour préparer la saisie d’une autre commande.
Correction
namespace TP10_Gestion_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 t = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listeClient();
listeProduit();
}
private void listeProduit()
{
try
{
cmd = new SqlCommand("select idProduit from produit", conn);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
idProduit.Items.Add(dr[0]);
}
idProduit.SelectedIndex = -1;
dr.Close();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
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 idClient_SelectedIndexChanged(object sender, EventArgs e)
{
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();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void idProduit_SelectedIndexChanged(object sender, EventArgs e)
{
if (idProduit.SelectedIndex != -1)
{
int id = int.Parse(idProduit.Text);
try
{
cmd = new SqlCommand("select * from produit where idProduit='" + id + "' ", conn);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
nomProduit.Text = dr[1].ToString();
prixProduit.Text = dr[2].ToString();
}
dr.Close();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
float m=int.Parse(qtitProduit.Text) * float.Parse(prixProduit.Text);
dv.Rows.Add(idProduit.Text,nomProduit.Text,prixProduit.Text,qtitProduit.Text,m.ToString());
t = t + m;
total.Text = t.ToString();
idProduit.SelectedIndex = -1;
nomProduit.Text = "";
prixProduit.Text = "";
qtitProduit.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
int i = dv.CurrentCell.RowIndex;
float m = float.Parse(dv.Rows[i].Cells[4].Value.ToString());
t = t - m;
total.Text = t.ToString();
dv.Rows.RemoveAt(i);
}
private void button3_Click(object sender, EventArgs e)
{
int idCm = int.Parse(idCommande.Text);
String dateCm = dateCommande.Value.ToString("yyyy-MM-dd");
int idCl = int.Parse(idClient.Text);
try
{
cmd = new SqlCommand("INSERT INTO commande VALUES(" + idCm + " , '" + dateCm + "'," + idCl + ")", conn);
conn.Open();
cmd.ExecuteNonQuery();
for (int i = 0; i < dv.Rows.Count; i++)
{
cmd = new SqlCommand("INSERT INTO detail VALUES(" + idCm + " , " + int.Parse(dv.Rows[i].Cells[0].Value.ToString()) + "," + int.Parse(dv.Rows[i].Cells[3].Value.ToString()) + ")", conn);
cmd.ExecuteNonQuery();
}
dr.Close();
MessageBox.Show("Les insertions ont été effectuées avec succè");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
dv.Rows.Clear();
idClient.SelectedIndex = -1;
nomClient.Text = "";
villeClient.Text = "";
idCommande.Text = "";
dateCommande.Text = DateTime.Now.Date.ToString();
}
}
}
