Im writing an application that updates a database that ive created. The application has a datagridview on it which displays the data from the database. Something very weird is going on with my app.
Here is the code that updates the database
string updateCommandString = "UPDATE RoomsTable SET [Date Checked]=@checkedDate WHERE ID = @id";
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\users\spreston\documents\visual studio 2012\Projects\roomChecksProgram\roomChecksProgram\roomsBase.accdb"))
{
using (OleDbCommand updateCommand = new OleDbCommand())
{
updateCommand.Connection = conn;
updateCommand.CommandText = updateCommandString;
updateCommand.CommandType = CommandType.Text;
updateCommand.Parameters.AddWithValue("@checkedDate",
this.dateTimePicker1.Value.ToShortDateString());
updateCommand.Parameters.AddWithValue("@id", row.roomID);
try
{
conn.Open();
updateCommand.ExecuteNonQuery();
conn.Close();
conn.Dispose();
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
Now when I run that code, close out off the app, and run the app again, the changed are correctly displayed in my datagridview that is connected to the database, but when i look at the actual database, nothing has changed at all. I dont know why this is happening.
My sql update updates the database, which is connected to the datagrid view. Sow HOW is the datagrid view displaying the correct data but not the database itself.
edit: I have had no sort of experience with sql before.
edit: transaction code:
string updateCommandString = "UPDATE RoomsTable SET [Date Checked]=@checkedDate WHERE ID = @id";
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\users\spreston\documents\visual studio 2012\Projects\roomChecksProgram\roomChecksProgram\roomsBase.accdb"))
{
OleDbTransaction transaction = null;
using (OleDbCommand updateCommand = new OleDbCommand())
{
updateCommand.Connection = conn;
updateCommand.Transaction = transaction;
updateCommand.CommandText = updateCommandString;
updateCommand.CommandType = CommandType.Text;
updateCommand.Parameters.AddWithValue("@checkedDate", this.dateTimePicker1.Value.ToShortDateString());
updateCommand.Parameters.AddWithValue("@id", row.roomID);
try
{
conn.Open();
transaction = conn.BeginTransaction();
updateCommand.ExecuteNonQuery();
transaction.Commit();
conn.Close();
conn.Dispose();
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
dateTimePicker1.Value.ToShortDateString()is necessary. Converting DateTime to string is culture specific.