Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > INSERT INTO error

Reply
Thread Tools

INSERT INTO error

 
 
Gtbntgar
Guest
Posts: n/a
 
      02-24-2005
Please can someone look at my SQL command it is not working. It is giving
me a syntax error

Act = request.form("Act")
NT = request.form("NT")

Set Conn = Server.CreateObject("ADODB.Connection")
DSNtest="DRIVER={Microsoft Access Driver (*.mdb)}; "
DSNtest=dsntest & "DBQ=" & Server.MapPath("/db/invent.mdb")
Conn.Open DSNtest
Conn.execute ("INSERT INTO Table2 (TNumb, LogAct) VALUES (" & NT & ", '" &
Act & "')")

The TNumb field is numeric and LogAct is text. If I remove the TNumb info
it works.



 
Reply With Quote
 
 
 
 
Gérard Leclercq
Guest
Posts: n/a
 
      02-24-2005
Maybe this helps

Conn.execute ("INSERT INTO Table2 (TNumb, LogAct) VALUES (" & Clng(NT) & ",
'" & Act & "')")
or
Conn.execute ("INSERT INTO Table2 (TNumb, LogAct) VALUES (" & CDbl(NT) & ",
'" & Act & "')")



 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      02-24-2005
Gtbntgar wrote:
> Please can someone look at my SQL command it is not working. It is
> giving me a syntax error
>
> Act = request.form("Act")
> NT = request.form("NT")
>
> Set Conn = Server.CreateObject("ADODB.Connection")
> DSNtest="DRIVER={Microsoft Access Driver (*.mdb)}; "
> DSNtest=dsntest & "DBQ=" & Server.MapPath("/db/invent.mdb")


Nothing to do with your problem, but ODBC is obsolete. Use OLE DB:
DSNtest="Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("/db/invent.mdb")

> Conn.Open DSNtest
> Conn.execute ("INSERT INTO Table2 (TNumb, LogAct) VALUES (" & NT & ",
> '" & Act & "')")
>
> The TNumb field is numeric and LogAct is text. If I remove the TNumb
> info it works.


Nobody can debug a sql statement without knowing what it is. Do this:
Dim sSQL
sSQL = "INSERT INTO Table2 (TNumb, LogAct) " & _
"VALUES (" & NT & ",'" & Act & "')"

'comment out these two statements when finished debugging:
Response.Write sSQL
Response.End

DSNtest="Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("/db/invent.mdb")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open DSNtest
conn.execute sSQL,,129
conn.close: set conn=nothing

(the 129 is the combination of two constants: adCmdText (1) and
adExecuteNoRecords (12. You should always tell ADO what the command type
is, and whether or not the command needs to create a recordset to retrieve
data, which it does by default unless you tell it not to)

Look at the response-written string in the browser winow. If the problem is
not evident, copy it to the clipboard, open your database in Access, create
a new query in Design View, switch to SQL View, paste your statement in and
try to run it. You may get a better error message.

I prefer this technique for running sql statements built in code:

dim conn, sSQL, cmd,DSNtest,Act,NT
Act = request.form("Act")
NT = request.form("NT")
sSQL = "INSERT INTO Table2 (TNumb, LogAct) VALUES (?,?)"

'validate Act and NT at this point. Then:

DSNtest="Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("/db/invent.mdb")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open DSNtest
Set cmd= Server.CreateObject("ADODB.Command")
cmd.CommandType=1
cmd.CommandText=sSQL
Set cmd.ActiveConnection=conn
cmd.execute ,array(NT,Act),128

conn.close: set conn=nothing

This technique has several advantages:
1. No need to worry about apostrophes
2. No need to worry about sql injection

Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
Reply With Quote
 
Gtbntgar
Guest
Posts: n/a
 
      02-24-2005
Thank you very much for your detailed information.


"Bob Barrows [MVP]" <> wrote in message
news:...
> Gtbntgar wrote:
> > Please can someone look at my SQL command it is not working. It is
> > giving me a syntax error
> >
> > Act = request.form("Act")
> > NT = request.form("NT")
> >
> > Set Conn = Server.CreateObject("ADODB.Connection")
> > DSNtest="DRIVER={Microsoft Access Driver (*.mdb)}; "
> > DSNtest=dsntest & "DBQ=" & Server.MapPath("/db/invent.mdb")

>
> Nothing to do with your problem, but ODBC is obsolete. Use OLE DB:
> DSNtest="Provider=Microsoft.Jet.OLEDB.4.0;" & _
> "Data Source=" & Server.MapPath("/db/invent.mdb")
>
> > Conn.Open DSNtest
> > Conn.execute ("INSERT INTO Table2 (TNumb, LogAct) VALUES (" & NT & ",
> > '" & Act & "')")
> >
> > The TNumb field is numeric and LogAct is text. If I remove the TNumb
> > info it works.

>
> Nobody can debug a sql statement without knowing what it is. Do this:
> Dim sSQL
> sSQL = "INSERT INTO Table2 (TNumb, LogAct) " & _
> "VALUES (" & NT & ",'" & Act & "')"
>
> 'comment out these two statements when finished debugging:
> Response.Write sSQL
> Response.End
>
> DSNtest="Provider=Microsoft.Jet.OLEDB.4.0;" & _
> "Data Source=" & Server.MapPath("/db/invent.mdb")
> Set Conn = Server.CreateObject("ADODB.Connection")
> Conn.Open DSNtest
> conn.execute sSQL,,129
> conn.close: set conn=nothing
>
> (the 129 is the combination of two constants: adCmdText (1) and
> adExecuteNoRecords (12. You should always tell ADO what the command type
> is, and whether or not the command needs to create a recordset to retrieve
> data, which it does by default unless you tell it not to)
>
> Look at the response-written string in the browser winow. If the problem

is
> not evident, copy it to the clipboard, open your database in Access,

create
> a new query in Design View, switch to SQL View, paste your statement in

and
> try to run it. You may get a better error message.
>
> I prefer this technique for running sql statements built in code:
>
> dim conn, sSQL, cmd,DSNtest,Act,NT
> Act = request.form("Act")
> NT = request.form("NT")
> sSQL = "INSERT INTO Table2 (TNumb, LogAct) VALUES (?,?)"
>
> 'validate Act and NT at this point. Then:
>
> DSNtest="Provider=Microsoft.Jet.OLEDB.4.0;" & _
> "Data Source=" & Server.MapPath("/db/invent.mdb")
> Set Conn = Server.CreateObject("ADODB.Connection")
> Conn.Open DSNtest
> Set cmd= Server.CreateObject("ADODB.Command")
> cmd.CommandType=1
> cmd.CommandText=sSQL
> Set cmd.ActiveConnection=conn
> cmd.execute ,array(NT,Act),128
>
> conn.close: set conn=nothing
>
> This technique has several advantages:
> 1. No need to worry about apostrophes
> 2. No need to worry about sql injection
>
> Bob Barrows
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing XML into PHP to insert into a MySQL DB impulse() XML 0 10-13-2006 03:05 PM
converting a text file into an "insert into ..." file kublaikhan55@hotmail.com Ruby 5 07-23-2006 07:35 PM
How To Insert Code With Javascript, How to insert into a div an amountof code Sergio del Amo Javascript 4 05-29-2005 02:45 AM
Tools to extract data from SQL database and convert it into XML & insert XML data into SQL databases Harry Zoroc XML 1 07-12-2004 10:10 PM
adapter update problem Syntax error in INSERT INTO statement. compuglobalhypermeganetz0r ASP .Net 0 12-08-2003 05:03 AM



Advertisments