Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use SqlCommand Bound Parameters

Status
Not open for further replies.

johnaregan

Programmer
Mar 13, 2001
87
0
0
Hi
I am trying to insert into Northwind using bound paramters, the command returns the SQL Server error:
Line 1: Incorrect syntax near '?'.
Can anybody see what it is that I am doing wrong? I have seen similar code on the web.


Code:
...
comm.CommandType = CommandType.Text;
comm.CommandText = "INSERT INTO Employees (FirstName, LastName) VALUES (?, ?)";
comm.Parameters.Add("FirstName", SqlDbType.NVarChar, 20);
comm.Parameters["FirstName"].Value = "Bob";
comm.Parameters.Add("LastName", SqlDbType.NVarChar, 20);
comm.Parameters["@LastName"].Value = "Bobs";
...
 
I think for SQL server you would have to create your SQL statement parameters like:
Code:
comm.CommandText = "INSERT INTO Employees (FirstName, LastName) VALUES (@FirstName,@LastName)";
Then, add them like:
Code:
comm.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar, 11));
comm.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar, 40));
cmd.Parameters["@FirstName"].Value = txtFirstName.Text;
cmd.Parameters["@LastName"].Value = txtLastName.Text;


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top