Skip to content
Advertisement

Data selected from SQL database from top item in checkboxlist is copied to all Gridview rows

I am trying to create a grid view table where i want to show some selected data from a checkboxlist. THe problem is that whenever i select some data from the checkboxlist, the gridview will only show the data from the top checked box and not multiple different data tied to each box in the list even though i have checked multiple boxes to show data from?

Det gridview does add a new line when a new box is checked, however the new row copies the data from the previous row

Both the gridview and the checkboxlist are connected to the SQLdatabase, where the data comes from:

CODE

I expected that the gridview add a new row when a new box in checkboxlist is checked,and keep the previous row intact. The row should contain the information tied to the item in the checkboxlist, which is provided by the SQLdatabase.

However, the gridview does add a new row, when a new box is checked, but it copies the data from the already displayed row.

Advertisement

Answer

The problem in your code is that you use the CheckboxList1.SelectedValue. What should be the SelectedValue of a CheckBoxList? Do you suppose that whatever this value is, it will change while you loop over the Items collection?

You need to use Item.Value instead

Other points to note: Disposable objects like connection and reader should be disposed when you don’t need them anymore. The using statement ensure this even in case of excepions. Another point very important is the use of parameters when you want to pass a query text to your database engine. Concatenating string together is a sure way to find yourself in troubles with Sql Injection and possible parsing errors.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement