System.Data.OleDb.OleDbException: 'No value given for one or more required parameters.' - vb.net

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.

Related

Database locked in vb.net when trying to update data in vb.net

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

SQL select statement with 2 conditions

I have a table that has columns CustomerCell and ReceiptType. I need to create a SELECT statement that displays every record that matches CustomerCell or ReceiptType.
I tried this code:
If TextBox1.Text.Trim.Length <> 0 OrElse CheckBox4.Checked = True Then
Dim Conn As New SqlConnection(constr)
Dim ds As New DataTable
Dim sqlstr As String = "Select [RcptNum], [RcptDate], [RcptCustName], [RcptCustCell], [RcptAmount], [RcptType], [RcptFld1], [RcptFld2], [RcptFld3], [RcptUser] From [tblReceipt] where (RcptCustCell = '" & TextBox1.Text & "') or ([RcptType] = 'Cash') "
Dim da As New SqlDataAdapter(sqlstr, Conn)
ds.Reset()
da = New SqlDataAdapter(sqlstr, Conn)
da.Fill(ds)
dgv.DataSource = ds
Call griddraw()
Conn.Close()
End If
Where Textbox1 is for CustomerCell and CheckBox4 is for ReceiptType. When I enter customer cell and receipt type I should see 2 records however with the above code I can see only one record.
This is my form:
As stated, look into parameters to avoid SQL injection and it does clear up your query a little more. I've put this together which may help. Might need a few tweaks for your application:
If TextBox1.Text.Trim.Length <> 0 OrElse CheckBox4.Checked = True Then
Dim dt As DataTable
Dim sqlstr As String = "Select [RcptNum], [RcptDate], [RcptCustName], [RcptCustCell], [RcptAmount], [RcptType], [RcptFld1], [RcptFld2], [RcptFld3], [RcptUser] From [tblReceipt] where (RcptCustCell = #RcptCustCell) or ([RcptType] = 'Cash') "
Using con As New SqlConnection(constr),
com As New SqlCommand(sqlstr, con)
com.Parameters.Add("#RcptCustCell", SqlDbType.VarChar).Value = TextBox1.Text
con.Open()
dt = New DataTable
dt.Load(com.ExecuteReader)
dgv.DataSource = dt
Call griddraw()
End Using
End If
Dim Conn As New SqlConnection(constr)
Dim ds As New DataTable
Dim sqlstr As String = "Select [RcptNum], [RcptDate], [RcptCustName], [RcptCustCell], [RcptAmount], [RcptType], [RcptFld1], [RcptFld2], [RcptFld3], [RcptUser] From [tblReceipt]"
If TextBox1.Text.trim.length <> 0 then
sqlstr += "where (RcptCustCell = '" & TextBox1.Text & "')"
endif
If chkPaymentCheck.checked then
if sqlstr.contains("where") = false then
sqlstr += "where RcptType = 'Check'"
EndIf
sqlstr += "or RcptType = 'Check'"
endif
Dim da As New SqlDataAdapter(sqlstr, Conn)
ds.Reset()
da = New SqlDataAdapter(sqlstr, Conn)
da.Fill(ds)
dgv.DataSource = ds
Call griddraw()
Conn.Close()
Try this and you can continue with the if statements to add more checks.

vb.net message: no value given for one or more required parameters

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.

Not able to show matched data from database in listview

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.

Check if value exist in DBF database

I am trying to check if value "IAV-1419" exist in second column (ColName) in database named PROMGL.DBF.
I get this error : No value give for one or more required parameters
Dim con As New OleDbConnection
Dim cmd As New OleDbCommand
Dim FilePath As String = "C:\"
Dim DBF_File As String = "PROMGL"
Dim ColName As String = "[NALOG,C,8]"
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _
" ;Extended Properties=dBASE IV"
cmd = New OleDbCommand("SELECT * FROM PROMGL WHERE [NALOG,C,8] = #NAL")
cmd.Connection = con
con.Open()
cmd.Parameters.AddWithValue("#NAL", "IAV-1419")
Using reader As OleDbDataReader = cmd.ExecuteReader()
If reader.HasRows Then
con.Close()
Label6.Text = "EXIST"
TextBox1.Text = ""
TextBox1.Focus()
Else
Label6.Text = "DOESN'T EXIST"
End If
End Using
I am stuck here, if anyone could please check this code for me.
are you sure that your connectionstring is correct????
Data Source=" & FilePath &
how connection string know the database where it point only to "C:\" i think is missing database name
Another personal suggestion make your code more simple to read in example :
Dim FilePath As String = "C:\"
Dim DBF_File As String = "PROMGL"
Dim ColName As String = "[NALOG,C,8]"
Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _
" ;Extended Properties=dBASE IV")
con.Open()
Using cmd As New OleDbCommand("SELECT * FROM PROMGL WHERE [NALOG,C,8] = #NAL", con
cmd.Parameters.AddWithValue("#NAL", "IAV-1419")
Using reader As OleDbDataReader = cmd.ExecuteReader()
If reader.HasRows Then
Label6.Text = "EXIST"
TextBox1.Text = ""
TextBox1.Focus()
Else
Label6.Text = "DOESN'T EXIST"
End If
End Using
End Using
End Using