Chèn hộp danh sách kiểm tra itmes đa giá trị vào quyền truy cập một trường

lập trình


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

Những gì tôi đã thử:

chào mọi người
Tôi có CheckedListBox chỉ định các tài liệu tệp sẽ được cung cấp. Tôi muốn các hộp đã chọn được lưu trữ trong một trường duy nhất có tên là Pieces_fourni trong bảng truy cập của tôi và các hộp không được chọn sẽ được lưu trữ trong một trường duy nhất khác có tên là Pieces_manquant. Tôi đã thử nhiều lần nhưng không thành công , Ai có thể giúp tôi đây là mã

Giải pháp 1

Một cột trong DB chỉ chứa một giá trị: chứa 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

コメント

タイトルとURLをコピーしました