Pretty new to c# and db programming. And I've taken over somebody else's code. I'm getting an error when I'm trying to update the DB. Here's the code:
private void EnableEvent(int eventID)
{
OleDbCommand oleCMD = new OleDbCommand();
oleCMD.Connection = Database.SqlConn();
OleDbTransaction oleTrans = oleCMD.Connection.BeginTransaction();
oleCMD.Transaction = oleTrans;
try
{
StringBuilder sql = new StringBuilder();
sql.AppendFormat("UPDATE Events SET isActive = 1 where EventID='{0}'", eventID);
oleCMD.CommandText = sql.ToString();
// insert the header
oleCMD.ExecuteNonQuery();
oleTrans.Commit();
}
catch(开发者_JAVA技巧Exception e)
{
MessageBox.Show(e.Message, "Database Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
oleCMD.Connection.Close();
oleCMD.Dispose();
}
}
Try this!
private void EnableEvent(int eventID)
{
OleDbConnection myConn = new OleDbConnection(myConnString);
myConn.Open();
OleDbCommand myCommand = myConn.CreateCommand();
OleDbTransaction myTrans;
// Start a local transaction
myTrans = myConn.BeginTransaction();
// Assign transaction object for a pending local transaction
myCommand.Connection = myConn;
myCommand.Transaction = myTrans;
try
{
StringBuilder sql = new StringBuilder();
sql.AppendFormat("UPDATE Events SET isActive = 1 where EventID='{0}'", eventID);
myCommand.CommandText = sql.ToString();
// insert the header
myCommand.ExecuteNonQuery();
myTrans.Commit();
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Database Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
myCommand.Connection.Close();
myCommand.Dispose();
}
}
Let me know if this helps!
EDIT:
Gotcha now ... format your query like below and it would surely work
StringBuilder sql = new StringBuilder();
sql.AppendFormat("UPDATE Events SET isActive = 1 where EventID = {0}", eventID);
With Numeric
fields yiou shouldn't put apostrophes around the value. That was the problem.
精彩评论