Hello I have a simple method to update customer details in one of my database tables however when i try to update it an error occurs saying the database is locked. I have no idea how to fix this because my add and delete queries work just fine.
This is the error message:
System.Data.SQLite.SQLiteException: 'database is locked
database is locked'
Public Sub updateguest(ByVal sql As String)
Try
con.Open()
With cmd
.CommandText = sql
.Connection = con
End With
result = cmd.ExecuteNonQuery
If result > 0 Then
MsgBox("NEW RECORD HAS BEEN UPDATED!")
con.Close()
Else
MsgBox("NO RECORD HASS BEEN UPDATDD!")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
End Sub
Private Sub IbtnUpdate_Click(sender As Object, e As EventArgs) Handles ibtnUpdate.Click
Dim usql As String = "UPDATE Customers SET fname = '" & txtFName.Text & "'" & "WHERE CustomerID ='" & txtSearchID.Text & "'"
updateguest(usql)
End Sub
Private Sub IbtnSearch_Click(sender As Object, e As EventArgs) Handles ibtnSearch.Click
Dim sSQL As String
Dim newds As New DataSet
Dim newdt As New DataTable
Dim msql, msql1 As String
Dim con As New SQLiteConnection(ConnectionString)
con.Open()
msql = "SELECT * FROM Customers Where Fname Like '" & txtSearchName.Text & "%'"
msql1 = "SELECT * FROM Customers Where CustomerID '" & txtSearchID.Text & "'"
Dim cmd As New SQLiteCommand(msql, con)
Dim cmd1 As New SQLiteCommand(msql1, con)
Dim dt = GetSearchResults(txtSearchName.Text)
dgvCustomerInfo.DataSource = dt
Dim mdr As SQLiteDataReader = cmd.ExecuteReader()
If mdr.Read() Then
If txtSearchName.Text <> "" Then
sSQL = "SELECT * FROM customers WHERE fname LIKE'" & txtSearchName.Text & "%'"
Dim con1 As New SQLiteConnection(ConnectionString)
Dim cmd2 As New SQLiteCommand(sSQL, con1)
con1.Open()
Dim da As New SQLiteDataAdapter(cmd2)
da.Fill(newds, "customers")
newdt = newds.Tables(0)
If newdt.Rows.Count > 0 Then
ToTextbox(newdt)
End If
dgvCustomerInfo.DataSource = newdt
con1.Close()
txtSearchID.Clear()
ElseIf txtSearchID.Text <> "" Then
sSQL = "SELECT * FROM customers WHERE CustomerID ='" & txtSearchID.Text & "'"
Dim con2 As New SQLiteConnection(ConnectionString)
Dim cmd2 As New SQLiteCommand(sSQL, con2)
con2.Open()
Dim da As New SQLiteDataAdapter(cmd2)
da.Fill(newds, "customers")
newdt = newds.Tables(0)
If newdt.Rows.Count > 0 Then
ToTextbox(newdt)
End If
dgvCustomerInfo.DataSource = newdt
con2.Close()
txtSearchName.Clear()
End If
Else
MsgBox("No data found")
End If
End Sub
Private Sub IbtnDelete_Click(sender As Object, e As EventArgs) Handles ibtnDelete.Click
Dim dsql As String = "DELETE FROM customers WHERE customerid = " & txtSearchID.Text & ""
deleteme(dsql)
updatedgv(dgvCustomerInfo)
txtSearchID.Clear()
txtSearchName.Clear()
End Sub
Public Sub deleteme(ByVal sql As String)
Try
con.Open()
With cmd
.CommandText = sql
.Connection = con
End With
result = cmd.ExecuteNonQuery
If result > 0 Then
MsgBox("NEW RECORD HAS BEEN DELTED!")
con.Close()
Else
MsgBox("NO RECORD HASS BEEN DELTED!")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
End Sub
You made a good start on keeping your database code separate from you user interface code. However, any message boxes should be shown in the user interface and any sql statements should be written in the data access code.
I used Using...End Using blocks to ensure that database objects are closed and disposed. I used parameters to protect against sql injection. I am not too sure of the mapping of DbType types to Sqlite types. You might have to fool with that a bit. In you original Update statement you had the ID value in quotes. This would pass a string. When you use parameters, you don't have to worry about that or ampersands and double quotes. Just one clean string.
Private ConStr As String = "Your connection string"
Public Function updateguest(FirstName As String, ID As Integer) As Integer
Dim Result As Integer
Dim usql As String = "UPDATE Customers SET fname = #fname WHERE CustomerID = #ID;"
Using con As New SQLiteConnection(ConStr),
cmd As New SQLiteCommand(usql, con)
cmd.Parameters.Add("#fname", DbType.String).Value = FirstName
cmd.Parameters.Add("#ID", DbType.Int32).Value = ID
con.Open()
Result = cmd.ExecuteNonQuery
End Using
Return Result
End Function
Private Sub IbtnUpdate_Click(sender As Object, e As EventArgs) Handles ibtnUpdate.Click
Try
Dim Result = updateguest(txtFName.Text, CInt(txtSearchID.Text))
If Result > 0 Then
MsgBox("New RECORD HAS BEEN UPDATED!")
Else
MsgBox("NO RECORD HAS BEEN UPDATDD!")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
System.Data.OleDb.OleDbException: 'No value given for one or more required parameters.'
I have this error in this code how can I solve it?
Imports System.Data.OleDb
Public Class yenikayit
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim isimT As String = ""
Dim soyisimT As String = ""
Dim tcNoT As String = ""
Dim yasT As String = ""
Dim cinsiyetT As String = ""
Dim perNoT As String = ""
Dim egDurT As String = ""
Dim meslekT As String = ""
Dim telNoT As String = ""
Dim emailT As String = ""
isimT = TextBox1.Text
soyisimT = TextBox2.Text
tcNoT = TextBox3.Text
yasT = TextBox6.Text
cinsiyetT = ComboBox1.Text
perNoT = TextBox4.Text
egDurT = ComboBox2.Text
telNoT = TextBox5.Text
meslekT = TextBox10.Text
emailT = TextBox9.Text
Dim query As String = "INSERT INTO Personel (isim,soyisim,Yas,Cinsiyet,Alan,Egitim,TC,personel_id,tel_no,email) VALUES(isimT,soyisimT,yasT,cinsiyetT,meslekT,egDurT,tcNoT,perNoT,telNoT,emailT)"
Dim dbsource As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MONSTER\Desktop\AGKS\AGKS\Database5.accdb"
Dim conn = New OleDbConnection(dbsource)
Dim cmd As New OleDbCommand(query, conn)
conn.Open()
Dim sonuc As Boolean
sonuc = cmd.ExecuteNonQuery()
If (sonuc = 1) Then
MsgBox("Girdiğiniz veriler kayıt olmuştur")
conn.Close()
End If
End Sub
End Class
this is the part of the code that give us error
Currently, your code does not add any parameter values to the command, hence the "No Values Given" error. The command doesn't recognize those names and assign them values just because you have declared them as variables above. You should make it clear that those are parameters in your sql query, and then set each parameter's value before executing the query:
Note that all instances of OleDbType.VarChar need to be replaced with the correct datatypes of your columns. I obviously have no way of knowing what types those columns are so I have just made them all VarChar, but you should adjust those to match your table schema.
Dim dbsource As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MONSTER\Desktop\AGKS\AGKS\Database5.accdb"
Dim query As String = "INSERT INTO Personel (isim,soyisim,Yas,Cinsiyet,Alan,Egitim,TC,personel_id,tel_no,email) " & _
"VALUES (#isimT,#soyisimT,#yasT,#cinsiyetT,#meslekT,#egDurT,#tcNoT,#perNoT,#telNoT,#emailT)"
Using conn As OleDbConnection = New OleDbConnection(dbsource)
Dim cmd As New OleDbCommand(query, conn)
With cmd
.Parameters.Add("#isimT", OleDbType.VarChar).Value = TextBox1.Text
.Parameters.Add("#soyisimT", OleDbType.VarChar).Value = TextBox2.Text
.Parameters.Add("#yasT", OleDbType.VarChar).Value = TextBox6.Text
.Parameters.Add("#cinsiyetT", OleDbType.VarChar).Value = ComboBox1.Text
.Parameters.Add("#meslekT", OleDbType.VarChar).Value = TextBox10.Text
.Parameters.Add("#egDurT", OleDbType.VarChar).Value = ComboBox2.Text
.Parameters.Add("#tcNoT", OleDbType.VarChar).Value = TextBox3.Text
.Parameters.Add("#perNoT", OleDbType.VarChar).Value = TextBox4.Text
.Parameters.Add("#telNoT", OleDbType.VarChar).Value = TextBox5.Text
.Parameters.Add("#emailT", OleDbType.VarChar).Value = TextBox9.Text
End With
conn.Open()
Dim sonuc As Boolean
sonuc = cmd.ExecuteNonQuery()
If (sonuc = 1) Then
MsgBox("Girdiğiniz veriler kayıt olmuştur")
End If
conn.Close()
End Using
Ok, try something like this:
Dim query As String = "INSERT INTO Personel (isim,soyisim,Yas,Cinsiyet,Alan,Egitim,TC,personel_id,tel_no,email) "
query +="VALUES(#isimT,#soyisimT,#yasT,#cinsiyetT,#meslekT,#egDurT,#tcNoT,#perNoT,#telNoT,#emailT)"
Dim dbsource As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MONSTER\Desktop\AGKS\AGKS\Database5.accdb"
Dim conn = New OleDbConnection(dbsource)
Dim cmd As New OleDbCommand(query, conn)
query.Parameters.AddWithValue("#isimT", TextBox1.Text)
cmd.Parameters.AddWithValue("#soyisimT", TextBox2.Text)
cmd.Parameters.AddWithValue("#tcNoT", TextBox3.Text)
cmd.Parameters.AddWithValue("#yasT", TextBox6.Text)
cmd.Parameters.AddWithValue("#cinsiyetT", ComboBox1.Text)
cmd.Parameters.AddWithValue("#perNoT", TextBox4.Text)
cmd.Parameters.AddWithValue("#egDurT", ComboBox2.Text)
cmd.Parameters.AddWithValue("#telNoT", TextBox5.Text)
cmd.Parameters.AddWithValue("#meslekT", TextBox10.Text)
cmd.Parameters.AddWithValue("#emailT", TextBox9.Text)
conn.Open()
Dim sonuc As Boolean
sonuc = cmd.ExecuteNonQuery()
If (sonuc = 1) Then
MsgBox("Girdiğiniz veriler kayıt olmuştur")
conn.Close()
End If
This should do the job. This should fix your exception.
I made a POS project but when i save the data to access database the error is shown:
no value given for one or more required parameters.
My code is:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim MyConnection As OleDb.OleDbConnection = Nothing
Dim MyTransaction As OleDb.OleDbTransaction = Nothing
Try
'create the connection and transaction object
MyConnection = New OleDb.OleDbConnection(My.Settings.dbConnectionString)
MyConnection.Open()
MyTransaction = MyConnection.BeginTransaction
'insert the new recipt
Dim SQL As String = "insert into recipts (ReciptDate, ReciptTotal) values (:0,:1)"
Dim CMD1 As New OleDb.OleDbCommand
CMD1.Connection = MyConnection
CMD1.Transaction = MyTransaction
CMD1.CommandText = SQL
CMD1.Parameters.AddWithValue(":0", Now.Date)
CMD1.Parameters.AddWithValue(":1", TextBox4.Text)
CMD1.ExecuteNonQuery()
CMD1.Dispose()
'get the id for the recipt
SQL = "Select max (reciptId) as MAXID from recipts"
Dim CMD2 As New OleDb.OleDbCommand
CMD2.Connection = MyConnection
CMD2.Transaction = MyTransaction
CMD2.CommandText = SQL
Dim ReciptID As Long = CMD2.ExecuteScalar()
CMD2.Dispose()
'insert the details of the recipt
Dim I As Integer
For I = 0 To DGV2.Rows.Count - 1
'get the values
Dim Barcode As String = DGV2.Rows(I).Cells(0).Value
Dim BuyPrice As Decimal = DGV2.Rows(I).Cells(2).Value
Dim SellPrice As Decimal = DGV2.Rows(I).Cells(3).Value
Dim ItemCount As Integer = DGV2.Rows(I).Cells(4).Value
'next create a command
Dim CMD3 As New OleDb.OleDbCommand
SQL = "insert into ReciptDetails" & _
"(ReciptId, Barcode,ItemCount,ItemBuyPrice,ItemSellPrice)" & _
"Values" & _
"(:0 ,:1 ,:2 ,:3 ,:4)"
CMD3.Connection = MyConnection
CMD3.Transaction = Mytransaction
CMD3.CommandText = SQL
CMD1.Parameters.AddWithValue(":0", ReciptID)
CMD1.Parameters.AddWithValue(":1", Barcode)
CMD1.Parameters.AddWithValue(":2", ItemCount)
CMD1.Parameters.AddWithValue(":3", BuyPrice)
CMD1.Parameters.AddWithValue(":4", SellPrice)
CMD3.ExecuteNonQuery()
CMD3.Dispose()
Next
'all well save the changes
Mytransaction.Commit()
'close conncetion
Mytransaction.Dispose()
MyConnection.Close()
MyConnection.Dispose()
DGV2.Rows.Clear()
TextBox4.Text = ""
Catch ex As Exception
If Mytransaction IsNot Nothing Then
Mytransaction.Rollback()
End If
If MyConnection IsNot Nothing Then
If MyConnection.State = ConnectionState.Open Then
MyConnection.Close()
End If
End If
MsgBox(ex.Message, MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly)
End Try
End Sub
What is the problem here?
Simple typo. In your CMD3 you're adding all the parameters to CMD1
Hence CMD3 has 5 missing parameters.
In the event dragdrop of a datagridview I got this error:
Here's the whole code of my dragdrop event :
Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Dim clientPoint As Point = DataGridView1.PointToClient(New Point(e.X, e.Y))
Dim hit As DataGridView.HitTestInfo = DataGridView1.HitTest(clientPoint.X, clientPoint.Y)
Dim dgvr As DataGridViewRow = DirectCast(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
Dim celldata As Object() = New Object(dgvr.Cells.Count) {}
For col As Integer = 0 To dgvr.Cells.Count - 1
celldata(col) = dgvr.Cells(col).Value
Next
Dim dt As New DataTable()
dt = ds_data.Tables(0)
Dim colCheckbox As DataColumn = dt.Columns.Add("Column1", GetType(Boolean))
Dim row As DataRow = dt.NewRow()
row.ItemArray = celldata
dt.Rows.InsertAt(row, hit.RowIndex)
dgvr.DataGridView.Rows.Remove(dgvr)
Dim sqlCmd As SqlCommand
sqlCmd = con.CreateCommand()
sqlCmd.CommandText = "update megatom_data_commande set ordre='" & hit.RowIndex & "' where id='" & DataGridView1.Rows(hit.RowIndex).Cells("ID").Value.ToString & "'"
Try
sqlCmd.ExecuteNonQuery()
DataGridView2.DataSource = ds_data2.Tables(0).DefaultView
DataGridView1.DataSource = ds_data.Tables(0).DefaultView
Dim CMD As New SqlCommand("PLANNIFIER")
CMD.Parameters.Add("#CHAINE", SqlDbType.VarChar).Value = cboChaine.SelectedItem.ToString
ExecuteCMD(CMD)
Catch ex As SqlException
End Try
End Sub
In fact my datatable have 15 columns but I added manually a checkbox column to select rows so I think i have a problem of indexes but I dont know how to resolve that problem.
My ds_data is defined like the code below:
requestPlanifie = "select megatom_data_commande.id as ID,numéro,observation,moule,commande,id_etat,programme,ordre,moule,glav,Qté_commandée_Totale," _
& " Qté_coupée_cuir, Qté_coupée_synthétique, Qté_piquée, Qté_finie from megatom_data_commande" _
& " inner join dbo.MEGATOM_DATA_MOULE on megatom_data_moule.id=megatom_data_commande.id_moule " _
& " where id_chaine='" & cboChaine.Text & "' and ( id_etat='3' or id_etat='4') order by numéro asc,ordre asc"
Dim dataAdapter As New SqlDataAdapter(requestPlanifie, con)
Try
dataAdapter.Fill(ds_data, "ds_data")
dataAdapter2.Fill(ds_data2, "ds_data2")
Catch ex As SqlException
End Try
DataGridView1.DataSource = ds_data.Tables(0).DefaultView
Dim i As Integer
Dim itm As ListViewItem
Dim str(7) As String
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim Errors As String
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "Trojans.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
Errors = ""
For i = 1 To 4
Dim di As New DirectoryInfo(path(i))
For Each File In System.IO.Directory.GetFiles(path(i), "*", SearchOption.AllDirectories)
Dim fs As New FileInfo(File)
Label4.Text = fs.FullName
TextBox4.Text = _Info.mdsc(fs.FullName).ToUpper
Try
Using myConnection As OleDbConnection = New OleDbConnection(connString)
myConnection.Open()
Dim strg As String
strg = "SELECT * FROM Table1 WHERE MD5='" & TextBox4.Text
Using cmd2 As OleDbCommand = New OleDbCommand(strg, myConnection)
cmd2.Parameters.AddWithValue("MD5", TextBox4.Text)
Using dr As OleDbDataReader = cmd2.ExecuteReader
If dr.HasRows Then
dr.Read()
If TextBox4.Text = dr("MD5").ToString Then
Dim show As String = "SELECT * FROM Table1 WHERE MD5='" & TextBox4.Text
Label10.Text = Label10.Text + 1
str(0) = fs.Name
str(1) = show
str(2) = _Info.getSHA1Hash(fs.FullName)
str(3) = _Info.getSHA256Hash(fs.FullName)
str(4) = _Info.GetCRC32(fs.FullName)
str(5) = fs.FullName
str(6) = NativeMethods.FormatBytes(fs.Length) & " (" & fs.Length & " bytes)"
itm = New ListViewItem(str)
ListView1.Items.Add(itm) 'Added this but still no go
MsgBox("Found: " & dr("MD5") & "!")
Else
MsgBox("Error")
End If
Else
MsgBox("Error")
End If
End Using
End Using
End Using
Catch ex As Exception
'MsgBox(ex.ToString)
End Try
Label5.Text += 1
If Label5.Text < ProgressBar1.Maximum Then
ProgressBar1.Value = Label5.Text
End If
Next
Next
myConnection.Close()
I am trying to search a path,hash a file and show current hash in textbox4 at the same time searching ms database for the hash and if its their show corresponding info from database to list-view. Everything works ok except it does not add the data that is matched to list view.