Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
<pre> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            'CheckedListBox1.Show()
            DateTimePicker1.Value = Date.Now



            'Dim cmd As New OleDbCommand


            Dim cmd As OleDbCommand = New OleDbCommand("select * from dossier_ass where pieces_fourni =@pieces_fourni and pieces_manquant = @pieces_manquant", cn)
            Dim da As New OleDbDataAdapter(cmd)
            Dim dt As New DataTable()
            Dim pieces_fourni As String = ""
            Dim pieces_manquant As String = ""
            cmd.Parameters.AddWithValue("@pieces_fourni", pieces_fourni)
            cmd.Parameters.AddWithValue("@pieces_manquant", pieces_manquant)
            
            cn.Open()

            cmd = New OleDbCommand("Insert into tab_asso (id_dossier,denomination,date_recep,nom_prenom,adresse,wilaya,code_wilaya,region,pieces_fourni,pieces_manquant) VALUES(@id_dossier,@denomination,@date_recep,@nom_prenom,@adresse,@wilaya,@code_wilaya,@region,@pieces_fourni,@pieces_manquant) ", cn)
            cmd.Parameters.AddWithValue("@id_dossier", TextBox1.Text)
            cmd.Parameters.AddWithValue("@denomination", TextBox2.Text)
            cmd.Parameters.AddWithValue("@date_recep", DateTimePicker1.Text)
            cmd.Parameters.AddWithValue("@nom_prenom", TextBox6.Text)
            cmd.Parameters.AddWithValue("@adresse", TextBox7.Text)
            cmd.Parameters.AddWithValue("@wilaya", ComboBox1.Text)
            cmd.Parameters.AddWithValue("@code_wilaya", TextBox4.Text)
            cmd.Parameters.AddWithValue("@Region", TextBox5.Text)
            For Each dr As DataRow In dt.Rows
                Dim selectedIndex As Integer = CheckedListBox1.SelectedIndex
                If selectedIndex <> -1 Then
                    'Loop through items to get their check states.
                    For i As Integer = 0 To CheckedListBox1.Items.Count - 1
                        Dim checkstate As CheckState
                        checkstate = CheckedListBox1.GetItemCheckState(i)
                        If (checkstate = checkstate.Checked) Then
                            
                            cmd.Parameters.AddWithValue("@pieces_fourni", CheckedListBox1.Items(i)).ToString() 'true
                        Else
                            
                            cmd.Parameters.AddWithValue("@pieces_manquant", CheckedListBox1.Items(i)).ToString() 'false

                        End If
                    Next
                End If
            Next

            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Erreur lors de l'insertion de l'enregistrement dans la table..." & ex.Message, "Insértion d'Enregistrement")
        Finally
            cn.Close()
            refraiche()
        End Try
        MessageBox.Show("Données insérées avec succès")

    End Sub


What I have tried:

Hello everybody
I have a CheckedListBox which designates the file documents to be provided I want the checked boxes to be stored in a single field named pieces_fourni in my access table and the unchecked boxes to be stored in another single field named pieces_manquant I have tried several times without success, Can anybody help me here is the code
Posted
Updated 6-Mar-24 22:12pm
v2
Comments
CHill60 7-Mar-24 4:59am    
You can't keep adding parameters you need to build up two strings of values and then assign those strings to the parameters.
Having said that, it is poor database design
Kamel Brahouni 7-Mar-24 5:29am    
I am a beginner in vb

1 solution

A column in a DB holds only one value: to hold muyltip[le values you would have to do one of two things:
1) Convert your "Checked" items to a comma delimited string, and your "Unchecked" items to another. Then store the strings in a single column each.
The problem with this approach is that it's difficult to break the string up in a way that allows you to refer back to the actual items you are checking. You have to split the string, then convert them to numeric values to identify which control to check or uncheck.

2) Remember that you are using a relational database which is very good at combining data from multiple tables: create a second table which uses the originals row ID to refer back to the appropriate row, an index into an array of controls on your form, and a Checked/Unchecked status.
A simple loop then sets them as appropriate
 
Share this answer
 
Comments
Richard Deeming 7-Mar-24 10:39am    
Unless you're using the abomination that is multivalued fields[^], that is.

No idea how you pass those from C# though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900