3
Answers

Unable to update Sql database table

mark jones

mark jones

16y
4.2k
1

Can someone please help me.  I am currently using Sql server Express 5 and c# and I unable to update a Sql datbase table.  I have the following windows forms, Form1 and  Form3.  Form1 is used to show the menu and on the menu is a button that displays a list in a datagrid which is on Form3.  On Form3 also is a save button that I want to save any changes that are made to the list.

The problem am having is when I add a row to the list in the table the changes are not showing in the sql datbase when I press the save button.  Below is the code I use for Form1 and the code for the 'save button' on Form3'

Thanks for nay help

//Form 1

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;


namespace MovieBase1
{
 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button4;
  private System.Windows.Forms.Button button5;
  private System.Windows.Forms.Button button6;
  private System.Windows.Forms.Label label1;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;
  
  public DataSet Movset1 = new DataSet();
  

  
  
  public static SqlConnection Mcon = new SqlConnection("workstation id=SHERMAN2;packet size=4096;integrated security=SSPI;data source=\"SH" +
   "ERMAN2\\SQLEXPRESS\";persist security info=True;initial catalog=MDatabase");

    public SqlCommand Movcmd = Mcon.CreateCommand();
  private System.Windows.Forms.Button button7;
  
  public SqlDataAdapter MovAdapt = new SqlDataAdapter();
   
       

  
  
     

  public Form1()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }
        
  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
   this.button5 = new System.Windows.Forms.Button();
   this.button6 = new System.Windows.Forms.Button();
   this.label1 = new System.Windows.Forms.Label();
   this.button7 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(304, 72);
   this.button1.Name = "button1";
   this.button1.TabIndex = 0;
   this.button1.Text = "Delete Film";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(328, 120);
   this.button2.Name = "button2";
   this.button2.TabIndex = 1;
   this.button2.Text = "Current Movie Count";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(336, 168);
   this.button3.Name = "button3";
   this.button3.TabIndex = 2;
   this.button3.Text = "Movie List";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // button4
   //
   this.button4.Location = new System.Drawing.Point(336, 208);
   this.button4.Name = "button4";
   this.button4.TabIndex = 3;
   this.button4.Text = "Exit";
   this.button4.Click += new System.EventHandler(this.button4_Click);
   //
   // button5
   //
   this.button5.Location = new System.Drawing.Point(336, 264);
   this.button5.Name = "button5";
   this.button5.TabIndex = 4;
   this.button5.Text = "button5";
   this.button5.Click += new System.EventHandler(this.button5_Click);
   //
   // button6
   //
   this.button6.Location = new System.Drawing.Point(328, 16);
   this.button6.Name = "button6";
   this.button6.TabIndex = 5;
   this.button6.Text = "Add Film";
   this.button6.Click += new System.EventHandler(this.button6_Click);
   //
   // label1
   //
   this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
   this.label1.Location = new System.Drawing.Point(32, 16);
   this.label1.Name = "label1";
   this.label1.TabIndex = 6;
   this.label1.Text = "Movie Base";
   //
   // button7
   //
   this.button7.Location = new System.Drawing.Point(80, 152);
   this.button7.Name = "button7";
   this.button7.Size = new System.Drawing.Size(168, 23);
   this.button7.TabIndex = 7;
   this.button7.Text = "button7";
   this.button7.Click += new System.EventHandler(this.button7_Click);
   //
   // Form1
   //
   this.AccessibleDescription = "public";
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(432, 366);
   this.Controls.Add(this.button7);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.button6);
   this.Controls.Add(this.button5);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
   
  }
  
  private void button6_Click(object sender, System.EventArgs e)
  {
   
   MessageBox.Show("under construction");
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   MessageBox.Show("under construction");
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   MessageBox.Show(" under constructiion ");
  }

  private void button3_Click(object sender, System.EventArgs e)
  {   Form3 F3 = new Form3();
   Mcon.Open();
   Movcmd.CommandText = "SELECT * FROM MovTable1";
   MovAdapt.SelectCommand = Movcmd;
   MovAdapt.Fill(Movset1, "MovTable1");
            F3.dataGrid1.DataSource = Movset1;
   F3.Show();
  }

  private void button4_Click(object sender, System.EventArgs e)
  {
   this.Close();
  }

  private void button5_Click(object sender, System.EventArgs e)
  {
   MessageBox.Show("under construction");
  }

  private void button7_Click(object sender, System.EventArgs e)
  {
   
  }

  
 }
}


--------------------------------

\\'Save button on Form3 '

private void button1_Click(object sender, System.EventArgs e)
  { 
  
   string Updatestr = "UPDATE F1.Movset1, MovieTable1";
   F1.Movcmd.CommandText = Updatestr;   
   F1.Movset1.AcceptChanges();
   F1.Movset1.Clear();
   Form1.Mcon.Close();  
     
  }

Answers (3)
Next Recommended Forum