Salut a vous tous, j'ai un probleme au methode l'ajoute poo

Résolu
mohamedali1989 Messages postés 80 Date d'inscription jeudi 22 mars 2012 Statut Membre Dernière intervention 12 mars 2013 - 3 avril 2012 à 13:11
mohamedali1989 Messages postés 80 Date d'inscription jeudi 22 mars 2012 Statut Membre Dernière intervention 12 mars 2013 - 3 avril 2012 à 14:07
quant je click un bottun l'ajoute ile me affiché ce message:


Column 'IDUser' is constrained to be unique. Value '0' is already present.

sachant que le class qui contient des methods ceci :
using System;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace WindowsFormsApplication1
{
    class utili
    {
#region //instanciation dataaccess
        private dataaecces _access = null;
        public DataRow row;
        private dataaecces Access
        {
            get
            {
                if (this._access == null)
                {
                    this._access = new dataaecces();
                    this._access.GetData("utilisateur");
                }
                return this._access;
            }
        }

#endregion
     
        public utili() { }

        public int Id { get; set; }
        public string Nom { get; set; }
        public string mp { get; set; }

        #region // constructeur
        public utili(int id, string nom, string mpu)
        {
            this.Id = id;
            this.Nom = nom;
            this.mp = mpu;
        }
        #endregion


        #region //ajouter
        public void AddToTable(int id,string nom,string mp)
        {
            //Boolean t;
            //for (int i = 0; i <= Access.dTable.Rows.Count - 1; i++)
            //{
            //    if (row[0].ToString() == this.Id.ToString())
            //    {
            //        t = true;
            //    }
            //    else 
            //    {
            //        t = false;
            //    }

            //}
            //if (t == true)
            //{ 
                row = Access.dTable.NewRow();

            row[0] = this.Id;
            row[1] = this.Nom;
            row[2] = this.mp;

            this.Access.dTable.Rows.Add(row);
                
         
        }
        #endregion

        #region //excutenonquery
        public void Valider()
        {
            this.Access.DataAdapter.Update(this.Access.dTable);
        }
        #endregion

        #region //fonction afficher
        public Boolean afficher(string rech)
        {
            Boolean t=false;
            for(int i = 0; i <= Access.dTable.Rows.Count - 1;i++)
            {
                Access.row=Access.dTable.Rows[i];
           
                if (Access.row[0].ToString() == rech)
                {
                    t=true;
                }
                else 
                {
                    t=false;
                }
            }
            return t; 
        }
        #endregion //


        #region // modification
        public void modifier(int id, string nom, string mp)
        {
            DataRow r;
            Access.dTable.Clear();

            r = Access.dTable.Rows.Find(this.Id);

            Access.row.BeginEdit();
         r[0] = this.Id;
            r[1] = this.Nom;
            r[2] = this.mp;

            r.EndEdit();
            Access.valider();
            
        }
               #endregion //


        #region//supprimer
        public void supprimer(string s)
        {
            DataRow dr;
          dr= Access.dTable.Rows.Find(s);
            dr.Delete();
            Access.valider();
        }
        #endregion //
    }
}



et le form est :

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;

using Microsoft.VisualBasic;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        utili ut;
        public Form1()
        {
            InitializeComponent();
       
               ut = new utili();

        }
   
       
           private void Form1_Load(object sender, EventArgs e)
        {
                                        }

        private void button1_Click(object sender, EventArgs e)

        {

            
             ut.AddToTable(int.Parse(textBox1.Text),textBox2.Text ,textBox3.Text);
                      
             ut.Valider();
           
          
           
            
                             
        }

        private void button2_Click(object sender, EventArgs e)
        {
           
            if (ut.afficher(textBox1.Text) == true)
            {
                textBox1.Text = ut.row[0].ToString();
                textBox2.Text = ut.row[1].ToString();
                textBox3.Text = ut.row[2].ToString();
            }
            else
            {
                MessageBox.Show("n'exist");
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {

            ut.modifier(int.Parse(textBox1.Text), textBox2.Text, textBox3.Text);

            MessageBox.Show("");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ut.supprimer(textBox1.Text);
            MessageBox.Show("a ete supprimé");
        }
    }
}

3 réponses

mohamedali1989 Messages postés 80 Date d'inscription jeudi 22 mars 2012 Statut Membre Dernière intervention 12 mars 2013
3 avril 2012 à 14:07
je fais que vous me dire mai pas marché
3
cs_L0ci Messages postés 224 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 11 juin 2013 7
3 avril 2012 à 13:37
Le problème vient du fait que tu essaie d’insérer dans ta base de données (colonne ID) une valeur qui y est déjà.
Pense a incrémenter la valeur de l'id dans ton code ou mieux à configurer ta base de données pour insérer automatiquement l'id (option auto increment)
0
mohamedali1989 Messages postés 80 Date d'inscription jeudi 22 mars 2012 Statut Membre Dernière intervention 12 mars 2013
3 avril 2012 à 13:39
ok thank's, j'essaie et je te répondre
0
Rejoignez-nous