0

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

4
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by shlimeel
0

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

0

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?

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.

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.

Thanks a lot !

It worked.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.