Elvin Mammadov
Elvin Mammadov

Reputation: 27387

Update SQL comand doesn't change the data

I know may be it is stupid question. But This query doesn't work. I search solution more than 1 hour. Please Help

 public static bool ChangeEventStatus(Connector cn, EventData eventData)
    {
        int updatedRows = 0;

        using (OleDbCommand cmd = cn.CreateCommand())
        {
            cmd.CommandText = "Update EventList Set IsProcessed = ? Where EventId = ?";

            cmd.Parameters.Add("IsProcessed", OleDbType.Boolean).Value = true;
            cmd.Parameters.Add("EventId", OleDbType.BigInt).Value = eventData.EventId;


            updatedRows = cmd.ExecuteNonQuery();

        }

        return (updatedRows == 1);
    }

What is wrong in my code. ChangeEventStatus method return true, but database records doesn't change.

enter image description here

Upvotes: 0

Views: 75

Answers (3)

Aleksei Semidotskii
Aleksei Semidotskii

Reputation: 1455

I changed the code a bit and wrote the test. All work fine. Check:

  • Connection string
  • Is you pass correct EventId to function ?

    [TestClass]
    public class UnitTest1
    {
      [TestMethod]
      public void TestMethod1()
      {
        Assert.AreEqual(true, ChangeEventStatus(new EventData() {EventId = 3}));
      }
    
      private static bool ChangeEventStatus(EventData eventData)
      {
          int updatedRows = 0;
    
          using (var cn = new OleDbConnection("Provider=sqloledb;Data Source=localhost;Initial Catalog=FastExperiments;User Id = sa; Password = pass; "))
          {
            using (OleDbCommand cmd = cn.CreateCommand())
            {
                cn.Open();
    
                cmd.CommandText = "Update EventList Set IsProcessed = ? Where EventId = ?";
    
                cmd.Parameters.Add("IsProcessed", OleDbType.Boolean).Value = true;
                cmd.Parameters.Add("EventId", OleDbType.BigInt).Value = eventData.EventId;
    
                updatedRows = cmd.ExecuteNonQuery();
            }
          }
          return (updatedRows == 1);
      }
    
      private class EventData
      {
        public int EventId { get; set; }
      }
    }
    

Upvotes: 0

Elvin Mammadov
Elvin Mammadov

Reputation: 27387

I found problem. thanks everyone. I forgot commit transaction after updateting

Upvotes: 0

daryal
daryal

Reputation: 14919

 public static bool ChangeEventStatus(Connector cn, EventData eventData)
 {
        int updatedRows = 0;

         using (OleDbConnection conn = new OleDbConnection(someConnectionString));
        {
             conn.Open();
             using (OleDbCommand cmd = cn.CreateCommand())
             {
                 cmd.CommandText = "Update EventList Set IsProcessed = ? Where EventId = ?";

                 cmd.Parameters.Add("@IsProcessed", OleDbType.Boolean).Value = true;
                 cmd.Parameters.Add("@EventId", OleDbType.BigInt).Value = eventData.EventId;


                 updatedRows = cmd.ExecuteNonQuery();

        }

        return (updatedRows == 1);
    }
}

Upvotes: 1

Related Questions