Tuhin Bhatt
Tuhin Bhatt

Reputation: 362

Sql Command Error (Asp.net)

I Get Error when i use the following code

str = "Insert into BasicData (Id,First name,Last name,std) values (@sid,@firstname,@lastname,@std)"

The Error i get is Near Name

But when i remove spaces between First name

If i make it

str = "Insert into BasicData (Id,Firstname,Lastname,std) values (@sid,@firstname,@lastname,@std)"

The error goes away.... I gave column names First Name in the Database's Table( I also changed it to Firstname-so the error went away)

So does Sql Database permit you to have Spaces in Column Names??? If it does how should i write the sql insert statement

Upvotes: 0

Views: 164

Answers (4)

J. Steen
J. Steen

Reputation: 15578

insert into basicdata (id, [First name], [Last name], std)
    values (@sid,@firstname,@lastname,@std)

Note the brackets, which will allow you to reference columns with spaces in their name. While this is not good practice, it's definitely allowed by the system. You should probably stay away from such design in the future though. Makes it harder to read. =)

Upvotes: 5

Tim
Tim

Reputation: 5421

column names with spaces must be wrapped in [brackets].

Upvotes: 0

Aziz Shaikh
Aziz Shaikh

Reputation: 16524

Try this:

Insert into BasicData ([Id],[First name],[Last name],[std]) values (@sid,@firstname,@lastname,@std)

Upvotes: 0

RobertoBr
RobertoBr

Reputation: 1801

It is not a good pratice. But you could use [] for the collumn names.

Ex. ", [First Name], [Segond Name]," and so on..

Upvotes: 3

Related Questions