1
Answer

Update database

Photo of Scale black

Scale black

18y
2.5k
1
Im trying to update the content in the database but it doesnt work i dont understand why. I commented most of the code tell me if something is unclear Get content code: //if creating new file use textbox1 rather then the double clicked data cell string fns = ""; if (open == false) { dataGridView1.CurrentCell = this.dataGridView1[0, e.RowIndex]; fns = dataGridView1.CurrentCell.Value.ToString(); } else { //use filename box (file creation) fns = textBox1.Text; } // get content where name == fns SqlConnection sql = new SqlConnection(sqlcon); a = new SqlDataAdapter("SELECT [Content], [primkey], [Name] FROM Keeper WHERE [Name] ='" + fns + "'", sqlcon); try { a.Fill(s, "Keeper"); foreach (DataRow dr in s.Tables[0].Rows) { dataGridView2.DataSource = s; dataGridView2.DataMember = "Keeper"; } //get content from datagrid insert into richtextbox dataGridView2.CurrentCell = this.dataGridView2[0, 0]; richTextBox1.Text = dataGridView2.CurrentCell.Value.ToString(); } catch (SqlException sqlexcp) { MessageBox.Show(sqlexcp.Message.ToString()); } //close connection, reset bool, bring up editor sql.Close(); open = false; richTextBox1.BringToFront(); Save content: //get content from textbox insert into datagrid dataGridView2.CurrentCell = this.dataGridView2[0, 0]; dataGridView2.CurrentCell.Value = richTextBox1.Text; //update DB SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(a); a.Update(s, "Keeper");

Answers (1)

0
Photo of Scale black
NA 7 0 18y
I found the problem but i dont know how to fix this. dataGridView2.CurrentCell = this.dataGridView2[0, 0]; dataGridView2.CurrentCell.Value = richTextBox1.Text; To update the dataset and datagrid, now the problem is becuase i dont click the next line in the datagrid the data doesnt get commited or something (altough the dataset does get changed) when updating to try and save the changes in the database nothing happens. If i unhide the datagrid and set the text in richtextbox hit save, click the second line in the datagrid and hit save again the data does get commited. So how would i simulate clicking the next line or how do i properly set the text in the datagrid or dataset so that it does update. If i use dataset.getxml i can see that the dataset is update regardless if i click the next line but it doesnt update the database. Thanks.