OleDb Error unknown field name “Name” in visual basics - vb.net

Hey there are many question like this however i didn't find the right one. i keep on getting this error,
image
i am Using
vb.net and Ms-access database.
I have researched and didn't get anything, i checked the name "productID" and it is correct. So i dont know what wrong? Please help
My Code:
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim dbProvider As String
Dim dbSource As String
Dim MyDocumentsFolder As String
Dim TheDatabase As String
Dim FullDatabasePath As String
Dim Sql As String
Dim con As New OleDb.OleDbConnection
Dim empty = Me.Controls.OfType(Of TextBox)().Where(Function(txt) txt.Text.Length = 0)
If empty.Any Then
MessageBox.Show("Please fill all informations")
Else
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
TheDatabase = "C:\Users\jacob\Desktop\MS Office\project.mdb"
MyDocumentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
FullDatabasePath = MyDocumentsFolder & TheDatabase
dbSource = "Data Source = " & TheDatabase
con.ConnectionString = dbProvider & dbSource
Sql = "INSERT INTO tblUsers (productID, quantity, price, productSection, supplierID) VALUES (#ProductID, #Quantity, #Price, #ProductSection, #SupplierID)"
MessageBox.Show("Database is now open")
con.Open()
Dim command As New OleDb.OleDbCommand(Sql, con)
command.Parameters.Add("#ProductID", OleDb.OleDbType.VarChar).Value = txtProductName.Text
command.Parameters.Add("#Quantity", OleDb.OleDbType.Integer).Value = txtQuantity.Text
command.Parameters.Add("#Price", OleDb.OleDbType.Currency).Value = txtPrice.Text
command.Parameters.Add("#ProductSection", OleDb.OleDbType.VarChar).Value = txtSection.Text
command.Parameters.Add("#SupplierID", OleDb.OleDbType.VarChar).Value = txtSupplier.Text
command.ExecuteNonQuery()
MessageBox.Show("Product Added Successfully")
con.Close()
MessageBox.Show("Database is now Closed")
End If
End Sub
The error is shown in this line : command.ExecuteNonQuery()

SQL is case sensitive, check if something is capitalized in your Sql variable and not capitalized in your table in SQL server or vise versa.
you can also check if the capitalization on the parameters might be causing the issue as well.

Related

how to insert multiple records in Access database using VB.NET

I am trying learn how to use Access within VB.NET so i tried to make a simple application using an Access Database that can be used as a dictionary where someone can add some words int the database and then he can search for them.
My db contains two tables one with Word | Description and another one with Word | Synonym
The issue is that one word may have more than one Synonyms so i was thinking i could type all the synonyms in a textbox and using Regex.Split(" ") to split them and insert them in a loop. Can this be done with OleDbParameters?
This is what i have done so far but it only inserts the last record:
str = "insert into Synonyms ([Word],[Synonym]) values (#word,#synonym)"
cmd = New OleDbCommand(str, myConnection)
cmd.Parameters.Add(New OleDbParameter("Word", CType(txtWord.Text,
String)))
cmd.Parameters.Add("#synonym", OleDbType.VarChar)
Dim syn As String() = Regex.Split(txtSynonyms.Text, " ")
Dim i As Integer = 0
While i < syn.Length()
cmd.Parameters("#synonym").Value = syn(i)
i = i + 1
End While
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
MsgBox("Synonyms for word """ & txtWord.Text & """ added")
txtWord.Clear()
txtSynonyms.Clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
myConnection.Open()
While i < syn.Length()
cmd.Parameters("#synonym").Value = syn(i)
cmd.ExecuteNonQuery()
i = i + 1
End While
myConnection.Close()
Maybe you will find this helpful.
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim con As New OleDb.OleDbConnection
Dim dbprovider As String
Dim dbsource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim inc As Integer
dbprovider = "Provider=Microsoft.ACE.OLEDB.12.0;"
dbsource = "Data Source = C:\your_path_here\Nwind_Sample.accdb"
con.ConnectionString = dbprovider & dbsource
con.Open()
sql = "SELECT * FROM [OrderDetails]"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "OrderDetails")
Dim builder As New OleDbCommandBuilder(da)
Dim dsnewrow As DataRow
dsnewrow = ds.Tables("OrderDetails").NewRow()
dsnewrow.Item(0) = OrderID.Text
dsnewrow.Item(1) = ProductID.Text
dsnewrow.Item(2) = UnitPrice.Text
dsnewrow.Item(3) = Quantity.Text
dsnewrow.Item(4) = Discount.Text
'dsnewrow.Item(6) = True
ds.Tables("OrderDetails").Rows.Add(dsnewrow)
da.Update(ds, "OrderDetails")
End Sub
End Class

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

Getting Data MS Access then Placing it inside VBNET texbox

i'm trying to figure out what's wrong with my code,
I'm trying to extract the value of employe ID selected from a dropdown, then get all information of that employee and place it in a textbox vb.net form, but there's a error whenever i select an employee ID
it's giving me an error message of : "An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: No value given for one or more required parameters."
Thus highlighting reader, i'm not sure what's the problem, i have initialized the reader... Please guide me. Thanks
Private Sub eNumText_SelectedIndexChanged(sender As Object, e As EventArgs) Handles eNumText.SelectedIndexChanged
Dim dbSource = "Data Source= C:\Databse\Company_db.accdb"
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= c:\Databse\Company_db.accdb"
Dim sqlQuery As String
Dim sqlCommand As New OleDbCommand
Dim sqlAdapter As New OleDbDataAdapter
Dim Table As New DataTable
Dim empNum As String
Dim empFname As String
Dim empLname As String
Dim empDept As String
Dim empStat As String
Dim empYears As String
empNum = eNumText.Text
empFname = empFnameText.Text
empLname = empLnameText.Text
empDept = DeptText.Text
empStat = StatText.Text
empYears = yearstext.Text
sqlQuery = "SELECT * FROM tbl_empinfo WHERE EmpID like empNum"
With sqlCommand
.CommandText = sqlQuery
.Connection = con
.Parameters.AddWithValue("EmpID", empNum)
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(Table)
End With
With DataGridView1
.DataSource = Table
End With
End With
Dim path = "Data Source= C:\Databse\Company_db.accdb"
Dim command = "SELECT * FROM tbl_empinfo WHERE EmpID like empNum"
QueryData(path, Command)
con.Close()
End Sub
Private Sub QueryData(PathDb As String, command As String)
PathDb = "Data Source= C:\Databse\Company_db.accdb"
Using connection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathDb)
Using da As New System.Data.OleDb.OleDbCommand(command, con)
con.Open()
Dim reader = da.ExecuteReader()
If reader.Read() Then
empFnameText.Text = reader("FirstName")
empLnameText.Text = reader("LastName")
End If
con.Close()
End Using
End Using
End Sub
You need to prefix your parameter empNum with an #: #empNum. See this answer: How to pass a parameter from vb.net for an example.

VB.NET SQL ACCESS Insert Statement

net and i'm trying to insert data into access database using sql, i have the code below, when i try to execute, it prompts me an error message and highlighting con.open() i don't understand why it's not working, can anyone guide me. Thank
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles AddBut.Click
Dim dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= c:\Databse\Company_db.accdb"
Dim dbSource = "Data Source= C:\Databse\Company_db.accdb"
Dim empNum As String
Dim empFname As String
Dim empLname As String
Dim empDept As String
Dim empStat As String
Dim empYears As String
empNum = eNumText.Text
empFname = empFnameText.Text
empLname = empLnameText.Text
empDept = Deptd.Text
empStat = Statd.Text
empYears = yearstext.Text
Dim sql = "INSERT INTO tbl_empinfo (EmpID, FirstName, LastName, Department, Status, Years " & _
") " & _
"Values(empNum, empFname, empLname, empDept, empStat, empYears)"
con.ConnectionString = dbProvider & dbSource
Using cmd = New OleDb.OleDbCommand(sql, con)
con.Open()
cmd.Parameters.AddWithValue("EmpID", empNum)
cmd.Parameters.AddWithValue("FirstName", empFname)
cmd.Parameters.AddWithValue("LastName", empLname)
cmd.Parameters.AddWithValue("Department", empDept)
cmd.Parameters.AddWithValue("Status", empStat)
cmd.Parameters.AddWithValue("Years", empYears)
cmd.ExecuteNonQuery()
End Using
con.Close()
End Sub
Standard problem, when you see a Syntax error in an otherwise fine SQL statement, look for RESERVED KEYWORDS for the underlying database.
In your case the word POSITION is a reserved keyword for MS-ACCESS.
Put it between square brackets
Dim sql = "INSERT INTO tbl_empinfo (EmpID, FirstName, LastName, Department, " & _
"[Position], Status, Years) " & _
"Values(empNum, empFname, empLname, empDept, empStat, empYears)"
However, you have another error in that query. You have 7 fields to insert but you pass only 6 parameters, missing just the parameter for the POSITION field.
You need to fix also the connection string. You write
con.ConnectionString = dbProvider & dbSource
but this result in an invalid file name
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=
c:\Databse\Company_db.accdbData Source= C:\Databse\Company_db.accdb"
(line splitted for readability)
I consider that you have initialized the object of "OleDbConnection".
Try to get the Connection String from the Property window of your database (From Server Explorer Window right click on your database select properties option).
And when you are opening the connection use following code:
If con.State = ConnectionState.Closed Then
con.Open()
End If
Hope it helps you.

Update a Single cell of a database table in VB.net

I am using MS Access Database. Now I have to update a particular cell value. Here is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String
Dim Presc As New System.Text.StringBuilder
Dim prs As String(), j As Integer
prs = Split(txtPrescription.Text, vbCrLf)
For j = 0 To prs.Length - 1
Presc.Append(prs(j))
Presc.Append(",")
Next
Try
str = Trim(lsvCase.SelectedItems(0).Text)
'MessageBox.Show(str)
Dim con As System.Data.OleDb.OleDbConnection
Dim ds As New DataSet
Dim rea As System.Data.OleDb.OleDbDataReader
con = New OleDb.OleDbConnection
Dim da As New System.Data.OleDb.OleDbDataAdapter
con.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source= F:\Sowmya Laptop Backups\sowdb1.accdb;"
con.Open()
Dim cmd As OleDb.OleDbCommand = con.CreateCommand
cmd.CommandText = "UPDATE Casehistory set Prescription =' " & Presc.ToString() & "'"
rea = cmd.ExecuteReader
cmd.ExecuteNonQuery()
da.FillSchema(ds, SchemaType.Mapped)
da.Update(ds, "Casehistory")
con.Close()
Catch ex As Exception
Finally
End Try
End Sub
This code updates all the cells in that column. I want to update only the particular cell having Case_ID = str
Where I have to add the WHERE clause (WHERE Case_ID = " & str & "
I would use command parameters, neater and prevents the SQL injection issue. Something like:
cmd.CommandText = "UPDATE Casehistory set Prescription =#Presc WHERE Case_ID = #CaseID"
cmd.Parameters.AddWithValue("#Presc", Presc.ToString())
cmd.Parameters.AddWithValue("#CaseID",str)
As an aside, having all this code in the button click event is less than ideal. You might want to investigate structuring your app in a more maintainable way - perhaps with a Data Layer for example.
The Where clause should be appended to the end of the OleDbCommmand.CommandText, where you define the Update statement.