Can someone please help me with the following problem. I am trying to update an sql datatable to show the changes. But when I run the code below the changes arent saved. I am currently using SQL Express 5.
using
System;using
System.Drawing;using
System.Collections;using
System.ComponentModel;using
System.Windows.Forms;using
System.Data;using
System.Data.SqlClient;
namespace
MovieBase1{
///{
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; ///"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(null,Mcon); public Form1(){
// // Required for Windows Form Designer support //InitializeComponent();
// // TODO: Add any constructor code after InitializeComponent call //}
///{
if( disposing ){
if (components != null){
components.Dispose();
}
}
base.Dispose( disposing );}
#region
Windows Form Designer generated code ///{
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
///[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){
}
}
}
---------------------------
any help would be appreciated.......thanks
mark jonesPosted Jul 23, 2008, 8:53 AM
further to the above, here is the code I use to save the changes and update the sql database, problem being that the sql database doesnt update:
private void button1_Click(object sender, System.EventArgs e)
{
F1.Movset1.Tables.Add("MovieTable1";
string Updatestr = "UPDATE F1.Movset1, MovieTable1";
F1.Movcmd.CommandText = Updatestr;
F1.Movcmd.ExecuteNonQuery();
Form1.Mcon.Close();
}
---------------------------------
Movietable1 is the SQl table I want to update
cheers
cheers
ArshadPosted Jul 20, 2008, 10:41 AM
Hi, you are just filling the DataSet and just dispalying into the Grid, you are not using any Update command of dataSet, u can Update the Database using the DataSet as well as just Pass the Direct SQL Query and connection object to the to the SqlCommand and Use the ExecuteQuery Method of the SqlCoomad class,You can do either this or you can Update the Database using the DataSet, Thanks