Syntax error when running SQL Insert query

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
0
requeth
3/8/2010 8:41:10 PM
dotnet.general 306 articles. 0 followers. Follow

3 Replies
933 Views

Similar Articles

[PageSpeed] 44

requeth@gmail.com wrote:
> 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.
0
Mr
3/9/2010 1:58:31 AM
<requeth@gmail.com> a �crit dans le message de groupe de discussion : 
d985d594-727c-45c8-99f0-fe35e930839f@q15g2000yqj.googlegroups.com...
> 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] ...

-- 
Fred
foleide@free.fr 

0
Fred
3/9/2010 4:07:55 AM
requeth@gmail.com wrote:
> 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')"

-- 
G�ran Andersson
_____
http://www.guffa.com
0
ISO
3/9/2010 12:56:33 PM
Reply:

Similar Artilces: