articles:

must declare the scalar variable

There are two commmon reasons for this error:

  1. The parameter is simply misspelled
  2. When using OleDb the parameters in the SQL are to be set to ? and not @paramname

For more details on these see http://blogs.msdn.com/b/spike/archive/2010/03/03/must-declare-the-scalar-variable.aspx

But there is a third reason i've yet to see documented:

When adding parameters to an sqldatasource programmatically you need to omit the @ sign at the beginning of the paramater name.

E.g. mySqldatasource.insertparameters.add("@myparam",value)
mySqldatasource.insertcommand = "INSERT INTO myTable (myparamfield) values (@myparam)"

will return this error when you run the insert command:

System.Data.SqlClient.SqlException: Must declare the scalar variable @myparam

The code needs to be:

mySqldatasource.insertparameters.add("myparam",value)
mySqldatasource.insertcommand = "INSERT INTO myTable (myparamfield) values (@myparam)"

Note how the param in the parameters collection doesn't have an @ sign, but the reference to it in the command does.

Post a comment

name:
(optional)

email:
(optional)

comment: