Inserting New Record

  • Thread starter Thread starter Curt Emich
  • Start date Start date
C

Curt Emich

Can someone tell me specifically what's missing in this code so that it
won't write the record into the database? Please, no references to lengthy,
bloated, meandering articles telling me how to make a watch when I'm merely
asking what time it is.



Dim thisDataAdapter As OleDb.OleDbDataAdapter = New
OleDb.OleDbDataAdapter("SELECT * FROM Sessions", OleDbConnection1)

thisDataAdapter.InsertCommand = New OleDb.OleDbCommand("INSERT INTO Sessions
SET Notes = ? ", OleDbConnection1)

thisDataAdapter.InsertCommand.Parameters.Add("Notes", OleDbType.VarChar, 15,
"Notes")

' Dim workParm As OleDbParameter =
catDA.UpdateCommand.Parameters.Add("@CategoryID", OleDbType.Integer)

' workParm.SourceColumn = "CategoryID"

' workParm.SourceVersion = DataRowVersion.Original

Dim thisDataset As DataSet = New DataSet()

thisDataAdapter.Fill(thisDataset, "Sessions")

Dim thisTable As New DataTable()

thisTable = thisDataset.Tables("Sessions")

Dim cRow As DataRow = thisTable.NewRow()

cRow("Notes") = "Yadda"

thisTable.Rows.Add(cRow)

thisDataAdapter.Update(thisDataset)
 
You need to make a couple changes.

Change
thisDataAdapter.Update(thisDataset)
To
thisDataAdapter.Update(thisDataset, "Sessions")

Change
thisDataAdapter.InsertCommand = New OleDb.OleDbCommand("INSERT INTO
Sessions SET Notes = ? ", OleDbConnection1)
To
thisDataAdapter.InsertCommand = New OleDb.OleDbCommand("INSERT INTO
Sessions (Notes) VALUES(?) ", OleDbConnection1)

Good luck!
 
Thanks for getting back to me.

I smell a rat here. I've attempted to do this seven ways from Sunday
and nothing works. Check out this link:

http://www.dotnetextreme.com/code/BasicAdo.asp

There's an example there the most basic insert code I've seen yet. I
copied it virtually verbatim and I still get no new record in my
database.

Have you by any chance heard of MS Access being a problem in this
regard? I've checked the properties on the database and it's not read
only.

Thanks again for your help.
 
Actually I created a brand new Access file with a table called Session and a
column in that table named Notes (type was text). It worked just fine. Are
there any other columns in the table that are required? Also if you wrap the
code in a Try/Catch block you can get more information about the exception
that gets thrown.

Here's my full code (a console application):

Imports System.Data
Imports System.Data.OleDb

Module Module1

Sub Main()

Try
Dim OleDbConnection1 As New OleDb.OleDbConnection("Jet
OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database
Locking Mode=1;Jet OLEDB:Database Password=;Data Source=""C:\Documents and
Settings\agaskell\My Documents\db1.mdb"";Password=;Jet OLEDB:Engine
Type=5;Jet OLEDB:Global Bulk
Transactions=1;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System
database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet
OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet
OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica
Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False")
Dim thisDataAdapter As OleDb.OleDbDataAdapter = New
OleDb.OleDbDataAdapter("SELECT * FROM Sessions", OleDbConnection1)

thisDataAdapter.InsertCommand = New OleDb.OleDbCommand("INSERT
INTO Sessions (Notes) VALUES(?)", OleDbConnection1)
thisDataAdapter.InsertCommand.Parameters.Add("Notes",
OleDbType.VarChar, 15, "Notes")

Dim thisDataset As DataSet = New DataSet
thisDataAdapter.Fill(thisDataset, "Sessions")

Dim thisTable As New DataTable
thisTable = thisDataset.Tables("Sessions")
Dim cRow As DataRow = thisTable.NewRow()
cRow("Notes") = "Yadda2"

thisTable.Rows.Add(cRow)
thisDataAdapter.Update(thisDataset, "Sessions")

Catch e As Exception
Console.WriteLine(e.ToString())
Console.ReadLine()

End Try

End Sub

End Module
 
Back
Top