VB.NET error adding a record to database - vb.net

I'm using the code from http://homeandlearn.co.uk/NET/nets12p9.html for adding a record to the database.
It says when using a commandbuilder I should not get the error message:
Update requires a valid InsertCommand when passed DataRow collection with new rows.
However when I do the update I still get the error message. How can I fix this?
This is my code:
Dim dbProv As String
Dim dbSource As String
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String
Dim Command As OleDb.OleDbCommand
Dim dr As DataRow
Dim cb As New OleDb.OleDbCommandBuilder(da)
sql = "SELECT * FROM Cliënten"
dbProv = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = [mydatabase]"
con.ConnectionString = dbProv & dbSource
con.Open()
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Cliënten")
dr = ds.Tables("Cliënten").NewRow()
dr.Item("Field1") = TextBox1.Text
dr.Item("Field2") = TextBox2.Text
ds.Tables("Cliënten").Rows.Add(dr)
da.Update(ds, "Cliënten")
MsgBox("New Record added to the Database")
con.Close()

To make da.Update() works, you must assign a valid InsertCommand, then the DataAdapter will execute it automatically. Here an example:
da.InsertCommand = New OleDb.OleDbCommand("INTERT INTO Cliënten (Field1, Field2) VALUES (#field1, #field2)")
da.InsertCommand.Parameters.Add(New OleDb.OleDbParameter("#field1", OleDb.OleDbType.VarChar, 0, "Field1"))
da.InsertCommand.Parameters.Add(New OleDb.OleDbParameter("#field2", OleDb.OleDbType.VarChar, 0, "Field2"))
da.Update(ds, "Cliënten")
WARNING: I've presumed you are using OleDb.OleDbType.VarChar for Field1 and Field2; if not you have to replace it with correct DB data format.

From what I read here it seems as though CommandBuilder should auto-generate the INSERT command for you based on the SELECT.
I think you are creating your CommandBuilder object too early - ie Before you specify the SELECT command / initialise Connection etc.
Perhaps this may work better...
Dim dbProv As String
Dim dbSource As String
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String
Dim Command As OleDb.OleDbCommand
Dim dr As DataRow
sql = "SELECT * FROM Cliënten" 'Consider specifying columns
'individually rather than using *
dbProv = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = [mydatabase]"
con.ConnectionString = dbProv & dbSource
con.Open()
da = New OleDb.OleDbDataAdapter(sql, con)
Dim cb As New OleDb.OleDbCommandBuilder(da) 'Init CommandBuilder here
cb.RefreshSchema() 'This may also help
da.Fill(ds, "Cliënten")
dr = ds.Tables("Cliënten").NewRow()
dr.Item("Field1") = TextBox1.Text
dr.Item("Field2") = TextBox2.Text
ds.Tables("Cliënten").Rows.Add(dr)
da.Update(ds, "Cliënten")
MsgBox("New Record added to the Database")
con.Close()

Related

Crystal Report not showing data even though dataset contains data from MSAccess - what am I missing?

Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim ssql As String
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= 'C:\Users\xxxx\xxxxx\xxxxxxxxe/xxxxxx.accdb'"
con.Open()
ssql = "SELECT * FROM tblTempProformaInvoice"
da = New OleDb.OleDbDataAdapter(ssql, con)
da.Fill(ds, "GetProformaInvoice")
con.Close()
MsgBox(ds.Tables("GetProformaInvoice").Rows(0).Item("spinvnr").ToString)
Dim rpt As New PFI
rpt.SetDataSource(ds)
CrystalReportViewer1.ReportSource = rpt
CrystalReportViewer1.Refresh()
try
rpt.SetDataSource(ds.Tables(0))

Datagridview WHERE SQL Error

I'm trying to automatically fill a datagridview upon loading. This is what I have so far
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Administratot\Downloads\RailwayDatabase2.accdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("Select * from tbl_shifts WHERE EmployeeName = '" & EmployeeLogin.usersname & "' AND Completed = True", MyConn)
Dim view As New DataView(tables(0))
source1.DataSource = view
DataGridView2.DataSource = view
When I attempt this, I am met with an error reading
Cannot find table 0.
You must fill the dataset first.
da = New OleDbDataAdapter("Select * from tbl_shifts WHERE EmployeeName = '" & EmployeeLogin.usersname & "' AND Completed = True", MyConn)
da.Fill(ds)
Dim view As New DataView(tables(0))
This might help. Keep it simple.
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Administratot\Downloads\RailwayDatabase2.accdb"
Dim MyConn As New OleDbConnection(connString)
Dim da As OleDbDataAdapter
Dim ds As DataSet
--For error handling, do this
Try
'Open the connection
MyConn.Open()
'Fill the dataset
da = New OleDbDataAdapter("Select * from tbl_shifts WHERE EmployeeName = '" & EmployeeLogin.usersname & "' AND Completed = True", MyConn)
ds = New DataSet
da.Fill(ds)
'Fill datagridview
DataGridView2.DataSource = ds.Tables(0)
'Close the connection
MyConn.Close()
Catch ex As Exception
'Make sure connection is closed
If MyConn.State <> ConnectionState.Closed Then MyConn.Close()
End Try
And of course, you will put second group of code in Form_Load Event.

DataSet update error requires InsertCommand

Trying to add a row to an Access 2010 database with VB 2013. Everything seems to work until the UPDATE statement when I get this error:
Update requires a valid InsertCommand when passed DataRow collection with new rows.
My code is:
Dim sqlConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\CLI_CRVM.accdb")
Dim cmd As New System.Data.OleDb.OleDbCommand()
Dim ds As New DataSet1()
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "SELECT * FROM [extract] ;"
cmd.Connection = sqlConnection
sqlConnection.Open()
Dim da = New OleDb.OleDbDataAdapter(cmd.CommandText, sqlConnection)
da.Fill(ds, "extract")
ds.Clear()
Dim newExtractRow As DataRow = ds.Tables("extract").NewRow()
newExtractRow("Field1") = "ABC123"
ds.Tables("Extract").Rows.Add(newExtractRow)
da.Update(ds, "Extract")
sqlConnection.Close()
Everything I've found so far seems to reference an SQL database, not OleDb connection.
You can probably use an OleDbCommandBuilder, like so
' your existing OleDbDataAdapter
Dim da = New OleDb.OleDbDataAdapter(cmd.CommandText, sqlConnection)
' add the following lines:
Dim cb = New OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
For details, see
OleDbCommandBuilder Class
Working Example
For an Access table named extract with fields
ID - AutoNumber, Primary Key
Field1 - Text(255)
and data
ID Field1
-- -------
1 TEST999
the following VB.NET code will insert a second row into the table in the Access database
Using con As New OleDbConnection
con.ConnectionString =
"Provider=Microsoft.ACE.OLEDB.12.0;" &
"Data Source=C:\Users\Public\Database1.accdb;"
con.Open()
Using da As New OleDbDataAdapter("SELECT * FROM [extract]", con)
Dim cb = New OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
Dim dt = New DataTable
da.Fill(dt)
Dim dr As DataRow = dt.NewRow
dr("Field1") = "ABC123"
dt.Rows.Add(dr)
da.Update(dt)
End Using
con.Close()
End Using
with the result
ID Field1
-- -------
1 TEST999
2 ABC123
The OleDbCommandBuilder object automatically (and invisibly) creates the INSERT, UPDATE, and DELETE commands based on the SELECT command that was supplied when the OleDbDataAdapter object was created.
You need to set the InsertCommand property
Dim da = New OleDb.OleDbDataAdapter(cmd.CommandText, sqlConnection)
da.InsertCommand = cmd
da.Fill(ds, "extract")
More info here

DataTable.Rows.Find error: "Table doesn't have a primary key." [duplicate]

This question already has an answer here:
Table doesn't have a primary key
(1 answer)
Closed 9 years ago.
I can't search my datatable. It keeps repeating the same error "table doesn't have a primary key." and I don't know what to do. I already added the .FillSchema, but it still won't work. I'm using Visual Basic.
Dim con As New OleDb.OleDbConnection
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim sql As String
Dim pass As Integer
Dim cmd As New OleDb.OleDbCommand
Dim dr As OleDb.OleDbDataReader
Dim dRow As DataRow
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Drug Emporium.accdb"
con.Open()
cmd.CommandText = "Select * From Inventory"
cmd.CommandType = CommandType.Text
cmd.Connection = con
sql = "Select * From Inventory"
dr = cmd.ExecuteReader()
da = New OleDb.OleDbDataAdapter(sql, con)
da.FillSchema(ds, SchemaType.Source, "Drug Emprorium")
da.Fill(ds, "Drug Emporium")
Dim cb As New OleDb.OleDbCommandBuilder(da)
dRow = ds.Tables("Drug Emporium").Rows.Find(intItemNum)
dRow.Delete()
con.Close()
I tried this but it still didn't work. It now gives a null reference exception on the Delete()
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Drug Emporium.accdb"
con.Open()
cmd.CommandText = "Select * From Inventory"
cmd.CommandType = CommandType.Text
cmd.Connection = con
sql = "Select * From Inventory"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Drug Emporium")
da.FillSchema(ds, SchemaType.Source, "Drug Emprorium")
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("Drug Emporium").PrimaryKey = New DataColumn() {ds.Tables("Drug Emporium").Columns("InventoryNumber")}
dRow = ds.Tables("Drug Emporium").Rows.Find(intItemNum)
dRow.Delete()
con.Close()
Your Problem is in the Table structure. You should normally show the DDL (SQL to create the table), but I understand it is uncommon with Access.
Anyway, I suspect your error happens in the delete: if you want to modify an resultset some Drivers need a primary key in the Table (and the selected colums) in order to be able to sent a clear modification operation back to the DB.

How to determine the id of a data in vb.net?

I'm currently working on an ms access database in vb.net.
And I need a code that can determine which data I am updating in the database. Because when I try to update the data, the previous data is being cloned and it will generate two data(the updated and the previous data), and the program will also generate a random id number for the updated data, which is not good.
Here is my code for the update button:
'update
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("GH").Rows(INC).Item(1) = TextBox13.Text
ds.Tables("GH").Rows(INC).Item(2) = TextBox14.Text
ds.Tables("GH").Rows(INC).Item(3) = TextBox15.Text
da.Update(ds, "GH")
MsgBox("Data updated")
My code for form load:
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\ACCESS DATABASE\search.mdb"
con.Open()
sql = "SELECT * FROM GH"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "GH")
con.Close()
I'm using system.data.oledb namespace
Here are my declarations:
Dim cmd As OleDbCommand
Dim cn As OleDbConnection
Dim dr As OleDbDataReader
Dim ds As New DataSet
Dim con As New OleDb.OleDbConnection
Dim da As OleDb.OleDbDataAdapter
Your question is not clear to me at all, but if you want to find the last ID of an insert, you would usually check the results of "SELECT ##IDENTITY" immediately after the record is inserted.