Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Problem with Insert Statement

Author  Topic 

basic
Starting Member

2 Posts

Posted - 2006-07-21 : 03:15:28
I am new to VB.net..I have the following problem with the insert statement..
trno=5
avail=10
Dim cmd1 As New SqlCommand
cmd1.commandtext = "insert into available"+"(trainno,availability)" +"VALUES(&trno&,&avail&)"
It gives me an error..

If i directly use values instead of variable its working fine..
I tried various different options of this statement but in vain..
Kindly help me..Urgent..

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-07-21 : 03:54:02
try this...

cmd1.commandtext = "insert into available(trainno,availability) VALUES(" & trno & "," & avail & ")"


I am assuming both the columns are numbers, otherwise you have to add quotes at appropriate places.

BTW, what is the error that you get? Try outputting the cmd1.commandtext without executing it.


Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-07-21 : 08:48:54
Please simply use parameters .....

(warning: code below may need tweaking for syntax/spelling)


trno=5
avail=10 ' I assume these are integers ?????

Dim cmd1 As New SqlCommand
cmd1.commandtext = "insert into available trainno,availability) VALUES (@TrainNo, @Availability)"
cmd1.parameters.add("@TrainNo", sqldbtypes.Integer).value = trno
cmd1.parameters.add("@Availability", sqldbtypes.Integer).value = avail
cmd1.executeNonQuery()


Much easier and clearer. Do not concatenate and build SQL strings with user input. Better yet, use parameters.

See:
http://weblogs.sqlteam.com/jeffs/archive/2006/07/21/10728.aspx

for more details.

- Jeff
Go to Top of Page

basic
Starting Member

2 Posts

Posted - 2006-07-21 : 11:20:08
Thank u so much guys..It did work..
Really I didnt expect such a quick reply...
Appreciate ur sincere effort..

Go to Top of Page
   

- Advertisement -