• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

In VB.Net - Trying to insert variables into MSSQL

Hi Experts, In vb.net I am connecting to a MS SQL 2005 Database and inserting the following-
objCommand = New SqlCommand("Insert Into Bookings(Guest, Bnumber, Cumber) Values(" & " '1', '2', '3')")  This works correctly.   But when I substitue my variables into the "Values", I get the error message "The name "xxxx" is not valid in this context.   I think I'm getting my " " and ' ' mixed up.   Please can someone show me the syntax for multiple variables in the SQL command in .net.  Thanks in advance
  • 2
1 Solution
Mind you should not wrap strings with ' '
Dim guest As String
Dim bnumber As Integer
Dim cumber As Integer
Dim insertBookingFormat As String = "INSERT INTO Bookings(Guest, Bnumber, Cumber) VALUES('{0}', {1}, {2})"
Dim objCommand As New SqlCommand([String].Format(insertBookingFormat, guest, bnumber, cumber))

Open in new window

Sorry, you should not wrap INTEGERS, not strings. See example above.
PNRTAuthor Commented:
Sorry, the 1 2 and 3 were just example variables, not integers.
But your solution worked first time
Many Thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now