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 :

[MSG-ERREUR] la réference d'objet n'ai pas définie a une instance d'un objet - ?


Sujet :

Windows Forms

  1. #1
    Membre régulier
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Octobre 2005
    Messages
    77
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2005
    Messages : 77
    Points : 83
    Points
    83
    Par défaut [MSG-ERREUR] la réference d'objet n'ai pas définie a une instance d'un objet - ?
    Bonjour a tous,

    Voila je souhaite faire un update des valeurs contenu dans un dataset à la suite de modification dans un datagrid.

    Voici le code utilisé(voir ci dessous), néanmoins, il me mets le message d'erreur " [QUESTION] la réference d'objet n'ai pas définie a une instance d'un objet" au moment de l'update. . .

    quelqu'un aurait une idée ?

    merci d'avance

    namespace WindowsApplication1
    {
    public partial class Form1 : Form
    {

    #region init

    private SqlConnection connect;
    private string connection1;
    private DataSet ds = new DataSet();
    private CurrencyManager CM;
    private SqlDataAdapter MonAdapter;
    private SqlCommand CmdSelect, CmdInsert, CmdUpdate, CmdDelete;

    #endregion

    #region Constructeur
    public Form1()
    {
    InitializeComponent();
    try
    {
    connection1 = @"Data Source=sept;Initial Catalog=db1;User ID=gstock;password=gst01;Trusted_connection=False";
    connect = new SqlConnection(connection1);

    //-
    ds.Tables.Add("Liste_article");

    }
    catch (Exception xcp)
    {
    MessageBox.Show(xcp.Message);
    }

    }
    #endregion

    #region Button1 - Connection/Rq Simple
    private void button1_Click(object sender, EventArgs e)
    {
    try
    {
    CmdSelect = new SqlCommand("SELECT ref_art, nom_art, designation_art FROM articles", connect);
    CmdSelect.CommandType = System.Data.CommandType.Text;
    //-
    MonAdapter = new SqlDataAdapter(CmdSelect);
    //-
    ds.Clear();
    //-
    MonAdapter.Fill(ds.Tables["Liste_article"]);
    CM = (CurrencyManager)BindingContext[ds.Tables["Liste_article"]];
    //-
    dataGridView1.DataSource = ds.Tables["Liste_article"];
    }
    catch (Exception xcp)
    {
    MessageBox.Show(xcp.Message);
    }

    }
    #endregion

    #region Button2 - DataAdapter
    private void button2_Click(object sender, EventArgs e)
    {
    try
    {
    CmdSelect = new SqlCommand("SELECT id_art, ref_art, nom_art, designation_art FROM articles", connect);
    CmdSelect.CommandType = System.Data.CommandType.Text;
    //-
    CmdUpdate = new SqlCommand("UPDATE articles SET ref_art = @refart, nom_art = @nomart, designation_art = @designationart WHERE id_art = @idart", connect);
    CmdUpdate.Parameters.Add(new SqlParameter("@refart", SqlDbType.VarChar, 50, "ref_art"));
    CmdUpdate.Parameters.Add(new SqlParameter("@nomart", SqlDbType.VarChar, 50, "nom_art"));
    CmdUpdate.Parameters.Add(new SqlParameter("@designationart", SqlDbType.VarChar, 250, "designation_art"));
    CmdUpdate.Parameters.Add(new SqlParameter("@idart", SqlDbType.Int, 10,"id_art"));
    //-
    MonAdapter = new SqlDataAdapter(CmdSelect);
    MonAdapter.UpdateCommand = CmdUpdate;
    //-
    ds.Clear();
    //-
    MonAdapter.Fill(ds.Tables["Liste_article"]);
    //-
    textBox1.DataBindings.Clear();
    textBox2.DataBindings.Clear();
    textBox3.DataBindings.Clear();
    textBox4.DataBindings.Clear();
    //-
    textBox1.DataBindings.Add("Text", ds.Tables["Liste_article"], "id_art");
    textBox2.DataBindings.Add("Text", ds.Tables["Liste_article"], "ref_art");
    textBox3.DataBindings.Add("Text", ds.Tables["Liste_article"], "nom_art");
    textBox4.DataBindings.Add("Text", ds.Tables["Liste_article"], "designation_art");
    //-
    dataGridView1.DataSource = ds.Tables["Liste_article"];
    //-
    button3.Enabled = true;
    }
    catch (Exception xcp)
    {
    MessageBox.Show(xcp.Message);
    }
    }
    #endregion

    #region Button3 - Update
    private void button3_Click(object sender, EventArgs e)
    {
    try
    {
    CM.EndCurrentEdit();
    //-
    MonAdapter.Update(ds.Tables["Liste_article"]);
    //-
    ds.Clear();
    //-
    MonAdapter.Fill(ds.Tables["Liste_article"]);

    }
    catch (Exception xcp)
    {
    MessageBox.Show(xcp.Message);
    }
    }
    #endregion

    }
    }

  2. #2
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Janvier 2005
    Messages
    27
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 27
    Points : 25
    Points
    25
    Par défaut
    ton erreur est ou exactement ?

  3. #3
    Membre régulier
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Octobre 2005
    Messages
    77
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2005
    Messages : 77
    Points : 83
    Points
    83
    Par défaut
    Au moment de l'etape 3 pour l'update. (voir region 3-Update)

    j'obtiens l'erreur en cliquant sur le button3, au moment ou je souhaite faire l'update des données qui ont été modifier dans le datagrid.

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Janvier 2005
    Messages
    27
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 27
    Points : 25
    Points
    25
    Par défaut
    il me semble quil faut faire un endedit et une validation du composant datagrid avant d'enregistrer les donnees

  5. #5
    Membre régulier
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Octobre 2005
    Messages
    77
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2005
    Messages : 77
    Points : 83
    Points
    83
    Par défaut
    merci à tous c'est réglé.

    un petit oublie du Currencymanager dans les requetes avancé.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 6
    Dernier message: 31/03/2010, 09h56
  2. Réponses: 7
    Dernier message: 31/12/2009, 09h10
  3. Réponses: 0
    Dernier message: 29/12/2009, 09h51
  4. Réponses: 20
    Dernier message: 13/08/2007, 16h11
  5. Réponses: 8
    Dernier message: 04/06/2007, 16h20

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