DataAdapter.Update() does not Update the Database

后端 未结 9 2013
庸人自扰
庸人自扰 2020-12-16 16:10

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.

9条回答
  •  暖寄归人
    2020-12-16 16:51

    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");
        }
    }
    

提交回复
热议问题