User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 392,075 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,118 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 5537 | Replies: 4 | Solved
Reply
Join Date: Sep 2006
Posts: 9
Reputation: caterpillar is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
caterpillar caterpillar is offline Offline
Newbie Poster

Help Insert Into Syntax Problem

  #1  
Sep 20th, 2006
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Posts: 7
Reputation: itsdavetime is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
itsdavetime itsdavetime is offline Offline
Newbie Poster

Re: Insert Into Syntax Problem

  #2  
Sep 21st, 2006
You're not substituting Me.classname.UserID as a variable. You're passing it in the code as a value itself.
Reply With Quote  
Join Date: Sep 2006
Posts: 9
Reputation: caterpillar is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
caterpillar caterpillar is offline Offline
Newbie Poster

Re: Insert Into Syntax Problem

  #3  
Sep 21st, 2006
Originally Posted by itsdavetime View Post
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?
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,164
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 58
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Insert Into Syntax Problem

  #4  
Sep 21st, 2006
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.
Last edited by hollystyles : Sep 21st, 2006 at 10:37 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Sep 2006
Posts: 9
Reputation: caterpillar is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
caterpillar caterpillar is offline Offline
Newbie Poster

Re: Insert Into Syntax Problem

  #5  
Sep 21st, 2006
Originally Posted by hollystyles View Post
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 12:10 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC