Insérer une liste de contrôle d’éléments à valeurs multiples dans un accès à un seul champ

la programmation

[ad_1]

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

Ce que j’ai essayé :

Bonjour tout le monde
J’ai une CheckedListBox qui désigne les documents du fichier à fournir. Je souhaite que les cases cochées soient stockées dans un seul champ nommé Pieces_fourni dans ma table d’accès et les cases non cochées soient stockées dans un autre unique champ nommé Pieces_manquant J’ai essayé plusieurs fois sans succès , Quelqu’un peut-il m’aider, voici le code

Solution 1

Une colonne dans une base de données ne contient qu’une seule valeur : contenir 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

[ad_2]

コメント

Titre et URL copiés