Monday, July 10, 2006

A problem I had recently was that a GridView bound to a SqlDataSource was not deleting the records when the Delete command was executed. I've tagged this entry as a "gotcha" as it isn't completely obvious where the problem might be to the uninitiated.

The Delete Sql command was executing fine, but after using SqlProfiler I noticed that the “ID” parameter was set to “Null”, hence the fact that no record was deleted. This was not what I expected as I had used the normal SqlDataSource and DataGrid wizard to set up the controls. What I was missing was setting the “DataKeyNames” property on the GridView to the name of the primary key of my data … “ID”. Now, the SqlDataSource passed through the correct Sql statement and all was good!

I’ve avoided the SqlDataSource/Designer approach whenever possible for the very reason that it’s difficult to see what’s going on when something doesn’t work. I was creating a prototype so time pressures won out and I used it!