Syntax error when running SQL Insert query

  • Thread starter Thread starter requeth
  • Start date Start date
R

requeth

Hi,

When I run the code below I receive an error message of Incorrect
syntax near the keyword 'primary' after executenonquery is run.
Primary being my table name for my database. I've tried to simplify
this code down as much as I can, but I can't seem to fix the syntax
issue. If you see what I did wrong please let me know, I'm at whits
end. As for my DB table, all I have is one field which is set to text,
so the data being inserted should work fine. This isn't being rejected
by the SQL server itself since nothing shows in the logs. Database
connection is functioning.

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.OleDb
Imports System.Web
Imports System.Data.SqlClient

Public Class DataAccess
Public Sub MyClickevent(ByVal Sender As Object, ByVal E As
EventArgs)
Dim currentContext As System.Web.HttpContext =
System.Web.HttpContext.Current
Dim conn As New SqlConnection()
conn.ConnectionString = "Data Source=localhost
\sqlexpress;Initial Catalog=corporations;Persist Security
Info=True;User ID=corpuser;Password=secretpassword"
Dim rowCount As Integer
Dim previousConnectionState As ConnectionState
Dim strSQL As String = "INSERT INTO primary values bob"
previousConnectionState = conn.State
Try
If conn.State = ConnectionState.Closed Then
conn.Open()

End If
Dim dbComm As New SqlCommand(strSQL, conn)
rowCount = dbComm.ExecuteNonQuery()
currentContext.Response.Write("A new record has been
added: " & rowCount)
currentContext.Response.End()
Catch ex As Exception
currentContext.Response.Write(ex.Message)
currentContext.Response.Write(ex.StackTrace)
currentContext.Response.End()
Finally
If previousConnectionState = ConnectionState.Closed Then
conn.Close()
End If
End Try

End Sub

End Class
 
Hi,

When I run the code below I receive an error message of Incorrect
syntax near the keyword 'primary' after executenonquery is run.
Primary being my table name for my database. I've tried to simplify
this code down as much as I can, but I can't seem to fix the syntax
issue. If you see what I did wrong please let me know, I'm at whits
end. As for my DB table, all I have is one field which is set to text,
so the data being inserted should work fine. This isn't being rejected
by the SQL server itself since nothing shows in the logs. Database
connection is functioning.

You should look at the code in the link. Maybe, you can come up with a
function that does the same.

http://www.sitescripts.com/ASP/Database_Tools/SQLBless.html

I don't know what "bob" is about.

INSERT INTO primary values bob

But if bob has the characters in it in the link above and bob has not
been blessed, then you will get T-SQL syntax error.
 
Hi,
Hello,

When I run the code below I receive an error message of Incorrect
syntax near the keyword 'primary' after executenonquery is run.
Primary being my table name for my database.

As «primary» is a keyword, I would try to enclose it with square
brackets (with MSSQL).
INSERT [primary] ...
 
Hi,

When I run the code below I receive an error message of Incorrect
syntax near the keyword 'primary' after executenonquery is run.
Dim strSQL As String = "INSERT INTO primary values bob"

Primary is probably a reserved keyword, the syntax of the insert is
wrong, and bob is not recognised at all.

A query that would have a chance to work would look like this:

"insert into [primary] values ('bob')"
 
Back
Top