G
Guest
When I run the simple code below I get the following
error message:
Type = System.Data.OleDb.OleDbException
Message = Syntax error in INSERT INTO statement
I've tweaked the insert to statement every which way.
All the other ways create earlier error messages. I've
compared this insert to statement to many I've seen in
the book ado.net core reference, as well as other books.
For the life of me I can't see what's wrong with it.
Perhaps one of you can help me.
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form
Designer.
InitializeComponent()
'Add any initialization after the
InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component
list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnBuild As
System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.btnBuild = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'btnBuild
'
Me.btnBuild.Location = New System.Drawing.Point
(16, 24)
Me.btnBuild.Name = "btnBuild"
Me.btnBuild.Size = New System.Drawing.Size(72, 24)
Me.btnBuild.TabIndex = 0
Me.btnBuild.Text = "Build"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.btnBuild)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnBuild_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnBuild.Click
Dim strConn, strSQL As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
Dim cn As New OleDbConnection(strConn)
strSQL = "SELECT * FROM DateType ORDER BY
DateType;"
Dim da As New OleDbDataAdapter(strSQL, cn)
Dim ds As New DataSet
ds.DataSetName = "ds1"
cn.Open()
da.FillSchema(ds, SchemaType.Source, "DateType")
'cn.Close()
ds.WriteXmlSchema("H:\HasbaraNET\ado.net
tests\CodeUpdate\ds.xsd")
Dim dsA As New ds1
da.Fill(dsA, dsA.Tables(0).TableName)
Dim tblDateType As ds1.DateTypeDataTable =
dsA.Tables(0)
Dim rowDateType As ds1.DateTypeRow
rowDateType = tblDateType.NewDateTypeRow
rowDateType.DateType = "xxx"
rowDateType.CreateDate = Now
rowDateType.ChangeDate = Now
rowDateType.Active = True
rowDateType.ID = Integer.MaxValue
tblDateType.AddDateTypeRow(rowDateType)
da.InsertCommand = New OleDb.OleDbCommand("INSERT
INTO datetype(ID,DateType,CreateDate,ChangeDate,Active)
(?,?,?,?,?)", cn)
'INSERT INTO [Order Details] (OrderID, ProductID,
Quantity, UnitPrice) VALUES (?, ?, ?, ?)
'Try
' da.InsertCommand.Parameters.Add("@ID",
OleDb.OleDbType.Integer,4, "ID")
'Catch er As Exception
' MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
'End Try
da.InsertCommand.Parameters.Add("@DateType",
OleDb.OleDbType.VarChar, 255, "DateType")
da.InsertCommand.Parameters.Add("@CreateDate",
OleDb.OleDbType.Date, 8, "CreateDate")
da.InsertCommand.Parameters.Add("@ChangeDate",
OleDb.OleDbType.Date, 8, "ChangeDate")
da.InsertCommand.Parameters.Add("@Active",
OleDb.OleDbType.Boolean, 2, "Active")
Try
da.Update(dsA, "DateType")
Catch er As System.Exception
MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
Finally
cn.Close()
End Try
End Sub
End Class
error message:
Type = System.Data.OleDb.OleDbException
Message = Syntax error in INSERT INTO statement
I've tweaked the insert to statement every which way.
All the other ways create earlier error messages. I've
compared this insert to statement to many I've seen in
the book ado.net core reference, as well as other books.
For the life of me I can't see what's wrong with it.
Perhaps one of you can help me.
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form
Designer.
InitializeComponent()
'Add any initialization after the
InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component
list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnBuild As
System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.btnBuild = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'btnBuild
'
Me.btnBuild.Location = New System.Drawing.Point
(16, 24)
Me.btnBuild.Name = "btnBuild"
Me.btnBuild.Size = New System.Drawing.Size(72, 24)
Me.btnBuild.TabIndex = 0
Me.btnBuild.Text = "Build"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.btnBuild)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnBuild_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnBuild.Click
Dim strConn, strSQL As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
Dim cn As New OleDbConnection(strConn)
strSQL = "SELECT * FROM DateType ORDER BY
DateType;"
Dim da As New OleDbDataAdapter(strSQL, cn)
Dim ds As New DataSet
ds.DataSetName = "ds1"
cn.Open()
da.FillSchema(ds, SchemaType.Source, "DateType")
'cn.Close()
ds.WriteXmlSchema("H:\HasbaraNET\ado.net
tests\CodeUpdate\ds.xsd")
Dim dsA As New ds1
da.Fill(dsA, dsA.Tables(0).TableName)
Dim tblDateType As ds1.DateTypeDataTable =
dsA.Tables(0)
Dim rowDateType As ds1.DateTypeRow
rowDateType = tblDateType.NewDateTypeRow
rowDateType.DateType = "xxx"
rowDateType.CreateDate = Now
rowDateType.ChangeDate = Now
rowDateType.Active = True
rowDateType.ID = Integer.MaxValue
tblDateType.AddDateTypeRow(rowDateType)
da.InsertCommand = New OleDb.OleDbCommand("INSERT
INTO datetype(ID,DateType,CreateDate,ChangeDate,Active)
(?,?,?,?,?)", cn)
'INSERT INTO [Order Details] (OrderID, ProductID,
Quantity, UnitPrice) VALUES (?, ?, ?, ?)
'Try
' da.InsertCommand.Parameters.Add("@ID",
OleDb.OleDbType.Integer,4, "ID")
'Catch er As Exception
' MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
'End Try
da.InsertCommand.Parameters.Add("@DateType",
OleDb.OleDbType.VarChar, 255, "DateType")
da.InsertCommand.Parameters.Add("@CreateDate",
OleDb.OleDbType.Date, 8, "CreateDate")
da.InsertCommand.Parameters.Add("@ChangeDate",
OleDb.OleDbType.Date, 8, "ChangeDate")
da.InsertCommand.Parameters.Add("@Active",
OleDb.OleDbType.Boolean, 2, "Active")
Try
da.Update(dsA, "DateType")
Catch er As System.Exception
MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
Finally
cn.Close()
End Try
End Sub
End Class