devxlogo

Write Correct INSERT SQL Statement With ASP

Write Correct INSERT SQL Statement With ASP

Question:

I am trying to update an Access database with an ASP page. My ASP page contains a guest book with first name, last name, and other relevant text boxes. After pressing the submit button on the ASP form, the text box values are sent to the server and my script places them into two Dim variables called FirstName and LastName. A connection with the database is opened called dbconn, and then I attempt to create a new record in the database with this line:

dbconn.execute ("Insert Into GuestTable (FirstName,LastName) Values (FirstString, LastString)")

I get this error message:

Too few parameters. Expected 2. 

I have tried many variations and nothing has worked.

Answer:

You cannot use the variable NAMES inside your SQL string. You need to use the variable VALUES. Concatenate the variable values to generate the SQL statement and then proceed.

Dim strSQLstrSQL = "Insert Into GuestTable (FirstName,LastName) Values (" & FirstString & "," & LastString & ")"dbconn.execute (strSQL)

Assuming that the variables hold string values, you will need to enclose them within single quotes:

strSQL = "Insert Into GuestTable (FirstName,LastName) Values ('" & FirstString & "', '" & LastString & "')"

See also  How IoT Tech Will Transform Logistics
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist