problem with insert command

D

Dan

Hi,

i try to insert values into a sql server table like this (code-behind):

comm1 = Textbox1.Text
comm2=Textbox2.Text
.......

comd.CommandText = "insert into [dbo].[mytable] (field1, field2,...)
values(comm1, comm2, ....)"

i get the error:
The name "field1l" is not permitted in this context. Valid expressions are
constants, constant expressions, and (in some contexts) variables. Column
names are not permitted.

Can someone give me the right syntax?
Thanks in advance.
Dan
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Dan said:
Hi,

i try to insert values into a sql server table like this (code-behind):

comm1 = Textbox1.Text
comm2=Textbox2.Text
......

comd.CommandText = "insert into [dbo].[mytable] (field1, field2,...)
values(comm1, comm2, ....)"

i get the error:
The name "field1l" is not permitted in this context. Valid expressions are
constants, constant expressions, and (in some contexts) variables. Column
names are not permitted.

Can someone give me the right syntax?
Thanks in advance.
Dan

The database is not aware of the variables that you use in your code.

Put parameters in the query, and add the values as parameter objects to
the command object.
 
L

Lloyd Sheen

Dan said:
Hi,

i try to insert values into a sql server table like this (code-behind):

comm1 = Textbox1.Text
comm2=Textbox2.Text
......

comd.CommandText = "insert into [dbo].[mytable] (field1, field2,...)
values(comm1, comm2, ....)"

i get the error:
The name "field1l" is not permitted in this context. Valid expressions are
constants, constant expressions, and (in some contexts) variables. Column
names are not permitted.

Can someone give me the right syntax?
Thanks in advance.
Dan

Your SQL statement is in quotes so the comm1, comm2 are sent as those values
to the server which has no idea what you want to do.

Use parameters to pass the values of the fields. Something like "insert
into mytable (field1,field2) values (@field1, @field2)

Then in your code create the parameters, add them to the SQLCommand and then
execute.

Hope this gets you going in the right direction.
Lloyd Sheen
 
R

Rad [Visual C# MVP]

Hi,

i try to insert values into a sql server table like this (code-behind):

comm1 = Textbox1.Text
comm2=Textbox2.Text
......

comd.CommandText = "insert into [dbo].[mytable] (field1, field2,...)
values(comm1, comm2, ....)"

i get the error:
The name "field1l" is not permitted in this context. Valid expressions are
constants, constant expressions, and (in some contexts) variables. Column
names are not permitted.

Can someone give me the right syntax?
Thanks in advance.
Dan
You need to read up on parameterized queries ... then you'll know how
to safely pass user entered variables to database queries
 
D

Dan

Of course, thanks ..

Lloyd Sheen said:
Dan said:
Hi,

i try to insert values into a sql server table like this (code-behind):

comm1 = Textbox1.Text
comm2=Textbox2.Text
......

comd.CommandText = "insert into [dbo].[mytable] (field1, field2,...)
values(comm1, comm2, ....)"

i get the error:
The name "field1l" is not permitted in this context. Valid expressions
are constants, constant expressions, and (in some contexts) variables.
Column names are not permitted.

Can someone give me the right syntax?
Thanks in advance.
Dan

Your SQL statement is in quotes so the comm1, comm2 are sent as those
values to the server which has no idea what you want to do.

Use parameters to pass the values of the fields. Something like "insert
into mytable (field1,field2) values (@field1, @field2)

Then in your code create the parameters, add them to the SQLCommand and
then execute.

Hope this gets you going in the right direction.
Lloyd Sheen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top