markyjj01
Programmer
- Feb 10, 2008
- 5
Can someone please help me with the following problem. I want to update a sql database table through a dataset on a windows form(c#), the problem being that I want to do the update from a 2nd form. The dataset is created on the 1st form and I am not sure if I am referencing it correctly. I have tried making the dataset static and public but the changes are not showing in the sql database once I carry out the update.
The code for the 2 forms I am using is below....any help would be appreciated
//FORM1//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Common;
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 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;
internal static SqlDataAdapter MovAdapt = new SqlDataAdapter("SELECT * FROM MovTable1", Mcon);
public static DataTableMapping myNewMapping =
new DataTableMapping("MovMap","MovTable1";
Form3 F3;
public static DataSet Movset1 = new DataSet();
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
F3 = new Form3();
//
// 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, 16;
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, 20;
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 ";
}
public void button3_Click(object sender, System.EventArgs e)
{
SqlCommandBuilder cb = new SqlCommandBuilder(MovAdapt);
MovAdapt.Fill(Movset1, "MovTable1";
F3.dataGrid1.DataSource = Movset1;
F3.Movset2 = 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)
{
}
}
}
--------------------------------------------------------------------------------
//FORM2//
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace MovieBase1
{
/// <summary>
/// Summary description for Form3.
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
public System.Windows.Forms.DataGrid dataGrid1;
public System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public DataSet Movset2;
public DataSet MVset
{
get
{
return Movset2;
}
set
{
Movset2 = value;
}
}
public Form3()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <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.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.AccessibleDescription = "internal static";
this.dataGrid1.AccessibleName = "";
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(416, 120);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Save and Update ";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form3
//
this.AccessibleDescription = "Public";
this.AccessibleName = "Form3";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(496, 294);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form3";
this.Text = "Form3";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
public void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
{
}
public void button1_Click(object sender, System.EventArgs e)
{
Movset2.AcceptChanges();
Form1.MovAdapt.Update(Movset2, "MovTable1";
Form1.Mcon.Close();
}
}
}
The code for the 2 forms I am using is below....any help would be appreciated
//FORM1//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Common;
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 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;
internal static SqlDataAdapter MovAdapt = new SqlDataAdapter("SELECT * FROM MovTable1", Mcon);
public static DataTableMapping myNewMapping =
new DataTableMapping("MovMap","MovTable1";
Form3 F3;
public static DataSet Movset1 = new DataSet();
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
F3 = new Form3();
//
// 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, 16;
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, 20;
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 ";
}
public void button3_Click(object sender, System.EventArgs e)
{
SqlCommandBuilder cb = new SqlCommandBuilder(MovAdapt);
MovAdapt.Fill(Movset1, "MovTable1";
F3.dataGrid1.DataSource = Movset1;
F3.Movset2 = 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)
{
}
}
}
--------------------------------------------------------------------------------
//FORM2//
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace MovieBase1
{
/// <summary>
/// Summary description for Form3.
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
public System.Windows.Forms.DataGrid dataGrid1;
public System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public DataSet Movset2;
public DataSet MVset
{
get
{
return Movset2;
}
set
{
Movset2 = value;
}
}
public Form3()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <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.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.AccessibleDescription = "internal static";
this.dataGrid1.AccessibleName = "";
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(416, 120);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Save and Update ";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form3
//
this.AccessibleDescription = "Public";
this.AccessibleName = "Form3";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(496, 294);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form3";
this.Text = "Form3";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
public void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
{
}
public void button1_Click(object sender, System.EventArgs e)
{
Movset2.AcceptChanges();
Form1.MovAdapt.Update(Movset2, "MovTable1";
Form1.Mcon.Close();
}
}
}