WinForm Database is locked SQLite
-
- For Insert-, Update- and Delete-Statements you should use the ExecuteNonQuery()-method of the Command-object. 2) Though you initialized an Sql-Parameter and added it to the Command-object, you didn't adjust your Sql-Statement accordingly and you used the value of your variable idintrebare as the name for the parameter. 3) As CommandType.Text is the default, you don't have to explicitly set it. 4) Use disposable objects (the Connection and Command here) in a
using
-Block. See this Article: IDisposable: What Your Mother Never Told You About Resource Deallocation[^] 5) idintrebare appears to be a class-member. I would suggest you call updateIndex() with idintrebare as an argument instead. As I don't know the type of it, I'll useint
here, please adjust that if neccessary. This should work:
private void updateIndex(int id)
{
string update = "UPDATE questions SET indexrow=@idintrebare WHERE id=@idintrebare;";
using (var connection = Conexiune.getConnection())
using (SQLiteCommand cmd = new SQLiteCommand(update, connection))
{
connection.Open();
cmd.Parameters.AddWithValue("@idintrebare", id);
cmd.ExecuteNonQuery();
}
}If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
First, thank you for suggestions. So: `Conexiune` is a class. `getConnection()` is a function that makes the connection and return true if connection is opened. So I can't use that `var connection`. I used this code in other forms and it worked..I want to update `indexrow` column with the value of int variable `idintrebare`..So I rewrite your code like this:
string update = "UPDATE questions SET indexrow=@idintrebare WHERE id=@idintrebare;";
using (var connection = Conexiune.getConnection())
using (SQLiteCommand cmd = new SQLiteCommand(update, connection))
{
connection.Open();
cmd.Parameters.AddWithValue("@idintrebare", idintrebare);
cmd.ExecuteNonQuery();
},but I receive `Operation is not valid due to the current state of the object`. `Conexiune` class looks like this: (This is good)
private static string conn = @"Data Source=database.db;Pooling=true;FailIfMissing=false;Version=3;";
public static SQLiteConnection connect = null;
private Conexiune() { }
public static SQLiteConnection getConnection()
{
SQLiteConnection connect = null;
try
{
connect = new SQLiteConnection(conn);
connect.Open();
return connect;
}
catch (SQLiteException e)
{
throw new Exception("Cannot connect", e);
}
} - For Insert-, Update- and Delete-Statements you should use the ExecuteNonQuery()-method of the Command-object. 2) Though you initialized an Sql-Parameter and added it to the Command-object, you didn't adjust your Sql-Statement accordingly and you used the value of your variable idintrebare as the name for the parameter. 3) As CommandType.Text is the default, you don't have to explicitly set it. 4) Use disposable objects (the Connection and Command here) in a
-
First, thank you for suggestions. So: `Conexiune` is a class. `getConnection()` is a function that makes the connection and return true if connection is opened. So I can't use that `var connection`. I used this code in other forms and it worked..I want to update `indexrow` column with the value of int variable `idintrebare`..So I rewrite your code like this:
string update = "UPDATE questions SET indexrow=@idintrebare WHERE id=@idintrebare;";
using (var connection = Conexiune.getConnection())
using (SQLiteCommand cmd = new SQLiteCommand(update, connection))
{
connection.Open();
cmd.Parameters.AddWithValue("@idintrebare", idintrebare);
cmd.ExecuteNonQuery();
},but I receive `Operation is not valid due to the current state of the object`. `Conexiune` class looks like this: (This is good)
private static string conn = @"Data Source=database.db;Pooling=true;FailIfMissing=false;Version=3;";
public static SQLiteConnection connect = null;
private Conexiune() { }
public static SQLiteConnection getConnection()
{
SQLiteConnection connect = null;
try
{
connect = new SQLiteConnection(conn);
connect.Open();
return connect;
}
catch (SQLiteException e)
{
throw new Exception("Cannot connect", e);
}
}getConnection()
returns anSQLiteConnection
(not true/false). So you can use that using (var connection = Conexiune.getConnection()) -line. But as I see in that Conexiune-class, it already opens the connection for you. So the (new) error you're getting probably happens on my connection.Open() -line. Please remove that line and see what happens. As I've seen from your reply to Richard Deeming, who suggested almost the same new code as me, you still get the old error there. This makes me believe the cause is one of those listed on the page that Simon_Whale linked for you. Do you maybe have an open Select-query while you click on the button that calls updateIndex() ?If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
getConnection()
returns anSQLiteConnection
(not true/false). So you can use that using (var connection = Conexiune.getConnection()) -line. But as I see in that Conexiune-class, it already opens the connection for you. So the (new) error you're getting probably happens on my connection.Open() -line. Please remove that line and see what happens. As I've seen from your reply to Richard Deeming, who suggested almost the same new code as me, you still get the old error there. This makes me believe the cause is one of those listed on the page that Simon_Whale linked for you. Do you maybe have an open Select-query while you click on the button that calls updateIndex() ?If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
I removed that line and now database is still locked. Well, I have another function which selects data from table and is called by pressing on another button, but even if I don't press that button and try to call `updateIndex()` just after form loads, I still got the error..
-
I removed that line and now database is still locked. Well, I have another function which selects data from table and is called by pressing on another button, but even if I don't press that button and try to call `updateIndex()` just after form loads, I still got the error..
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
-
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
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();
}
} -
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();
}
}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
-
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
-
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
-
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
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?
-
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..
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
-
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
-
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?
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, assignread.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
-
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, assignread.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
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()); -
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());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