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

Recommended Answers

All 5 Replies

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

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?

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.

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.

hollystyles your the best!
You really helped me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.