IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

Modification et recherche


Sujet :

Windows Forms

  1. #1
    Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Août 2012
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2012
    Messages : 2
    Points : 3
    Points
    3
    Par défaut Modification et recherche
    Bonjour;

    j'ai un problème concernant la mise a jours et la modification des données au niveau d'une database. en fait la modification ne peut être réalisée et enregistrée que en arrêtant l’exécution et la relancée encore une fois.

    aussi en ce qui concerne la recherche ; ce que je doit faire c'est la recuperation toute la ligne en tapant le nom, le prenom, et le CIN mais ce qui bizarre c'est que la recherche se fait par rapport au prenom et le CIN et non pas le Nom (meme qu'il soit erroné)

    MERCI de me répondre.

    CODE DE LA RECHERHCE


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
      public partial class Recherche : Form
        {
            public Recherche()
            {
                InitializeComponent();
            }
     
            SqlConnection cs = new SqlConnection("Data Source = user-tosh\\Sqlexpress ;database=yosra;User ID =sa;Password=med;");
     
            DataTable dt = new DataTable();
     
     
            private void txtid_TextChanged(object sender, EventArgs e)
            {
     
            }
     
            private void Afficher_Click(object sender, EventArgs e)
            {
     
                String ch = txtNom.Text;
     
                String sh = txtPrenom.Text;
     
                String fh = txtCIN.Text;
     
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
     
                SqlConnection conn = new SqlConnection("Data Source = user-tosh\\Sqlexpress ;database=yosra;User ID =sa;Password=med;");
                //
     
                String ch1 = String.Concat("Select * FROM sagem where Nom='", ch, "'");
     
                da.SelectCommand = new SqlCommand(ch1, conn);
     
                //
     
                 String ch2 = String.Concat("Select * FROM sagem where Prenom='", sh, "'");
     
     
     
               da.SelectCommand = new SqlCommand(ch2, conn);
                //
     
                 String ch3 = String.Concat("Select * FROM sagem where CIN='", fh, "'");
     
     
     
     
     
     
                 da.SelectCommand = new SqlCommand(ch3, conn);
     
     
                ds.Clear();
                da.Fill(ds);
     
                dg.DataSource = ds.Tables[0];
            }

    CODE DE LA MODIFICATION

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
     
        public partial class Modification : Form
        {
            DataSet ds = new DataSet();
     
     
            SqlConnection conn = new SqlConnection("Data Source = user-tosh\\Sqlexpress ;database=yosra;User ID =sa;Password=med;");
     
            SqlDataAdapter da = new SqlDataAdapter();
     
            BindingSource tblNamesBS = new BindingSource();
     
            public Modification()
            {
                InitializeComponent();
            }
     
     
     
            // afiichage dans la datagridview
     
            private void Afficher_Click(object sender, EventArgs e)
            {
     
                SqlConnection conn = new SqlConnection("Data Source = user-tosh\\Sqlexpress ;database=yosra;User ID =sa;Password=med;");
     
                SqlDataAdapter da = new SqlDataAdapter();
     
                DataSet ds = new DataSet();
                da.SelectCommand = new SqlCommand("SELECT * from sagem", conn);
     
                ds.Clear();
                da.Fill(ds);
     
                dg.DataSource = ds.Tables[0];
     
                tblNamesBS.DataSource = ds.Tables[0];
     
                txtNom.DataBindings.Add(new Binding("Text", tblNamesBS, "Nom"));
                txtPrenom.DataBindings.Add(new Binding("Text", tblNamesBS, "Prenom"));
                txtCIN.DataBindings.Add(new Binding("Text", tblNamesBS, "CIN"));
                  txtID.DataBindings.Add(new Binding("Text", tblNamesBS, "ID"));
     
     
            }
     
            private void precedent_Click(object sender, EventArgs e)
            {
                tblNamesBS.MovePrevious();
                dgUpdate();
     
            }
     
            private void suivant_Click(object sender, EventArgs e)
            {
                tblNamesBS.MoveNext();
                dgUpdate();
     
            }
     
     
            private void dgUpdate()
            {
                dg.ClearSelection();
                dg.Rows[tblNamesBS.Position].Selected = true;
            }
     
     
     
            // Modification 
            private void button2_Click(object sender, EventArgs e)
            {
     
     
                conn.Open();
                da.UpdateCommand = new SqlCommand("UPDATE sagem SET Nom= @Nom, Prenom= @Prenom, CIN= @CIN  WHERE ID=@ID", conn);
     
                //da.UpdateCommand.Parameters.Add("@Nom", SqlDbType.VarChar).Value = dg.SelectedCells[0].OwningRow.Cells["Nom"].Value;
                //da.UpdateCommand.Parameters.Add("@Prenom", SqlDbType.VarChar).Value = dg.SelectedCells[0].OwningRow.Cells["Prenom"].Value;
                //da.UpdateCommand.Parameters.Add("@CIN", SqlDbType.Char).Value = dg.SelectedCells[0].OwningRow.Cells["CIN"].Value;
     
                da.UpdateCommand.Parameters.Add("@Nom", SqlDbType.VarChar).Value = txtNom.Text;
                da.UpdateCommand.Parameters.Add("@Prenom", SqlDbType.VarChar).Value = txtPrenom.Text;
                da.UpdateCommand.Parameters.Add("@CIN", SqlDbType.Char).Value = txtCIN.Text;
                da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value =txtID.Text;
     
     
                da.UpdateCommand.ExecuteNonQuery();  
                conn.Close();
     
     
     
     
     
     
            }

  2. #2
    Membre régulier
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Janvier 2011
    Messages
    71
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Drôme (Rhône Alpes)

    Informations professionnelles :
    Activité : Responsable de service informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2011
    Messages : 71
    Points : 110
    Points
    110
    Par défaut
    Salut,

    Plusieurs remarques :
    Pour la recherche, pourquoi faire plusieurs ch1, ch2 et ch3. Clairement, c'est ch3 qui est pris pour ton da.SelectCommand. Du coup, ch1 et ch2 ne servent à rien.
    Ensuite, utilise des parametres sql (ex : @Prenom) au lieu du concat. Premièrement, cela permet de ne pas avoir d'injection sql. par eexemple, avec ta technique, saisie dans ton control txtCIN la chaine :
    ABC' OR ''='
    Du coup ta requete sql devient :
    Select * FROM sagem where CIN='ABC' OR ''=''
    Ce qui du coup affiche toutes les lignes de ta table.
    De plus, il suffit qu'un jour tu est un Nom avec un apostrophe et tu auras une belle erreur SQL.

    Pour la modification, essaie de tester si ta connection n'est pas déjà ouverte.
    Essaie d'être plus clair, je ne cerne pas très bien ton souci. Tu as un message d'erreur ? En arrêtant l'exécution de quoi ?

Discussions similaires

  1. [SP-2010] Modification code recherche avancée
    Par diabli73 dans le forum SharePoint
    Réponses: 3
    Dernier message: 07/12/2012, 12h25
  2. [Débutant] modification et recherche dans une dataset
    Par momedalhouma dans le forum Accès aux données
    Réponses: 0
    Dernier message: 01/06/2012, 18h26
  3. Modif site, recherche étudiant ou pro bon marché
    Par tovis dans le forum ASP.NET
    Réponses: 5
    Dernier message: 09/08/2010, 13h05
  4. Recherche dans un fichier et modification
    Par kephy dans le forum Entrée/Sortie
    Réponses: 6
    Dernier message: 20/09/2005, 16h46
  5. [langage] recherche exactement un modif
    Par toto_titi dans le forum Langage
    Réponses: 3
    Dernier message: 04/07/2003, 11h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo