lundi 25 décembre 2017

How to check or uncheck a checkboxlist item according to database values

In my application, the user can choose different options from a data table, according to the company's interest. So a checkboxlist is populated from a stored procedure called SEL_Dewey_Subcat_Raw, that prints the ID value and the name to the checkbox list. This is ok, and it's working fine.

But now I need the user to be able to edit what they choose before. So I need that the checkboxlist is populated according to what they've chosen before, If the category was chosen before that particular item must be selected.

The code that populates the CBL:

SELECT * FROM [dbo].[Dewey_Subcat] ORDER BY[dbo].[Dewey_Subcat].[Nombre] ASC

This info is stored in a data table called [dbo].[Proveedores_Dewey], and it has this columns:

  1. ID (PK int)
  2. Id_Proveedor (int), the ID of the client
  3. Id_Dewey_Subcat (int), the ID of the category

So if one checkbox item is selected, for example, the data stored will be:

ID: 1 Id_Proveedor: 24 (client's ID) Id_Dewey_Subcat: 38 (category's)

This is the code I have so far for the edit panel checkboxlist, but it's not working!

For Each li As ListItem In Dewey_RBL.Items
        Dim SqlConDeweyCBL As SqlConnection
        Dim SqlComDeweyCBL As SqlCommand
        Dim SqlDRDeweyCBL As SqlDataReader
        SqlConDeweyCBL = New SqlConnection(ConfigurationManager.ConnectionStrings("EnchufeBBMCA").ToString())
        SqlComDeweyCBL = New SqlCommand("SELECT [dbo].[Proveedores_Dewey].[Id_Dewey_Subcat] FROM [dbo].[Proveedores_Dewey] WHERE([dbo].[Proveedores_Dewey].[IdProveedor] = '" & empresaId & "')", SqlConDeweyCBL)
        If SqlConDeweyCBL.State = ConnectionState.Closed Then
            SqlConDeweyCBL.Open()
        End If
        SqlDRDeweyCBL = SqlComDeweyCBL.ExecuteReader()
        While SqlDRDeweyCBL.Read()

            If SqlDRDeweyCBL("Id_Dewey_Subcat") = li.Value Then

                li.Selected = True
            Else

                li.Selected = False
            End If

        End While
        SqlDRDeweyCBL.Close()
        SqlConDeweyCBL.Close()
    Next

So how can I check the items of the checkbox list that are stored in the [dbo].[Proveedores_Dewey] and leave the others unchecked?

Thanks!




Aucun commentaire:

Enregistrer un commentaire