DataBindings et mise à jour de table SQL Server

DataBindings et mise à jour de table SQL Server - C#/.NET managed - Programmation

Marsh Posté le 19-02-2006 à 22:05:01    

Je souhaite faire un formulaire pour mettre à jour une table SQL Server en C#. J'ai créé une connexion que j'ai lié à un SqlDataAdapter. Ensuite, j'ai un DataSet, rempli par le SQLDataAdapter, et qui est databindé à des TextBox, et un DateTimePicker.
 
Voici mon code :

Code :
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Globalization;
  5. using System.Windows.Forms;
  6. namespace ConsoleApplication1
  7. {
  8. class Form1 : System.Windows.Forms.Form
  9. {
  10.  private System.Windows.Forms.TabControl tabControl1;
  11.  private System.Windows.Forms.TabPage tabPage1;
  12.  private System.Windows.Forms.TabPage tabPage2;
  13.  private System.Windows.Forms.ListBox lstNoms;
  14.  private System.Windows.Forms.ListBox lstPseudos;
  15.  private System.Data.SqlClient.SqlConnection sqlConnection1;
  16.  private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
  17.  private System.Windows.Forms.TextBox txtNom;
  18.  private System.Windows.Forms.Label label1;
  19.  private System.Windows.Forms.Label label2;
  20.  private System.Windows.Forms.TextBox txtPrenom;
  21.  private System.Windows.Forms.Label label3;
  22.  private System.Windows.Forms.TextBox txtPseudo;
  23.  private System.Windows.Forms.Label label4;
  24.  private System.Windows.Forms.DateTimePicker dtDateNaissance;
  25.  private System.Windows.Forms.Button cmdAppliquer;
  26.  private System.Windows.Forms.Button cmdAnnuler;
  27.  private System.Data.DataSet DataSet1;
  28.  #region Généré par l'assistant
  29.  private void InitializeComponent()
  30.  {
  31.   this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
  32.   this.tabControl1 = new System.Windows.Forms.TabControl();
  33.   this.tabPage1 = new System.Windows.Forms.TabPage();
  34.   this.lstNoms = new System.Windows.Forms.ListBox();
  35.   this.tabPage2 = new System.Windows.Forms.TabPage();
  36.   this.lstPseudos = new System.Windows.Forms.ListBox();
  37.   this.txtNom = new System.Windows.Forms.TextBox();
  38.   this.label1 = new System.Windows.Forms.Label();
  39.   this.label2 = new System.Windows.Forms.Label();
  40.   this.txtPrenom = new System.Windows.Forms.TextBox();
  41.   this.label3 = new System.Windows.Forms.Label();
  42.   this.txtPseudo = new System.Windows.Forms.TextBox();
  43.   this.dtDateNaissance = new System.Windows.Forms.DateTimePicker();
  44.   this.label4 = new System.Windows.Forms.Label();
  45.   this.cmdAppliquer = new System.Windows.Forms.Button();
  46.   this.cmdAnnuler = new System.Windows.Forms.Button();
  47.   this.tabControl1.SuspendLayout();
  48.   this.tabPage1.SuspendLayout();
  49.   this.tabPage2.SuspendLayout();
  50.   this.SuspendLayout();
  51.   //  
  52.   // sqlConnection1
  53.   //  
  54.   this.sqlConnection1.ConnectionString = "workstation id=THOMAS;packet size=4096;integrated security=SSPI;data source=THOMA" +
  55.    "S;persist security info=False;initial catalog=cyber_cafe";
  56.   //  
  57.   // tabControl1
  58.   //  
  59.   this.tabControl1.Controls.Add(this.tabPage1);
  60.   this.tabControl1.Controls.Add(this.tabPage2);
  61.   this.tabControl1.Location = new System.Drawing.Point(8, 8);
  62.   this.tabControl1.Name = "tabControl1";
  63.   this.tabControl1.SelectedIndex = 0;
  64.   this.tabControl1.Size = new System.Drawing.Size(240, 304);
  65.   this.tabControl1.TabIndex = 0;
  66.   //  
  67.   // tabPage1
  68.   //  
  69.   this.tabPage1.Controls.Add(this.lstNoms);
  70.   this.tabPage1.Location = new System.Drawing.Point(4, 22);
  71.   this.tabPage1.Name = "tabPage1";
  72.   this.tabPage1.Size = new System.Drawing.Size(232, 278);
  73.   this.tabPage1.TabIndex = 0;
  74.   this.tabPage1.Text = "Noms";
  75.   //  
  76.   // lstNoms
  77.   //  
  78.   this.lstNoms.Location = new System.Drawing.Point(8, 8);
  79.   this.lstNoms.Name = "lstNoms";
  80.   this.lstNoms.Size = new System.Drawing.Size(216, 264);
  81.   this.lstNoms.TabIndex = 0;
  82.   this.lstNoms.SelectedIndexChanged += new System.EventHandler(this.ListBox_IndexChanged);
  83.   //  
  84.   // tabPage2
  85.   //  
  86.   this.tabPage2.Controls.Add(this.lstPseudos);
  87.   this.tabPage2.Location = new System.Drawing.Point(4, 22);
  88.   this.tabPage2.Name = "tabPage2";
  89.   this.tabPage2.Size = new System.Drawing.Size(232, 278);
  90.   this.tabPage2.TabIndex = 1;
  91.   this.tabPage2.Text = "Pseudos";
  92.   //  
  93.   // lstPseudos
  94.   //  
  95.   this.lstPseudos.Location = new System.Drawing.Point(8, 8);
  96.   this.lstPseudos.Name = "lstPseudos";
  97.   this.lstPseudos.Size = new System.Drawing.Size(216, 264);
  98.   this.lstPseudos.TabIndex = 0;
  99.   //  
  100.   // txtNom
  101.   //  
  102.   this.txtNom.Location = new System.Drawing.Point(440, 40);
  103.   this.txtNom.Name = "txtNom";
  104.   this.txtNom.Size = new System.Drawing.Size(184, 20);
  105.   this.txtNom.TabIndex = 1;
  106.   this.txtNom.Text = "";
  107.   //  
  108.   // label1
  109.   //  
  110.   this.label1.Location = new System.Drawing.Point(264, 40);
  111.   this.label1.Name = "label1";
  112.   this.label1.Size = new System.Drawing.Size(168, 20);
  113.   this.label1.TabIndex = 2;
  114.   this.label1.Text = "Nom";
  115.   this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  116.   //  
  117.   // label2
  118.   //  
  119.   this.label2.Location = new System.Drawing.Point(264, 72);
  120.   this.label2.Name = "label2";
  121.   this.label2.Size = new System.Drawing.Size(168, 20);
  122.   this.label2.TabIndex = 4;
  123.   this.label2.Text = "Prénom";
  124.   this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  125.   //  
  126.   // txtPrenom
  127.   //  
  128.   this.txtPrenom.Location = new System.Drawing.Point(440, 72);
  129.   this.txtPrenom.Name = "txtPrenom";
  130.   this.txtPrenom.Size = new System.Drawing.Size(184, 20);
  131.   this.txtPrenom.TabIndex = 3;
  132.   this.txtPrenom.Text = "";
  133.   //  
  134.   // label3
  135.   //  
  136.   this.label3.Location = new System.Drawing.Point(264, 104);
  137.   this.label3.Name = "label3";
  138.   this.label3.Size = new System.Drawing.Size(168, 20);
  139.   this.label3.TabIndex = 6;
  140.   this.label3.Text = "Pseudo";
  141.   this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  142.   //  
  143.   // txtPseudo
  144.   //  
  145.   this.txtPseudo.Location = new System.Drawing.Point(440, 104);
  146.   this.txtPseudo.Name = "txtPseudo";
  147.   this.txtPseudo.Size = new System.Drawing.Size(184, 20);
  148.   this.txtPseudo.TabIndex = 5;
  149.   this.txtPseudo.Text = "";
  150.   //  
  151.   // dtDateNaissance
  152.   //  
  153.   this.dtDateNaissance.Location = new System.Drawing.Point(440, 136);
  154.   this.dtDateNaissance.Name = "dtDateNaissance";
  155.   this.dtDateNaissance.Size = new System.Drawing.Size(184, 20);
  156.   this.dtDateNaissance.TabIndex = 9;
  157.   this.dtDateNaissance.Value = new System.DateTime(1970, 1, 1, 1, 0, 0, 0);
  158.   //  
  159.   // label4
  160.   //  
  161.   this.label4.Location = new System.Drawing.Point(264, 136);
  162.   this.label4.Name = "label4";
  163.   this.label4.Size = new System.Drawing.Size(168, 20);
  164.   this.label4.TabIndex = 8;
  165.   this.label4.Text = "Date de naissace";
  166.   this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  167.   //  
  168.   // cmdAppliquer
  169.   //  
  170.   this.cmdAppliquer.Location = new System.Drawing.Point(256, 288);
  171.   this.cmdAppliquer.Name = "cmdAppliquer";
  172.   this.cmdAppliquer.Size = new System.Drawing.Size(88, 24);
  173.   this.cmdAppliquer.TabIndex = 10;
  174.   this.cmdAppliquer.Text = "Appliquer";
  175.   this.cmdAppliquer.Click += new System.EventHandler(this.cmdAppliquer_Click);
  176.   //  
  177.   // cmdAnnuler
  178.   //  
  179.   this.cmdAnnuler.Location = new System.Drawing.Point(536, 288);
  180.   this.cmdAnnuler.Name = "cmdAnnuler";
  181.   this.cmdAnnuler.Size = new System.Drawing.Size(88, 24);
  182.   this.cmdAnnuler.TabIndex = 11;
  183.   this.cmdAnnuler.Text = "Annuler";
  184.   //  
  185.   // Form1
  186.   //  
  187.   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  188.   this.ClientSize = new System.Drawing.Size(632, 317);
  189.   this.Controls.Add(this.cmdAnnuler);
  190.   this.Controls.Add(this.cmdAppliquer);
  191.   this.Controls.Add(this.label4);
  192.   this.Controls.Add(this.dtDateNaissance);
  193.   this.Controls.Add(this.label3);
  194.   this.Controls.Add(this.txtPseudo);
  195.   this.Controls.Add(this.txtPrenom);
  196.   this.Controls.Add(this.txtNom);
  197.   this.Controls.Add(this.label2);
  198.   this.Controls.Add(this.label1);
  199.   this.Controls.Add(this.tabControl1);
  200.   this.Name = "Form1";
  201.   this.Text = "Gestion des clients";
  202.   this.tabControl1.ResumeLayout(false);
  203.   this.tabPage1.ResumeLayout(false);
  204.   this.tabPage2.ResumeLayout(false);
  205.   this.ResumeLayout(false);
  206.  }
  207.  #endregion
  208.  public Form1()
  209.  {
  210.   this.InitializeComponent();
  211.   this.sqlDataAdapter1 = new SqlDataAdapter();
  212.   this.sqlDataAdapter1.SelectCommand = new SqlCommand("SELECT cli_id, cli_login, cli_password, cli_surname, cli_firstname, (cli_surname + ' ' + cli_firstname) AS cli_firstandsurname, cli_dateofbirth FROM client", this.sqlConnection1);
  213.   this.sqlDataAdapter1.UpdateCommand = new SqlCommand("UPDATE client SET cli_login = @cli_login, cli_password = @cli_password, cli_firstname = @cli_firstname, cli_surname = @cli_surname, cli_dateofbirth = @cli_dateofbirth WHERE cli_id = @cli_id", this.sqlConnection1);
  214.   this.sqlDataAdapter1.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cli_id", System.Data.SqlDbType.Int, 4, "cli_id" ));
  215.   this.sqlDataAdapter1.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cli_login", System.Data.SqlDbType.VarChar, 16, "cli_login" ));
  216.   this.sqlDataAdapter1.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cli_surname", System.Data.SqlDbType.VarChar, 30, "cli_surname" ));
  217.   this.sqlDataAdapter1.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cli_firstname", System.Data.SqlDbType.VarChar, 30, "cli_firstname" ));
  218.   this.sqlDataAdapter1.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cli_password", System.Data.SqlDbType.VarChar, 30, "cli_password" ));
  219.   this.sqlDataAdapter1.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cli_dateofbirth", System.Data.SqlDbType.DateTime, 8, "cli_dateofbirth" ));
  220.   this.DataSet1 = new DataSet();
  221.   this.sqlDataAdapter1.Fill(this.DataSet1, "client" );
  222.   this.txtNom.DataBindings.Add("Text", this.DataSet1.Tables["client"], "cli_surname" );
  223.   this.txtPrenom.DataBindings.Add("Text", this.DataSet1.Tables["client"], "cli_firstname" );
  224.   this.txtPseudo.DataBindings.Add("Text", this.DataSet1.Tables["client"], "cli_login" );
  225.   this.dtDateNaissance.DataBindings.Add("Value", this.DataSet1.Tables["client"], "cli_dateofbirth" );
  226.   this.lstNoms.DisplayMember = "cli_firstandsurname";
  227.   this.lstNoms.DataSource = this.DataSet1.Tables["client"].DefaultView;
  228.   this.lstPseudos.DisplayMember = "cli_login";
  229.   this.lstPseudos.DataSource = this.DataSet1.Tables["client"].DefaultView;
  230.  }
  231.  private void ListBox_IndexChanged(object sender, System.EventArgs e)
  232.  {
  233.   this.BindingContext[ this.DataSet1.Tables["client"] ].Position = ((ListBox)sender).SelectedIndex;
  234.  }
  235.  private void cmdAppliquer_Click(object sender, System.EventArgs e)
  236.  {
  237.   if(this.DataSet1.HasChanges())
  238.   {
  239.    this.sqlDataAdapter1.Update(this.DataSet1, "client" );
  240.    this.DataSet1.Tables["client"].AcceptChanges();
  241.   }
  242.   else
  243.   {
  244.    MessageBox.Show("Aucune modification à faire !" );
  245.   }
  246.  }
  247. }
  248. }


 
Le probleme avec ce code, c'est que si je change la valeur d'un champ, et que je veux cliquer sur Accepter, alors j'ai la message box "Pas de modif"... Par contre, si je selectionne un autre tuple.. et que je clique sur accepter.. alors la ça met à jour... Y a t il une solution propre, pour mettre a jour sans changer de tuple... Je desespère vraiment, je n'ai pas trouvé de code "potable" qui fonctionne.. merci d'avance :)

Reply

Marsh Posté le 19-02-2006 à 22:05:01   

Reply

Marsh Posté le 23-09-2008 à 16:14:13    

c'est con ça, j'ai le souci là, et j'aurais bien aimé la réponse qui va avec la question ^^

Reply

Marsh Posté le 23-09-2008 à 19:13:39    

l'idéal est de procéder comme suit:
 
créer un bindingSource dont le dataSource est le dataset et le datamember est la table du dataset.  
 
lié les controles au bindingSource.  
et quand tu cliques sur ton bouton, tu appeles la méthodes "EndEdit" du bindingSource et le tour est joué ;-)


---------------
quand un homme raisonne mal c'est qu'il n'a pas les données pour raisonner mieux (diderot)
Reply

Marsh Posté le 24-09-2008 à 01:52:02    

c'est ce que je fais faire, parceque là ça me fait n'importe quoi, et j'ai passé 3 heures à pas trouver pourquoi.
 
je te dis demain si ça passe :jap:

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed