checkboxlist选中的项目不会被选中&在字符串中插入空白值

时间:2016-03-31 04:19:00

标签: mysql asp.net vb.net

我有一个数据插入的表单。有一些复选框列表。 CheckboxList与db中的表绑定。现在选择项目后,它应该在db表中输入选定的值,但它会插入空字符串。

我有两个checkboxlist(产品,付款)付款正常。问题在于产品。

VB代码

Private Sub list_business_hospital_Load(sender As Object, e As EventArgs) Handles Me.Load
    Me.PopulateProducts()
    Me.PopulatePayments()
End Sub

Private Sub PopulateProducts()
    productsList.Items.Clear()
    Using conn As New MySqlConnection()
        conn.ConnectionString = ConfigurationManager _
            .ConnectionStrings("conio").ConnectionString()
        Using cmd As New MySqlCommand()
            cmd.CommandText = "select * from chemistsProducts"
            cmd.Connection = conn
            conn.Open()
            Using sdr As MySqlDataReader = cmd.ExecuteReader()
                While sdr.Read()
                    Dim item As New ListItem()
                    item.Text = sdr("productName").ToString()
                    item.Value = sdr("productName").ToString()
                    'item.Selected = Convert.ToBoolean(sdr("IsSelected"))
                    productsList.Items.Add(item)
                End While
            End Using
            conn.Close()
        End Using
    End Using
    productsList.Items.Insert(0, New ListItem("All", "All"))
End Sub

Private Sub PopulatePayments()
    Using conn As New MySqlConnection()
        conn.ConnectionString = ConfigurationManager _
            .ConnectionStrings("conio").ConnectionString()
        Using cmd As New MySqlCommand()
            cmd.CommandText = "select * from payment"
            cmd.Connection = conn
            conn.Open()
            Using sdr As MySqlDataReader = cmd.ExecuteReader()
                While sdr.Read()
                    Dim item As New ListItem()
                    item.Text = sdr("paymentName").ToString()
                    item.Value = sdr("paymentID").ToString()
                    'item.Selected = Convert.ToBoolean(sdr("IsSelected"))
                    ListPayment.Items.Add(item)
                End While
            End Using
            conn.Close()
        End Using
    End Using
    ListPayment.Items.Insert(0, New ListItem("All", "All"))
End Sub

Private Sub save_Click(sender As Object, e As EventArgs) Handles save.Click
        Dim selectedProducts As String = String.Empty
        For Each chk As ListItem In productsList.Items
            If chk.Selected = True Then
                selectedProducts &= "<li>" + chk.Text + "</li>"
            End If
        Next


        Dim payments As String = String.Empty
        For Each chk As ListItem In ListPayment.Items
            If chk.Selected = True Then
                payments &= "<li>" + chk.Text + "</li>"
            End If
        Next

        Try
            Dim str1 As String = "INSERT INTO chemists (`products`, `payment`) values ('" + selectedProducts + "', '" + payments + "')"

            Dim str2 As MySqlDataReader
            Dim adapter As New MySqlDataAdapter
            Dim command As New MySqlCommand
            command.CommandText = str1
            command.Connection = con
            adapter.SelectCommand = command
            con.Open()
            str2 = command.ExecuteReader
            con.Close()
            Response.Redirect("business-added.aspx")
        Catch ex As Exception
            Response.Write(ex)
        End Try
End Sub

此处付款已插入我选择的内容。问题出在selectedProducts

1 个答案:

答案 0 :(得分:0)

在页面加载中尝试以下,

If Not IsPostBack Then
    Me.PopulateProducts()
    Me.PopulatePayments()
End If