I\'m sure there is an extremely simple reason that this one line isn\'t working, but it has evaded for the past week, so I\'m hoping someone else will notice my fault.
//change this line
DataRow daRow = dataRecipe.Tables["CookBookRecipes"].NewRow();
daRow[0] = tbRName.Text;
daRow[1] = listBox1.SelectedItem.ToString();
daRow[2] = tbRCreate.Text;
daRow[3] = tbRIngredient.Text;
daRow[4] = tbRPrep.Text;
daRow[5] = tbRCook.Text;
daRow[6] = tbRDirections.Text;
daRow[7] = tbRYield.Text;
daRow[8] = textBox1.Text;
if (MessageBox.Show("You wish to save your updates?", "Save Updates?", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
//add & change this too
dataRecipe.Tables["CookBookRecipes"].Rows.Add(daRow);
dataAdapt.Update(dataRecipe, "CookBookRecipes");
MessageBox.Show("Recipe Updated", "Update");
}
}
I had the same issue: Filled a new Dataset with some new rows, but nothing happened on update. I've used the MySqlDataAdapter which works similar.
It turns out that when you need the InsertCommand from the MySqlCommandBuilder you have to specify the rowstate as added. See also: MSDN
Try the below source.
private void btnSave_Click(object sender, EventArgs e)
{
cb = new SqlCommandBuilder(dataAdapt);
//Old source: DataRow daRow = dataRecipe.Tables["CookBookRecipes"].Rows[inc];
//Added source code
DataRow daRow = dataRecipe.Tables["CookBookRecipes"].NewRow();
//Added source code
dataRecipe.Tables["CookBookRecipes"].AddRow(daRow);
daRow.BeginEdit();
daRow[0] = tbRName.Text;
daRow[1] = listBox1.SelectedItem.ToString();
daRow[2] = tbRCreate.Text;
daRow[3] = tbRIngredient.Text;
daRow[4] = tbRPrep.Text;
daRow[5] = tbRCook.Text;
daRow[6] = tbRDirections.Text;
daRow[7] = tbRYield.Text;
daRow[8] = textBox1.Text;
daRow.EndEdit();
//Reset state of rows to unchanged
dataRecipe.Tables["CookBookRecipes"].AcceptChanges();
//Set modified. The dataAdapt will call update stored procedured
//for the row that has Modifed row state.
//You can also try SetAdded() method for new row you want to insert
daRow.SetModified();
if (MessageBox.Show("You wish to save your updates?", "Save Updates?", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
dataAdapt.Update(dataRecipe, "CookBookRecipes");
MessageBox.Show("Recipe Updated", "Update");
}
}
Check the properties of your Database If you are using Local-DB.
In "Copy to Output Directory" property set the value "Copy if newer" and you are good to go.
In order to update the data on the database your SqlDataAdapter need to have its InsertCommand, UpdateCommand, DeleteCommand properties set. The SqlCommandBuilder instance that you've created has these commands but you need to set them to your SqlDataAdapter.
In other worlds: Somewhere between
SqlCommandBuilder cb;
cb = new SqlCommandBuilder(dataAdapt);
and
dataAdapt.Update(dataRecipe, "CookBookRecipes");
you need to
dataAdapt.DeleteCommand = cb.GetDeleteCommand(true);
dataAdapt.UpdateCommand = cb.GetUpdateCommand(true);
dataAdapt.InsertCommand = cb.GetInsertCommand(true);
What does the SqlCommand
for Update look like? I see the command but I don't see any SqlText, that's what you're missing.
You need to define what .Update
does by setting .UpdateCommand
property on the SqlDataAdapter
This link gives a pretty good breakdown on how to go about it: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand.aspx