I need to update some table values using Checkboxes. The table consist of 3 simple columns: id, name and selected (bool).
I added a asp:CheckBoxList control to a form, bound it to a SqlDataSource.
I added a button with the simple code MySqlDataSource.Update();
Then browse to the page.
I click a Checkbox, then click the button to Update the datasource and using the Sql Server Profiler I see this is what was sent to the database:
exec sp_executesql N'UPDATE [MyTable] SET [Name] = @Name, [Selected] = @Selected
WHERE [Id] = @Id',N'@Name nvarchar(4000),@Selected bit,
@Id int',@Name=NULL,@Selected=NULL,@Id=NULL
The problem is clear, the values are not being set…. but why?
I added a GridView, just for testing purposes, using the same exact SqlDataSource without moving a single bit from it, and I'm able to modify the records by clicking the "Edit" link, then the checkbox, then the "Update" link and that's it, that does save to the database, it correctly send the update command to Sql Server.
If I go to the UpdateQuery property of MyDataSource, I see there's an option to set the "Parameter Source", but I can't see in the dropdownlist one to set the parameter source of the same datasource. Seems like I'm missing something here…. but why the same SqlDataSource does work for the GridView ?
I spent several hours already searching for samples about using a CheckboxList to perform updates and I found a many sample but… most are just for displaying purpose.
I wonder if the CheckboxList can really work for updates?
UPDATE
Followed the advise to set the Parameter source as the CheckBoxList.
I set the parameter source for the Name and Selected columns to the property "SelectedValue".
It changed the behavior, but it still works wrong, now the query sent to the database is:
exec sp_executesql N'UPDATE [MyTable] SET [Name] = @Name, [Selected] = @Selected WHERE [Id] = @Id',N'@Name nvarchar(5),@Selected bit,@Id int',@Name=N'False',@Selected=0,@Id=NULL
You can see now it sends "False" and "0" instead of nulls, but the Id is still being sent as "null".
So, I changed the pararmeters to set the ID with a Parameter source of type Control, then CheckboxList, then set Selected to the SelectedValue property.
Now the error message is:
"Input string was not in a correct format."
I think it should be a way to get the underlying values from the "current" checkbox item, but I don't know why and I have still searching for examples.
Best Answer
I have made a repeater construct that appears like a
asp:CheckBoxList
, but with additional functionality. Here is what it looks like in a browser:Here is the .aspx code:
And the codebehind:
And here is the debugger open at the update statement. All values are accurate!
Obviously there is much room for improvement, but hopefully this will give you a good idea of how to proceed!