Hello
I am trying to update record in a database. I can see the changes in the datagrid, but they don't get saved into the database.. The 3 textboxes show the values of the currently selected row in the datagrid. So i press the Change button to make the textboxes active, change the text and then click the Save button.
Here's how the form looks like: img529.imageshack.us/my.php?image=45903634.jpg
Code:using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { private SqlConnection conn; private string connStr; SqlDataAdapter da; DataSet ds; bool b; public Form1() { InitializeComponent(); connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\dbOne.mdf" + ";Integrated Security=True;Connect Timeout=30;User Instance=True"; conn = new SqlConnection(connStr); conn.Open(); string selectStr = "select * from people"; da = new SqlDataAdapter(selectStr, conn); ds=new DataSet(); da.Fill(ds,"people"); da.InsertCommand = new SqlCommand("insert into people(name, age) values (@name, @age)", conn); da.InsertCommand.Parameters.Add("@name",SqlDbType.VarChar,50,"name"); da.InsertCommand.Parameters.Add("@age",SqlDbType.Int,5,"age"); da.UpdateCommand = new SqlCommand("update people set name=@name, age=@age where id=@id",conn); da.UpdateCommand.Parameters.Add("@name",SqlDbType.VarChar,50,"name"); da.UpdateCommand.Parameters.Add("@age",SqlDbType.Int,5,"age"); da.UpdateCommand.Parameters.Add("@id",SqlDbType.Int,5,"id"); da.DeleteCommand = new SqlCommand("delete from people where id=@id",conn); da.DeleteCommand.Parameters.Add("@id",SqlDbType.Int,5,"id"); dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.DataSource = ds.Tables["people"]; txtId.DataBindings.Add("Text",ds.Tables["people"],"id"); txtName.DataBindings.Add("Text", ds.Tables["people"], "name"); txtAge.DataBindings.Add("Text", ds.Tables["people"], "age"); txtId.Enabled = false; txtName.Enabled = false; txtAge.Enabled = false; btnSave.Enabled = false; btnCancel.Enabled = false; conn.Close(); } private void btnChange_Click(object sender, EventArgs e) { b = false; txtName.Enabled = true; txtAge.Enabled = true; btnNew.Enabled = false; btnDelete.Enabled = false; btnSave.Enabled = true; btnCancel.Enabled = true; } private void btnSave_Click(object sender, EventArgs e) { if (b) // insert new line { DataRow newLine = ds.Tables["people"].NewRow(); newLine["name"] = txtName.Text; newLine["age"] = txtAge.Text; ds.Tables["people"].Rows.Add(newLine); } da.Update(ds,"people"); } private void btnNew_Click(object sender, EventArgs e) { b = true; int i = ds.Tables["people"].Rows.Count; dataGridView1.Rows[i].Selected = true; txtId.Text = ""; txtName.Text = ""; txtAge.Text = ""; txtName.Enabled = true; txtAge.Enabled = true; btnSave.Enabled = true; btnCancel.Enabled = true; } private void btnDelete_Click(object sender, EventArgs e) { int i = int.Parse(dataGridView1.CurrentRow.Index.ToString()); ds.Tables["people"].Rows[i].Delete(); da.Update(ds,"people"); } } }
I have a feeling that your problem is asynchronous, fatal metadata desynchronisation.
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
Are you running the database from within Visual C#? If so then I believe that the database wont save any changes made when debugging the software (you will actually need to Publish the program for the database to work.
(I could be wrongI'm still learning lol)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks