Home All Groups Group Topic Archive Search About

insert string to SQL Server 2000

Author
7 Jul 2005 1:16 PM
Lam
Hi
I try to write a C# program which will get an input string and
I need to insert to the SQL database. I set the column type as Text.
and when I insert the string variable, for exmaple, "Now", it gave the error
"
The name 'Now' is not permitted in this context. Only constants,
expressions, or variables allowed here. Column names are not permitted. "


I try to change the column type "nvachar", it gives me the same error
does anyone know how to fix it?
Thanks  a lot

Author
7 Jul 2005 1:50 PM
Alejandro Mesa
How are you inserting the row?.

Do not use double quotation mark to delimit the string, because if
QUOTED_IDENTIFIER is on, then all strings delimited by double quotation marks
are interpreted as object identifiers. Use single quotation mark instead. You
can check the settings using "dbcc useroptions".

use northwind
go

create table t1 (
c1 varchar(50)
)
go

SET QUOTED_IDENTIFIER on
go

insert into t1 values("this will give an error.")
go

insert into t1 values('this will not give an error.')
go

SET QUOTED_IDENTIFIER off
go

insert into t1 values('this will not give an error.')
go

insert into t1 values("this will not give an error.")
go

SET QUOTED_IDENTIFIER on
go

select * from t1
go

drop table t1
go


AMB

Show quote
"Lam" wrote:

> Hi
> I try to write a C# program which will get an input string and
> I need to insert to the SQL database. I set the column type as Text.
> and when I insert the string variable, for exmaple, "Now", it gave the error
> "
> The name 'Now' is not permitted in this context. Only constants,
> expressions, or variables allowed here. Column names are not permitted. "
>
>
> I try to change the column type "nvachar", it gives me the same error
> does anyone know how to fix it?
> Thanks  a lot
>
>
>
>
Author
7 Jul 2005 3:05 PM
Lam
I store a input string in a variable
then I use insert command to insert
      insertQuery="INSERT INTO Log (Name) VALUES ("+salesName+")";
where salesName is a string variable
how can I fix it?


Show quote
"Alejandro Mesa" <AlejandroM***@discussions.microsoft.com> wrote in message
news:800D594F-A3F2-4E59-9B59-18FB9DFBBE72@microsoft.com...
> How are you inserting the row?.
>
> Do not use double quotation mark to delimit the string, because if
> QUOTED_IDENTIFIER is on, then all strings delimited by double quotation
marks
> are interpreted as object identifiers. Use single quotation mark instead.
You
> can check the settings using "dbcc useroptions".
>
> use northwind
> go
>
> create table t1 (
> c1 varchar(50)
> )
> go
>
> SET QUOTED_IDENTIFIER on
> go
>
> insert into t1 values("this will give an error.")
> go
>
> insert into t1 values('this will not give an error.')
> go
>
> SET QUOTED_IDENTIFIER off
> go
>
> insert into t1 values('this will not give an error.')
> go
>
> insert into t1 values("this will not give an error.")
> go
>
> SET QUOTED_IDENTIFIER on
> go
>
> select * from t1
> go
>
> drop table t1
> go
>
>
> AMB
>
> "Lam" wrote:
>
> > Hi
> > I try to write a C# program which will get an input string and
> > I need to insert to the SQL database. I set the column type as Text.
> > and when I insert the string variable, for exmaple, "Now", it gave the
error
> > "
> > The name 'Now' is not permitted in this context. Only constants,
> > expressions, or variables allowed here. Column names are not permitted.
"
> >
> >
> > I try to change the column type "nvachar", it gives me the same error
> > does anyone know how to fix it?
> > Thanks  a lot
> >
> >
> >
> >
Author
7 Jul 2005 4:02 PM
Daniel Wilson
Wrap your value in single quotes.

.... VALUES ( '" + salesName + "' )";

In case it's hard to see, that's parenthesis single-quote double-quote plus
variablename plus double-quote single-quote parenthesis

hth,

--
Daniel Wilson
Senior Software Solutions Developer
Embtrak Development Team
http://www.Embtrak.com
DVBrown Company

Show quote
"Lam" <javabeanb***@hotmail.com> wrote in message
news:OORVcYwgFHA.1248@TK2MSFTNGP12.phx.gbl...
> I store a input string in a variable
> then I use insert command to insert
>       insertQuery="INSERT INTO Log (Name) VALUES ("+salesName+")";
> where salesName is a string variable
> how can I fix it?
>
>
> "Alejandro Mesa" <AlejandroM***@discussions.microsoft.com> wrote in
message
> news:800D594F-A3F2-4E59-9B59-18FB9DFBBE72@microsoft.com...
> > How are you inserting the row?.
> >
> > Do not use double quotation mark to delimit the string, because if
> > QUOTED_IDENTIFIER is on, then all strings delimited by double quotation
> marks
> > are interpreted as object identifiers. Use single quotation mark
instead.
> You
> > can check the settings using "dbcc useroptions".
> >
> > use northwind
> > go
> >
> > create table t1 (
> > c1 varchar(50)
> > )
> > go
> >
> > SET QUOTED_IDENTIFIER on
> > go
> >
> > insert into t1 values("this will give an error.")
> > go
> >
> > insert into t1 values('this will not give an error.')
> > go
> >
> > SET QUOTED_IDENTIFIER off
> > go
> >
> > insert into t1 values('this will not give an error.')
> > go
> >
> > insert into t1 values("this will not give an error.")
> > go
> >
> > SET QUOTED_IDENTIFIER on
> > go
> >
> > select * from t1
> > go
> >
> > drop table t1
> > go
> >
> >
> > AMB
> >
> > "Lam" wrote:
> >
> > > Hi
> > > I try to write a C# program which will get an input string and
> > > I need to insert to the SQL database. I set the column type as Text.
> > > and when I insert the string variable, for exmaple, "Now", it gave the
> error
> > > "
> > > The name 'Now' is not permitted in this context. Only constants,
> > > expressions, or variables allowed here. Column names are not
permitted.
> "
> > >
> > >
> > > I try to change the column type "nvachar", it gives me the same error
> > > does anyone know how to fix it?
> > > Thanks  a lot
> > >
> > >
> > >
> > >
>
>

AddThis Social Bookmark Button