How To Set Default Focus To a Server Control

March 24, 2007

ASP.NET 2.0

You can easily set the focus to a specific control when your web form is loaded, by adding the line

Page.SetFocus(<ControlID>);

to your Page_Load() event.

Advertisements

Must declare the scalar variable “@ID”

March 23, 2007

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.