954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Insert Into Syntax Problem

Hi,

I have to insert fields in a table. I am using VB.net & SQL Server database.
The code is:
Dim userid As Int32 = Me.classname.UserID
Dim strSQLD As String = "Insert into table1 (userid) values (Me.classname.UserID)"

userid is of type int
While debugging, i can see that variable userid is getting the interegr value.

This is giving Syntax Error:The name 'userid' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.

Any help is appreciated.

Regards
Monica

caterpillar
Newbie Poster
9 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

You're not substituting Me.classname.UserID as a variable. You're passing it in the code as a value itself.

itsdavetime
Newbie Poster
7 posts since Aug 2006
Reputation Points: 10
Solved Threads: 1
 
You're not substituting Me.classname.UserID as a variable. You're passing it in the code as a value itself.


I have tried this also:

Dim var1 As Int32 = Me.classname.UserID
Dim strSQLD As String = "Insert into table1 (userid) values (var1)"

But it did not work. It is giving this error:
The name 'var1' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.

Any solutions?

caterpillar
Newbie Poster
9 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 
Dim userid As Int32 = Me.classname.UserID
Dim strSQLD As String = "Insert into table1 (userid) values (" & Me.classname.UserID & ")"


You need to concatenate the value into the string, I beleive VB.Net will implicitly convert UserID to a string, otherwise convert/cast it first.

UserId by the way is a keyword in TSQL so if you have to use it in any MS SQL strings always put [] around it [UserId] so it is evaluated literally by MSSQL's query engine and optimizer.

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 
Dim userid As Int32 = Me.classname.UserID
Dim strSQLD As String = "Insert into table1 (userid) values (" & Me.classname.UserID & ")"

You need to concatenate the value into the string, I beleive VB.Net will implicitly convert UserID to a string, otherwise convert/cast it first.

UserId by the way is a keyword in TSQL so if you have to use it in any MS SQL strings always put [] around it [UserId] so it is evaluated literally by MSSQL's query engine and optimizer.

Thanks a lot !

It worked.

caterpillar
Newbie Poster
9 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

hollystyles your the best!
You really helped me.

shlimeel
Newbie Poster
1 post since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You