INSERT - SQL. Add Record. Problem. Need Urgent Help. Thank You.

S

Shapper

Hello,

I need to add a new record to an ACCESS database.

I get the error: Syntax error in INSERT INTO statement.

I have no idea what am I doing wrong. This is my code:

' Set Connection
Dim connectionString As String = AppSettings("connectionString")
Dim dbConnection As IDbConnection = New
OleDbConnection(connectionString)

' Set Query
Dim queryString As String = "INSERT INTO t_news (title_pt-PT,
title_en-GB, text_pt-PT, text_en-GB, publication_date) VALUES
(@title_pt-PT, @title_en-GB, @text_pt-PT, @text_en-GB,
@publication_date)"

Dim dbCommand As IDbCommand = New OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

' Add Parameters
With dbCommand.Parameters
.Add(New OleDbParameter("@title_pt-PT", "sample"))
.Add(New OleDbParameter("@title_en-GB", "sample"))
.Add(New OleDbParameter("@text_pt-PT", "sample"))
.Add(New OleDbParameter("@text_en-GB", "sample"))
.Add(New OleDbParameter("@publication_date", DateTime.Now))
End With

' Insert Record
dbConnection.Open
Try
dbCommand.ExecuteNonQuery
Finally
dbConnection.Close
End Try

Can someone tell me what is wrong?

Thanks,
Miguel
 
K

Karl Seguin

I think it's the - in the column names...try wrapping them in []

INSERT INTO t_news ([title_pt-PT],
[title_en-GB], [text_pt-PT], [text_en-GB], publication_date) VALUES
(@title_pt-PT, @title_en-GB, @text_pt-PT, @text_en-GB,
@publication_date)

Karl
 
S

Shapper

Hello,

I had that before but I took it. In fact I found where the problem is
but I am not able to solve it.

I am getting a "Data type mismatch in criteria expression" with the
publication_date field.

In my Access database I have this field as "Date/Time".
I don't know what is going wrong here.

Do you know?

Thanks,
Miguel

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message REMOVE @ REMOVE openmymind REMOVEMETOO .
ANDME net:
I think it's the - in the column names...try wrapping them in []

INSERT INTO t_news ([title_pt-PT],
[title_en-GB], [text_pt-PT], [text_en-GB], publication_date) VALUES
(@title_pt-PT, @title_en-GB, @text_pt-PT, @text_en-GB,
@publication_date)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)


Hello,

I need to add a new record to an ACCESS database.

I get the error: Syntax error in INSERT INTO statement.

I have no idea what am I doing wrong. This is my code:

' Set Connection
Dim connectionString As String = AppSettings("connectionString")
Dim dbConnection As IDbConnection = New OleDbConnection(connectionString)

' Set Query
Dim queryString As String = "INSERT INTO t_news (title_pt-PT,
title_en-GB, text_pt-PT, text_en-GB, publication_date) VALUES
(@title_pt-PT, @title_en-GB, @text_pt-PT, @text_en-GB, @publication_date)"

Dim dbCommand As IDbCommand = New OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

' Add Parameters
With dbCommand.Parameters
.Add(New OleDbParameter("@title_pt-PT", "sample"))
.Add(New OleDbParameter("@title_en-GB", "sample"))
.Add(New OleDbParameter("@text_pt-PT", "sample"))
.Add(New OleDbParameter("@text_en-GB", "sample"))
.Add(New OleDbParameter("@publication_date", DateTime.Now))
End With

' Insert Record
dbConnection.Open
Try
dbCommand.ExecuteNonQuery
Finally
dbConnection.Close
End Try

Can someone tell me what is wrong?

Thanks,
Miguel
 
B

bhawin13

Specify datatype of last parameter

dbCommand.Parameters.Add("@publication_d­ate", OleDbType.DBDate).Value
= "yyyymmdd";

Here in add method first arg is parameter name
second is type of field
Refer msdn for add method of parameter collection of command.

bhawin13
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top