nintendomaniac Gt: Xxjuni2lyonxX | Bonjour,
je doit pour mardi prochain créé une application de gestion d'une base de données Access.
Toute l'application fonctionne, sauf la recherche par adresse mac où j'ai le message d'erreur prévu qui apparait.
Voici le code source
Code :
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.OleDb;
- namespace TestBDDCruas
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- #region variable pour la connection a la base de donnée
- //déclaration de la connection
- private OleDbConnection Conn = new OleDbConnection();
- //déclaration du chemin de l'éxécutable
- private string BDDPath = Application.ExecutablePath;
- //déclaration du chemin de la connection de la base de donnée
- private string ConnString;
- #endregion
- #region variables Connection pour ComboClass et listclass
- //déclaration du dataset
- private DataSet DtsClass = new DataSet();
- //déclaration de la datatable
- private DataTable DttClass = new DataTable();
- //déclaration de la requete sql pour le comboclass
- private string SqlClass = "SELECT * FROM Switchs";
- #endregion
- #region variables connection pour la datagrid et recherche
- //déclaration du dataset
- private DataSet DtsGrid = new DataSet();
- //déclaration du datatable
- private DataTable DttGrid = new DataTable();
- //déclaration de la requete sql
- private string SqlGrid = "SELECT * FROM Switchs";
- //déclaration de la requete sql
- private string SqlSearch;
- //déclaration du commande builder
- private OleDbCommandBuilder CmdGrid = new OleDbCommandBuilder();
- //déclaration du datarow
- private DataRow DtrGrid;
- #endregion
- #region variables
- //Import de la class Variable
- ClassVariable CsV = new ClassVariable();
- #endregion
- //-------------------------------------------------------------------------
- private void Form1_Load(object sender, EventArgs e)
- {
- // TODO : cette ligne de code charge les données dans la table 'cruasDataSet.Switchs'. Vous pouvez la déplacer ou la supprimer selon vos besoins.
- this.switchsTableAdapter.Fill(this.cruasDataSet.Switchs);
- #region Construction de la connection
- //Arangement du chemin de la prise de la base de donnée
- this.BDDPath = this.BDDPath.Substring(0, this.BDDPath.LastIndexOf("\\" ));
- //on crée le chemin de la connection a la base de donnée
- this.ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + this.BDDPath + "\\Cruas.mdb";
- //on crée la connection a la base de donnée
- this.Conn.ConnectionString = this.ConnString;
- #endregion
- //on ouvre la connection
- this.Conn.Open();
- //---------------------------------------------------------------------------
- #region comboLieu, listLieu; comboModele et listModele;
- //déclaration du data adapter pour le combobox
- OleDbDataAdapter Dta = new OleDbDataAdapter(this.SqlClass, this.Conn);
- //on leve l'exception
- try
- {
- //on rempli la dataset avec la table
- Dta.Fill(this.DtsClass, "Switchs" );
- }
- catch (Exception)
- {
- //affichage de l'érreur
- MessageBox.Show("Une Erreur est survenue, si cela persiste veuillez contacter au plus vite le développeur " );
- //on ferme la connection
- this.Conn.Close();
- //on ferme la fenêtre
- this.Close();
- //on quitte l'application
- Application.Exit();
- }
-
- #endregion
- //-----------------------------------------------------------------------
- #region datagridview
- //déclaration du data adapteur pour le datagridview
- OleDbDataAdapter DtaGrid = new OleDbDataAdapter(this.SqlGrid, this.Conn);
- //on leve l'exception
- try
- {
- DtaGrid.Fill(this.DtsGrid, "Switchs" );
- }
- catch (Exception)
- {
- //affichage de l'érreur
- MessageBox.Show("Une Erreur est survenue, si cela persiste veuillez contacter au plus vite le programmeur" );
- //on ferme la connection
- this.Conn.Close();
- //on quitte l'application
- Application.Exit();
- }
- //On rempli la liste avec le dataset
- this.DttGrid = this.DtsGrid.Tables["Switchs"];
- //on affiche la liste dans le datagridview
- this.Dtg.DataSource = this.DttGrid;
- //On stylise la datagridview
- this.StyleDtg();
- #endregion
- //Rafraichissement de la fenetre
- this.Show();
- //On ferme la connection
- this.Conn.Close();
- }
-
- #region buttons
- private void ButtonSave_Click(object sender, EventArgs e)
- {
- //On vérifie que tout les champs sont remplis
- if (this.TxtNumeroPI.Text == null || this.TxtNumeroPI.Text == null || this.comboModele.Text == null || this.comboLieu.Text == null)
- {
- MessageBox.Show ( "Veuillez remplir tout les champs" );
- }
- else
- {
- //déclaration du data adapter
- OleDbDataAdapter Dta = new OleDbDataAdapter ( this.SqlGrid , this.Conn );
- //création d'une nouvelle ligne
- this.DtrGrid = this.DtsGrid.Tables [ "Switchs" ].NewRow ();
- //remplissage des champs
- this.DtrGrid [ "N°" ] = this.DtsGrid.Tables [ "Switchs" ].Rows.Count + 1;
- this.DtrGrid [ "NumeroPI" ] = this.CsV.NumeroPI;
- this.DtrGrid [ "AdresseMAC" ] = this.CsV.AdresseMAC;
- this.DtrGrid [ "Modele" ] = this.CsV.Modele;
- this.DtrGrid ["Lieu"] = this.CsV.Lieu;
- //ajout de la ligne à la table
- this.DtsGrid.Tables [ "Switchs" ].Rows.Add ( DtrGrid );
- //on met tout dans la base de donnée
- this.CmdGrid = new OleDbCommandBuilder ( Dta );
- Dta.UpdateCommand = CmdGrid.GetUpdateCommand ();
- Dta.Update ( DtsGrid , "Switchs" );
- this.TxtNumeroPI.Text = null;
- this.TxtAdresseMAC.Text = null;
- this.comboModele.Text = null;
- this.comboLieu.Text = null;
- }
- }
-
- private void buttonModif_Click(object sender, EventArgs e)
- {
- if (this.CsV.Ligne < this.DtsGrid.Tables["Switchs"].Rows.Count)
- {
- if (this.CsV.Ligne >= 0)
- {
- //on charge les données dans les variables
- this.CsV.NumeroPI = this.DtsGrid.Tables["Switchs"].Rows[this.CsV.Ligne]["NumeroPI"].ToString();
- this.CsV.AdresseMAC = this.DtsGrid.Tables["Switchs"].Rows[this.CsV.Ligne]["AdresseMAC"].ToString();
- this.CsV.Modele = this.DtsGrid.Tables["Switchs"].Rows[this.CsV.Ligne]["Modele"].ToString();
- this.CsV.Lieu = this.DtsGrid.Tables["Switchs"].Rows[this.CsV.Ligne]["Lieu"].ToString();
- //on met les données des variables dans les textbox
- this.TxtNumeroPI.Text = this.CsV.NumeroPI;
- this.TxtAdresseMAC.Text = this.CsV.AdresseMAC;
- this.comboModele.Text = this.CsV.Modele;
- this.comboLieu.Text = this.CsV.Lieu;
- }
- else
- {
- MessageBox.Show("Il n'y a pas d'enregistrements" );
- }
- }
- }
-
- private void buttonUpdateModif_Click(object sender, EventArgs e)
- {
- //déclaration du data adapter
- OleDbDataAdapter Dta = new OleDbDataAdapter ( this.SqlGrid , this.Conn );
- //on leve l'exception
- try
- {
- //on prend la ligne en cour
- this.DtrGrid = this.DtsGrid.Tables [ "Switchs" ].Rows [ this.CsV.Ligne ];
- }
- catch ( IndexOutOfRangeException )
- {
- this.CsV.Ligne = 0;
- }
- //on vérifie que les txtbox ne sont pas vides
- if ( TxtNumeroPI.Text == null || TxtAdresseMAC.Text == null )
- {
- this.CsV.Ligne = 0;
- }
- else
- {
- //on rempli les champs
- this.DtrGrid [ "NumeroPI" ] = this.CsV.NumeroPI;
- this.DtrGrid [ "AdresseMAC" ] = this.CsV.AdresseMAC;
- this.DtrGrid [ "Modele" ] = this.CsV.Modele;
- this.DtrGrid ["Lieu"] = this.CsV.Lieu;
- }
- //on leve l'exception
- try
- {
- //on met tout dans la base de donnée
- this.CmdGrid = new OleDbCommandBuilder ( Dta );
- Dta.UpdateCommand = CmdGrid.GetUpdateCommand ();
- Dta.Update ( DtsGrid , "Switchs" );
- }
- catch (DBConcurrencyException /*DBCEx*/ )
- {
- MessageBox.Show ( "Vous ne pouvez pas modifier quelque chose que vous venez de sauvegarder, veuillez redémarer l'application et recommencer" );
- }
- TxtNumeroPI.Text = null;
- TxtAdresseMAC.Text = null;
- comboModele.Text = null;
- comboLieu.Text = null;
- }
- private void buttonAfter_Click(object sender, EventArgs e)
- {
- //on défini le nombre de lignes
- this.CsV.NbSauvegarde = this.DtsGrid.Tables["Switchs"].Rows.Count;
- //si il n'y a rien dans les textbox alors il faut aller a la premiere ligne
- if (this.TxtNumeroPI.Text == null || this.TxtAdresseMAC.Text == null || this.comboModele.Text == null || this.comboLieu.Text == null)
- {
- this.CsV.Ligne = 0;
- }
- else
- {
- //on saute une ligne a chaques click
- this.CsV.Ligne = this.CsV.Ligne + 1;
- //on ne dépasse pas le dernier enregistrement
- if (this.CsV.Ligne > this.CsV.NbSauvegarde - 1)
- {
- this.CsV.Ligne = this.CsV.NbSauvegarde - 1;
- }
- }
- //on vérifi qu'il y a un enregistrement
- if ( this.CsV.Ligne < 0 )
- {
- this.CsV.Ligne = 0;
- }
- else
- {
- //on charge les données dans les variables
- this.CsV.NumeroPI = this.DttGrid.Rows [ this.CsV.Ligne ] [ "NumeroPI" ].ToString ();
- this.CsV.AdresseMAC = this.DttGrid.Rows [ this.CsV.Ligne ] [ "AdresseMAC" ].ToString ();
- this.CsV.Modele = this.DttGrid.Rows [ this.CsV.Ligne ] [ "modele" ].ToString ();
- this.CsV.Lieu = this.DttGrid.Rows[this.CsV.Ligne]["Lieu"].ToString();
- //on charge les données dans les textbox
- this.TxtNumeroPI.Text = this.CsV.NumeroPI;
- this.TxtAdresseMAC.Text = this.CsV.AdresseMAC;
- this.comboModele.Text = this.CsV.Modele;
- this.comboLieu.Text = this.CsV.Lieu;
- }
- }
- private void buttonBefore_Click(object sender, EventArgs e)
- {
- //si il n'y a rien dans les textbox, alors il faut aller à la première ligne
- if (this.TxtNumeroPI.Text == null || this.TxtAdresseMAC.Text == null || this.comboModele.Text == null || this.comboLieu.Text == null)
- {
- this.CsV.Ligne = 0;
- }
- else
- {
- // On saute une ligne a chaques click
- this.CsV.Ligne = this.CsV.Ligne - 1;
- }
- // On vérifie qu'il y à un enregistrement et,qu'on ne dépasse pas le premier enregistrement
- if ( this.CsV.Ligne < 0 )
- {
- this.CsV.Ligne = 0;
- }
- else
- {
- //on charge les données dans les variables
- this.CsV.NumeroPI = this.DttGrid.Rows [ this.CsV.Ligne ] [ "NumeroPI" ].ToString ();
- this.CsV.AdresseMAC = this.DttGrid.Rows [ this.CsV.Ligne ] [ "AdresseMAC" ].ToString ();
- this.CsV.Modele = this.DttGrid.Rows [ this.CsV.Ligne ] [ "Modele" ].ToString ();
- this.CsV.Lieu = this.DttGrid.Rows[this.CsV.Ligne]["Lieu"].ToString();
- //On charge les données dans les textbox
- this.TxtNumeroPI.Text = this.CsV.NumeroPI;
- this.TxtAdresseMAC.Text = this.CsV.AdresseMAC;
- this.comboModele.Text = this.CsV.Modele;
- this.comboLieu.Text = this.CsV.Lieu;
- }
- }
-
- private void buttonDelete_Click(object sender, EventArgs e)
- {
- // Déclaration du data adapter
- OleDbDataAdapter Dta = new OleDbDataAdapter(this.SqlGrid, this.Conn);
- //on vérifi qu'il y a un enregistrement
- if (this.CsV.Ligne < 0)
- {
- this.CsV.Ligne = 0;
- }
- // On vérifie qu'il y à quelque chose dans les textbox
- else if (this.TxtNumeroPI.Text == null || this.TxtAdresseMAC.Text == null)
- {
- this.CsV.Ligne = 0;
- }
- else
- {
- // On prend la ligne en cours et on la supprime
- this.DtsGrid.Tables["Switchs"].Rows[this.CsV.Ligne].Delete();
- }
- //on va a la ligne précédente
- this.CsV.Ligne = this.CsV.Ligne - 1;
- // On ne dépasse pas le dernier enregistrement
- if (this.CsV.Ligne < 0)
- {
- this.CsV.Ligne = 0;
- }
- // On lève l'exception
- try
- {
- // On met à jour la base de donnée
- this.CmdGrid = new OleDbCommandBuilder(Dta);
- Dta.UpdateCommand = CmdGrid.GetUpdateCommand();
- Dta.Update(DtsGrid, "Switchs" );
- }
- catch (DBConcurrencyException)
- {
- MessageBox.Show("Vous ne pouvez pas modifier quelque chose que vous venez de sauvegarder, veuillez redémarer l'application et recommencer" );
- }
- // On suprime les données dans les textbox
- TxtNumeroPI.Text = "";
- TxtAdresseMAC.Text = "";
- comboModele.Text = "";
- comboLieu.Text = "";
- }
-
- private void buttonExit_Click(object sender, EventArgs e)
- {
- this.Conn.Close();
- this.Close();
- Application.Exit();
- }
- private void buttonsearch_Click(object sender, EventArgs e)
- {
- // On vide le dataset
- this.DtsGrid.Clear();
- // On vérifie qu'il y à quelque chose dans la txtsearch
- if (this.TxtSearch.Text == null)
- {
- // On choisi la requete sql
- this.SqlSearch = this.SqlGrid;
- }
- else if (this.RadNumeroPI.Checked == true)
- {
- // On choisi la requete sql
- this.SqlSearch = "SELECT Switchs.N°,Switchs.NumeroPI, Switchs.AdresseMAC, Switchs.Modele, Switchs.Lieu FROM Switchs WHERE (Switchs.NumeroPI LIKE '%" + this.CsV.Recherche + "%')";
- }
- else if (this.radAdresseMAC.Checked == true)
- {
- // On choisi la requete sql
- this.SqlSearch = "SELECT Contact.N°,Switchs.NumeroPI,Switchs.AdresseMAC, Switchs.Modele, Switchs.Lieu FROM Switchs WHERE (Switchs.AdresseMAC LIKE '%" + this.CsV.Recherche + "%')";
- }
- else if (this.radModele.Checked == true)
- {
- // On choisi la requete sql
- this.SqlSearch = "SELECT Switchs.N°,Switchs.NumeroPI, Switchs.AdresseMAC, Switchs.Modele, Switchs.Lieu FROM Switchs WHERE (Switchs.Modele LIKE '%" + this.CsV.Recherche + "%')";
- }
- else if (this.radLieu.Checked == true)
- {
- // On choisi la requete sql
- this.SqlSearch = "SELECT Switchs.N°,Switchs.NumeroPI, Switchs.AdresseMAC, Switchs.Modele, Switchs.Lieu FROM Switchs WHERE (Switchs.Lieu LIKE '%" + this.CsV.Recherche + "%')";
- }
- else if (this.radTout.Checked == true)
- {
- // On choisi la requete sql
- this.SqlSearch = "SELECT Switchs.N°,Switchs.NumeroPI, Switchs.AdresseMAC, Switchs.Modele, Switchs.Lieu FROM Switchs" ;
- }
- else if (this.RadNumeroPI.Checked == false && this.radAdresseMAC.Checked == false && this.radModele.Checked == false)
- {
- // On choisi la requete sql
- this.SqlSearch = this.SqlGrid;
- // On met un message d'alerte
- MessageBox.Show("Veuillez choisir un critère de sélection dans la base de donnée pour la recherche", "Un oublie", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- else
- {
- //on choisi la requete sql
- this.SqlSearch = this.SqlGrid;
- }
- //on ouvre la connection
- this.Conn.Open();
- //déclaration du data adapter
- OleDbDataAdapter Dta = new OleDbDataAdapter(this.SqlSearch, this.Conn);
- //on leve l'exception
- try
- {
- // On rempli le dataset avec la table
- Dta.Fill(this.DtsGrid, "Switchs" );
- }
- catch (Exception)
- {
- //affichage de l'erreur
- MessageBox.Show("Une Erreur est survenue, si cela persiste veuillez contacter au plus vite le programmeur" );
- //on ferme la connection
- this.Conn.Close();
- //on ferme la fenêtre
- //this.Close();
- //on quitte l'application
- Application.Exit();
- }
- //on rempli la liste du datagridview avec le dataset
- this.DttGrid = this.DtsGrid.Tables["Switchs"];
- //on affiche la liste du datagridview dans le datagridview
- this.Dtg.DataSource = this.DttGrid;
- //on stylise la datagridview
- this.StyleDtg();
- //rafraichissement de la fenetre
- //this.Show();
- //on ferme la connection
- this.Conn.Close();
- //on vide la TxtSearch pour commencer une nouvelle recherche
- this.TxtSearch.Text = null;
- }
- #endregion
-
- #region Fonctions
- private void StyleDtg()
- {
- this.Dtg.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 9, FontStyle.Bold, GraphicsUnit.Point);
- this.Dtg.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.ControlDark;
- this.Dtg.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
- this.Dtg.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
- this.Dtg.DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point);
- this.Dtg.DefaultCellStyle.BackColor = Color.Empty;
- this.Dtg.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.ControlLight;
- this.Dtg.CellBorderStyle = DataGridViewCellBorderStyle.Single;
- this.Dtg.GridColor = SystemColors.ControlDarkDark;
- Dtg.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
- }
- #endregion
- #region TextBox
- //TxtNom_TextChanged
- private void TxtNumeroPI_TextChanged(object sender, EventArgs e)
- {
- this.CsV.NumeroPI = this.TxtNumeroPI.Text;
- }
- //TxtAdresseMAC_TextChanged
- private void TxtAdresseMAC_TextChanged(object sender, EventArgs e)
- {
- this.CsV.AdresseMAC = this.TxtAdresseMAC.Text;
- }
- // comboModele_SelectedIndexChanged
- private void comboModele_SelectedIndexChanged(object sender, EventArgs e)
- {
- this.CsV.Modele = this.comboModele.Text;
- }
- private void comboLieu_SelectedIndexChanged(object sender, EventArgs e)
- {
- this.CsV.Lieu = this.comboLieu.Text;
- }
- private void TxtSearch_TextChanged(object sender, EventArgs e)
- {
- this.CsV.Recherche = this.TxtSearch.Text;
- }
- #endregion
- }
- }
|
Merci |