Gbohpy Fhgkhup
Gbohpy Fhgkhup

Reputation: 9

System.Data.SQLite.SQLiteException: Database is locked?

I encountered two problems [NOTE: This is my 1st time coding using SQL using Visual Studio]

I'm using SQLite Server Compact toolbox and Nuget System.Data.SQLite -Version 1.0.106

Everytime I'm trying to save it keep sending me this error

System.Data.SQLite.SQLiteException: Database is locked

I'm trying to create Update and Delete Button

Here's my Update button

   Private Sub Bttn_Update_Click(sender As Object, e As EventArgs) Handles Update_Bttn.Click

    Dim cons = New SQLiteConnection("uri=file:C:\Sqlite\Klinik_NurzawatiDB.db; version=3;Pooling=True;Max Pool Size=100;")
    cons.Open()

    If TextBox2.Text = "" Then
        MsgBox("Please insert a file Series", vbCritical, "Cannot update: Missing")

    ElseIf TextBox5.Text = "" Then
        MsgBox("Please insert file Group", vbInformation, "Required")
        TextBox5.Select()

    Else

        Try

            Dim sql As String = "UPDATE ClassTbl SET Series='" & TextBox2.Text & "', SubSeries='" & TextBox3.Text & "', Groups='" & TextBox5.Text & "', SubGroup='" & TextBox4.Text & "', ReferenceCode='" & TextBox6.Text & "',Seperator='" & TextBox7.Text & "', ItemName='" & TextBox1.Text & "' WHERE ID ='" & TextBox9.Text & "'"
            Dim cmd As SQLiteCommand = New SQLiteCommand(sql, cons)
            cmd.ExecuteNonQuery()

            MsgBox("Successfully updated!", MsgBoxStyle.Information, "File Status")
            '^OK

            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""
            TextBox8.Text = ""


        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End If
End Sub

Here's is my Delete button

Private Sub Delete_Bttn_Click(sender As Object, e As EventArgs) Handles Delete_Bttn.Click
    If TextBox9.Text = "" Then
        MsgBox("Please select Id to delete", vbExclamation, "File not selected")

    Else
        Dim con As New SQLiteConnection("uri=file:c:\sqlite\Klinik_NurzawatiDB.db; Version=3")
        con.Open()


        Dim response As DialogResult =
            MessageBox.Show(
            "Are you sure you want to delete this row?",
            "Records Lite: Confirm delete?",
            MessageBoxButtons.YesNo,
            MessageBoxIcon.Question,
            MessageBoxDefaultButton.Button2)


        If (response = DialogResult.No) Then
            'e.Cancel = True
            Exit Sub
            TextBox2.Text = ""
        End If

        Dim sql As String
        sql = "Delete From ClassTbl WHERE ID like '" & TextBox9.Text & "'"
        Dim cmd As SQLiteCommand = New SQLiteCommand(sql, con)
        cmd.ExecuteNonQuery()
        con.Close()


        MsgBox("Successfully", vbInformation, "Delete status")




    End If
End Sub

I always encountered problem at

cmd.ExecuteNonQuery()

For both of the buttons, any solution? I already created the same Database but still encountered these problems

Upvotes: 0

Views: 2671

Answers (1)

Pikoh
Pikoh

Reputation: 7703

The problem with your code is that your are not closing the conection in your update method. You need to add a cons.Close(). Anyway, it's always a good idea to use Using in all the classes that implement IDisposable

Upvotes: 4

Related Questions