SQL Connection problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric

    #1

    SQL Connection problems

    I have an app that uses SQL with access. I've been fine through the whole thing until I tried using the UPDATE command. When it gets to ExecuteNonQuery () it tells me that there is no open connection. A few lines before it though I can access the database. I went through the query builder and it generated an update statement. Where am I going wrong? Here's my code:

    oleDbDataAdapte r1.SelectComman d.CommandText = "SELECT Count FROM Choices WHERE Title = '" + title + "' AND Choice = '" + choice + "'";
    oleDbDataAdapte r1.Fill(dataSet 1);

    DataTable dataTable = dataSet1.Tables[0];

    int count = Int32.Parse(dat aTable.Rows[0][4].ToString());
    count++;

    oleDbDataAdapte r1.UpdateComman d.CommandText = "UPDATE Choices SET Count = '" + count.ToString( ) + "' WHERE Title = '" + title + "' AND Choice = '" + choice + "'";
    oleDbDataAdapte r1.UpdateComman d.ExecuteNonQue ry();
  • Morten Wennevik

    #2
    Re: SQL Connection problems

    If my memory serves me right, the Fill command opens a connection receives
    the data and immediatly closes the connection.

    You have to reopen the connection, execute, and close it manually.

    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

    Comment

    • Peter Koen

      #3
      Re: SQL Connection problems

      "=?Utf-8?B?RXJpYw==?=" <anonymous@disc ussions.microso ft.com> wrote in
      news:C23D7A8F-6B37-4381-864F-0B4367FFE529@mi crosoft.com:
      [color=blue]
      > I have an app that uses SQL with access. I've been fine through the
      > whole thing until I tried using the UPDATE command. When it gets to
      > ExecuteNonQuery () it tells me that there is no open connection. A few
      > lines before it though I can access the database. I went through the
      > query builder and it generated an update statement. Where am I going
      > wrong? Here's my code:[/color]

      Fill opens the Connection by its own.

      for the UpdateCommand you can do something like this:

      try
      {
      oleDbDataAdapte r1.UpdateComman d.Connection.Op en();
      oleDbDataAdapte r1.UpdateComman d.ExecuteNonQue ry();
      }
      catch(Exception e)
      {
      }
      finally
      {
      oleDbDataAdapte r1.UpdateComman d.Connection.Cl ose();
      }



      --
      best regards

      Peter Koen
      -----------------------------------
      MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS

      Comment

      Working...