cmd.CommandText = "DELETE FROM [Table] WHERE [ID] =1"
This does not work:
Dim IdNo As Single = 1 cmd.CommandText = "DELETE FROM [Table] WHERE [ID] = IdNo"
I can get the real value of IdNo later, but this gives a missing parameters errors. I have tried WHERE [ID] = 'IdNo'" with no luck. I would think that this is basic stuff but I am stuck. Any ideas?
Thanks,
Carthalion
Marked as answer by Carthalion on Jul 31, 2012 12:22 AM
cmd.CommandText = "DELETE FROM [Table] WHERE [ID] = " + IdNo;
Here, the real value of the variable "IdNo" will be assigned to the sql statement.
Hope this helps.
Please remember to click “Mark as Answer” on the post that helps you and to unmark it if a marked post does not actually answer your question.
Thank you!
----------------------
"Microsoft Community Contributor Award 2011"
Marked as answer by Carthalion on Jul 31, 2012 12:22 AM
Carthalion
Member
430 Points
196 Posts
Using a variable in a query filter
Jul 16, 2012 12:07 AM|LINK
This works:
cmd.CommandText = "DELETE FROM [Table] WHERE [ID] =1"
This does not work:
Dim IdNo As Single = 1
cmd.CommandText = "DELETE FROM [Table] WHERE [ID] = IdNo"
I can get the real value of IdNo later, but this gives a missing parameters errors. I have tried WHERE [ID] = 'IdNo'" with no luck. I would think that this is basic stuff but I am stuck. Any ideas?
Thanks,
Carthalion
santa_1975
Star
8574 Points
1499 Posts
Re: Using a variable in a query filter
Jul 16, 2012 01:33 AM|LINK
Hi,
Try this.
Here, the real value of the variable "IdNo" will be assigned to the sql statement.
Hope this helps.
Thank you!
----------------------
"Microsoft Community Contributor Award 2011"
imapsp
Member
134 Points
22 Posts
Re: Using a variable in a query filter
Jul 16, 2012 01:33 AM|LINK
I believe it is best to use parameters:
cmd.CommandText = "DELETE FROM [Table] WHERE [ID] = @ID" cmd.Parameters.Add("@ID", SqlDbType.Float) cmd.Parameters("@ID").Value = IdNoHope this helps.
hafizzeeshan
Participant
834 Points
253 Posts
Re: Using a variable in a query filter
Jul 16, 2012 04:24 AM|LINK
hi
There is syntex error in ur query. Vb.net Syntex is like this
cmd.CommandText = "DELETE FROM [Table] WHERE [ID] = " & IdNo
Regards
Zeeshan Rouf
Zeeshan Rauf
# 92-3216698206
Email : zeeshan_rouf@hotmail.com
Please mark the replies as answers if they help or unmark if not.
Carthalion
Member
430 Points
196 Posts
Re: Using a variable in a query filter
Jul 16, 2012 03:13 PM|LINK
That does make the difference. Thank you for the information!