Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. WinForm Database is locked SQLite

WinForm Database is locked SQLite

Scheduled Pinned Locked Moved C#
helpquestiondatabasesqliteannouncement
19 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sascha Lefevre

    Do you possibly have some Select-query that is executed in your program's startup-code (so it get's executed no matter what) ? If yes, you might comment that out for testing. Maybe you don't close the reader of that query properly so that it's still open when trying to execute updateIndex().

    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

    D Offline
    D Offline
    DPaul1994
    wrote on last edited by
    #10

    I am sure..Where I have used DataReader, I used CommnadBehavior.CloseConnection and after that I closed the reader. For example:

    string selectareLicenta = "SELECT licenta FROM licente WHERE licenta = '" + maskedTextBox1.Text + "'";
    using (SQLiteCommand cmd = new SQLiteCommand(selectareLicenta, Conexiune.getConnection()))
    {
    using (SQLiteDataReader read = cmd.ExecuteReader(CommandBehavior.CloseConnection))
    {
    if (read.Read())
    {
    SimulatorManager.Licenta = read["licenta"].ToString();
    }
    read.Close();
    }
    }

    S 1 Reply Last reply
    0
    • D DPaul1994

      I am sure..Where I have used DataReader, I used CommnadBehavior.CloseConnection and after that I closed the reader. For example:

      string selectareLicenta = "SELECT licenta FROM licente WHERE licenta = '" + maskedTextBox1.Text + "'";
      using (SQLiteCommand cmd = new SQLiteCommand(selectareLicenta, Conexiune.getConnection()))
      {
      using (SQLiteDataReader read = cmd.ExecuteReader(CommandBehavior.CloseConnection))
      {
      if (read.Read())
      {
      SimulatorManager.Licenta = read["licenta"].ToString();
      }
      read.Close();
      }
      }

      S Offline
      S Offline
      Sascha Lefevre
      wrote on last edited by
      #11

      I don't think this is the issue but for good practice you should change this like so:

      string selectareLicenta = "SELECT licenta FROM licente WHERE licenta = '" + maskedTextBox1.Text + "'";
      using (SQLiteConnection conn = Conexiune.getConnection())
      using (SQLiteCommand cmd = new SQLiteCommand(selectareLicenta, conn))
      using (SQLiteDataReader read = cmd.ExecuteReader(CommandBehavior.CloseConnection))
      {
      if (read.Read())
      {
      SimulatorManager.Licenta = read["licenta"].ToString();
      }
      // no need to explicitly close the reader when used in a using-block
      }

      ..and you also should use an Sql-Parameter here. Regarding the actual problem: I would suggest you double-check that all parts of your code where you run a select-query look like the above to ensure that the reader and connection get properly closed. For the purpose of testing you could execute an update-statement as the very first action in your program and see if that works.

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      D 3 Replies Last reply
      0
      • S Sascha Lefevre

        I don't think this is the issue but for good practice you should change this like so:

        string selectareLicenta = "SELECT licenta FROM licente WHERE licenta = '" + maskedTextBox1.Text + "'";
        using (SQLiteConnection conn = Conexiune.getConnection())
        using (SQLiteCommand cmd = new SQLiteCommand(selectareLicenta, conn))
        using (SQLiteDataReader read = cmd.ExecuteReader(CommandBehavior.CloseConnection))
        {
        if (read.Read())
        {
        SimulatorManager.Licenta = read["licenta"].ToString();
        }
        // no need to explicitly close the reader when used in a using-block
        }

        ..and you also should use an Sql-Parameter here. Regarding the actual problem: I would suggest you double-check that all parts of your code where you run a select-query look like the above to ensure that the reader and connection get properly closed. For the purpose of testing you could execute an update-statement as the very first action in your program and see if that works.

        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

        D Offline
        D Offline
        DPaul1994
        wrote on last edited by
        #12

        Very good idea, I will try it right now!

        1 Reply Last reply
        0
        • S Sascha Lefevre

          I don't think this is the issue but for good practice you should change this like so:

          string selectareLicenta = "SELECT licenta FROM licente WHERE licenta = '" + maskedTextBox1.Text + "'";
          using (SQLiteConnection conn = Conexiune.getConnection())
          using (SQLiteCommand cmd = new SQLiteCommand(selectareLicenta, conn))
          using (SQLiteDataReader read = cmd.ExecuteReader(CommandBehavior.CloseConnection))
          {
          if (read.Read())
          {
          SimulatorManager.Licenta = read["licenta"].ToString();
          }
          // no need to explicitly close the reader when used in a using-block
          }

          ..and you also should use an Sql-Parameter here. Regarding the actual problem: I would suggest you double-check that all parts of your code where you run a select-query look like the above to ensure that the reader and connection get properly closed. For the purpose of testing you could execute an update-statement as the very first action in your program and see if that works.

          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

          D Offline
          D Offline
          DPaul1994
          wrote on last edited by
          #13

          I tried to run the update on form loading and I get the error. It means that the error can be from the form that opens this form? Because I checked there and I have used `using` for everything and is ok..

          S 1 Reply Last reply
          0
          • S Sascha Lefevre

            I don't think this is the issue but for good practice you should change this like so:

            string selectareLicenta = "SELECT licenta FROM licente WHERE licenta = '" + maskedTextBox1.Text + "'";
            using (SQLiteConnection conn = Conexiune.getConnection())
            using (SQLiteCommand cmd = new SQLiteCommand(selectareLicenta, conn))
            using (SQLiteDataReader read = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
            if (read.Read())
            {
            SimulatorManager.Licenta = read["licenta"].ToString();
            }
            // no need to explicitly close the reader when used in a using-block
            }

            ..and you also should use an Sql-Parameter here. Regarding the actual problem: I would suggest you double-check that all parts of your code where you run a select-query look like the above to ensure that the reader and connection get properly closed. For the purpose of testing you could execute an update-statement as the very first action in your program and see if that works.

            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

            D Offline
            D Offline
            DPaul1994
            wrote on last edited by
            #14

            It's ok, I have found the error! In class `cs` where I have `static void Main()`, this is the main class of the project, I have this code:

            string selectutilizator = "SELECT * FROM accounts";
            using (SQLiteCommand selcom = new SQLiteCommand(selectutilizator, Conexiune.getConnection()))
            {
            using (SQLiteDataReader read = selcom.ExecuteReader())
            {
            if (read.HasRows)
            {
            Application.Run(new Elev());
            }
            else Application.Run(new Intro());
            }
            }

            But I need this for the program..what is wrong there?

            S 1 Reply Last reply
            0
            • D DPaul1994

              I tried to run the update on form loading and I get the error. It means that the error can be from the form that opens this form? Because I checked there and I have used `using` for everything and is ok..

              S Offline
              S Offline
              Sascha Lefevre
              wrote on last edited by
              #15

              Hmm. And there's nothing database-related happening before that in your code? Could probably another program be running and accessing the same database? Maybe there's some "zombie"-process of your program still running and keeping a connection to the database alive. See if rebooting Windows solves the issue.

              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

              D 1 Reply Last reply
              0
              • S Sascha Lefevre

                Hmm. And there's nothing database-related happening before that in your code? Could probably another program be running and accessing the same database? Maybe there's some "zombie"-process of your program still running and keeping a connection to the database alive. See if rebooting Windows solves the issue.

                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                D Offline
                D Offline
                DPaul1994
                wrote on last edited by
                #16

                See my last reply

                1 Reply Last reply
                0
                • D DPaul1994

                  It's ok, I have found the error! In class `cs` where I have `static void Main()`, this is the main class of the project, I have this code:

                  string selectutilizator = "SELECT * FROM accounts";
                  using (SQLiteCommand selcom = new SQLiteCommand(selectutilizator, Conexiune.getConnection()))
                  {
                  using (SQLiteDataReader read = selcom.ExecuteReader())
                  {
                  if (read.HasRows)
                  {
                  Application.Run(new Elev());
                  }
                  else Application.Run(new Intro());
                  }
                  }

                  But I need this for the program..what is wrong there?

                  S Offline
                  S Offline
                  Sascha Lefevre
                  wrote on last edited by
                  #17

                  Yep, that's the problem, most certainly. Application.Run(..) is a blocking call, so the DataReader is alive all the time. Just declare a boolean variable hasRows at the top of this code, assign read.HasRows to it after ExecuteReader(), move the if-else out of the using-blocks and use the boolean variable there instead of the reader.

                  If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                  D 1 Reply Last reply
                  0
                  • S Sascha Lefevre

                    Yep, that's the problem, most certainly. Application.Run(..) is a blocking call, so the DataReader is alive all the time. Just declare a boolean variable hasRows at the top of this code, assign read.HasRows to it after ExecuteReader(), move the if-else out of the using-blocks and use the boolean variable there instead of the reader.

                    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                    D Offline
                    D Offline
                    DPaul1994
                    wrote on last edited by
                    #18

                    Yes, you right. Thank you very much! This is the solution:

                    string selectutilizator = "SELECT * FROM accounts";
                    bool hasrows=false;
                    using (SQLiteCommand selcom = new SQLiteCommand(selectutilizator, Conexiune.getConnection()))
                    {
                    using (SQLiteDataReader read = selcom.ExecuteReader())
                    {
                    if (read.HasRows)
                    {
                    hasrows=true;
                    }
                    }
                    }
                    if (hasrows) Application.Run(new Elev());
                    else Application.Run(new Intro());

                    S 1 Reply Last reply
                    0
                    • D DPaul1994

                      Yes, you right. Thank you very much! This is the solution:

                      string selectutilizator = "SELECT * FROM accounts";
                      bool hasrows=false;
                      using (SQLiteCommand selcom = new SQLiteCommand(selectutilizator, Conexiune.getConnection()))
                      {
                      using (SQLiteDataReader read = selcom.ExecuteReader())
                      {
                      if (read.HasRows)
                      {
                      hasrows=true;
                      }
                      }
                      }
                      if (hasrows) Application.Run(new Elev());
                      else Application.Run(new Intro());

                      S Offline
                      S Offline
                      Sascha Lefevre
                      wrote on last edited by
                      #19

                      DPaul1994 wrote:

                      Yes, you right. Thank you very much!

                      You're welcome! :)

                      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups