Must declare the scalar variable “@ID”

GridView  – Auto Generated SQL Insert, Update And Delete Statements

ASP.NET 2.0 with SQL Server 2005

I recently ran into the error ‘Must declare the scalar variable “@ID” ‘ when attempting to delete rows from a GridView.  I had created the GridView via the smart tag and wizard options, so for a while I was stumped as to what the was the cause of the problem. I felt I knew what the error message was indicating, but could not figure out why it was occurring. A little Googling confirmed my suspicion that the parameter @ID was not being set in the SQL generated by the SQLDataSource but none of the posts I read revealed how I might fix it.

Because I had built the page using the various wizards I thought there was nothing further for me to do, but after a little further digging I discovered I had not set the DataKeyNames property for the GridView. Without this property set, the GridView cannot automatically wire up the parameter values and thus the generated SQL queries fail.

Double check the DataKeyNames property should you be experiencing this problem.

Depending how you have created the GridView and SQLDataSource, you may have to manually set the parameters in response to an SQLDataSource Inserting, Updating or Deleting event.


5 Responses to Must declare the scalar variable “@ID”

  1. bokey says:


    I”m glad I was able to find your post, it really helped me out alot.


  2. pmatlock says:

    This helped, Thank you for your work here!

  3. cool blog – keep up the writing sytle….

    This is a great post – best I’ve seen in quite some time….

  4. wordpress hosting…

    […]Must declare the scalar variable “@ID” « JFDI[…]…

  5. Thanks! this post really helped me.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: