1
I am doing a C# inventory control and the idea is that when the user clicks a button, the amount of a record in Datagridview is incremented.
The first time I press the button, it updates perfectly. But the second time, the application hangs and takes a long time to respond. Then when responding, a window written "database locked" appears. And then updates the item all right. But I don’t know how to fix it. The code of the button in question:
private void btnPlus_Click(object sender, EventArgs e)
{
IDRegistro = 0;
IDRegistro = Convert.ToInt32(dgvItems.CurrentRow.Cells[0].Value); //pega o id do item selecionado no dataGridView
conn = new SQLiteConnection(conexao);
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
cmd = new SQLiteCommand("SELECT QTD_ATUAL FROM ESTOQUE WHERE ID = " + IDRegistro, conn);
SQLiteDataReader dr = cmd.ExecuteReader();
dr.Read();
int qtdAtual = Convert.ToInt32(dr["QTD_ATUAL"]);
qtdAtual++;
cmd = new SQLiteCommand("UPDATE ESTOQUE SET QTD_ATUAL = @QTD_ATUAL WHERE ID = @ID", conn);
cmd.Parameters.AddWithValue("ID", IDRegistro);
cmd.Parameters.AddWithValue("QTD_ATUAL", qtdAtual);
cmd.ExecuteNonQuery();
MessageBox.Show("Registro atualizado");
Carregar(); //atualiaza o DataGridView
}
You must have forgotten to close the connection at the end of the method. Call the
conn.Open();
– Bruno Warmling
I can’t guarantee that’s what it is, but you forgot the
conn.Dispose()
– Jéf Bueno
If you give exception will also give problem because the connection will not be closed.
– Maniero
I tried to put Conn.Dispose() (and also Conn.Close()) at the end of the function - since it opens when it runs - but still, it didn’t work. I have checked that it locks in the second cmd.Executenonquery().
– Bia_Rodrig