I’m working on a Windows Forms project that requires me to connect to an access database and navigate through records, and add/delete them. I’ve looked at a few tutorials online, and thought I followed them correctly. Anyway, I can add a new item and it will show up fine in the window, but after I exit, all changes are lost and the database isn’t saved at all.
Any advice would be appreciated. Following is the code I’m using to save the database upon button-press. I’ve also hard-coded in some test values just for testing purposes.
private DataSet data;
private int inc;
private int MaxRows;
private OleDbConnection connect;
private OleDbDataAdapter da;
/**
* Autoloads the first item into our form's text fields.
*/
private void BookMgmt_Load(object sender, EventArgs e)
{
data = new DataSet();
connect = new System.Data.OleDb.OleDbConnection();
connect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\\LibraryAppDB.accdb";
string Books = "SELECT * FROM Books";
da = new OleDbDataAdapter(Books, connect);
connect.Open();
da.Fill(data, "Books");
Navigate();
MaxRows = data.Tables["Books"].Rows.Count;
connect.Close();
connect.Dispose();
}
/**
*changes data within the array update form with current
*item's info
*/
private void Navigate()
{
ID = data.Tables["Books"].Rows[inc];
textBox1.Text = ID.ItemArray.GetValue(0).ToString();
textBox2.Text = ID.ItemArray.GetValue(1).ToString();
textBox3.Text = ID.ItemArray.GetValue(2).ToString();
textBox5.Text = ID.ItemArray.GetValue(3).ToString();
textBox6.Text = ID.ItemArray.GetValue(4).ToString();
textBox7.Text = ID.ItemArray.GetValue(7).ToString();
textBox8.Text = ID.ItemArray.GetValue(8).ToString();
textBox10.Text = ID.ItemArray.GetValue(5).ToString();
textBox11.Text = ID.ItemArray.GetValue(6).ToString();
}
/**
* Saves the new element added into our actual database
*/
private void saveButton_Click(object sender, EventArgs e)
{
OleDbCommandBuilder cb;
DataRow dRow = data.Tables["Books"].NewRow();
cb = new OleDbCommandBuilder(da);
dRow[0] = 11;
dRow[1] = "test";
dRow[2] = "test";
dRow[3] = "test";
dRow[5] = "test";
dRow[6] = "test";
dRow[7] = "test";
dRow[8] = "test";
data.Tables["Books"].Rows.Add(dRow);
data.AcceptChanges();
da.Update(data, "Books");
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
Navigate();
MessageBox.Show("Entry Added", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/**
* Deletes the current item displayed on the form
*/
private void deleteButton_Click(object sender, EventArgs e)
{
OleDbCommandBuilder cb;
cb = new OleDbCommandBuilder(da);
if (textBox1.Text == "1")
{
}
else
{
data.Tables["Books"].Rows[inc].Delete();
this.MaxRows--;
this.inc = 0;
Navigate();
data.AcceptChanges()
da.Update(data, "Books");
MessageBox.Show("Gone");
}
data.AcceptChanges()resets the RowState on modified and new records to unchanged and removes the deleted records from the dataset. By calling it before the update you’re effectively telling the DataAdapter “nothing to do here”Just move the calls to accept changes to after the calls to update. The call is important because that way subsequent calls to update w.on’t attempt to make the changes again