【问题标题】:CheckBoxList inserting in SQL code在 SQL 代码中插入 CheckBoxList
【发布时间】:2014-02-17 01:17:10
【问题描述】:

你好,需要你的帮助来确定这段代码有什么问题, 这用于从复选框列表多项选择中填充一个 sql 数据库,但是 没有给出需要的结果。

我需要将每个选择保存在同一行的列中,但是当 每次它给出相同的以下结果时运行它

field_1  field_2  field_3  id

test2     test3   test4    1

即使是选定的值(测试 5、测试 6、测试 7)也不是(测试 2、测试 3、 测试 4) 来自 cbl。

非常感谢您的帮助 :)

Test.aspx.vb

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

' Define data objects
        Dim myConn As SqlConnection
        Dim cmd As SqlCommand
        Dim sqlstring As String

        ' Read the connection string 
        myConn = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\EmailEngineDB.mdf;Integrated Security=True")

        ' Create command
        sqlstring = " INSERT INTO TestTBL (field_1, field_2, field_3) VALUES (@field_1, @field_2, @field_3)"
        cmd = New SqlCommand(sqlstring, myConn)

        ' Add command parameters
        For i As Integer = 0 To CheckBoxList1.Items.Count - 1
            If CheckBoxList1.Items(i).Selected Then


                cmd.Parameters.Add("@field_" + i.ToString(), Data.SqlDbType.NVarChar)
                cmd.Parameters("@field_" + i.ToString()).Value = CheckBoxList1.Items(i).Text
            End If

        Next
        ' Enclose database code in Try-Catch-Finally
        Try
            ' Open the connection
            myConn.Open()
            ' Execute the command
            cmd.ExecuteNonQuery()
            ' Display success message
            Label1.Text = "Created Successfully :)"
            ' Reload page if the query executed successfully
            ' Response.Redirect("SamplesCollector.aspx")

        Catch ex As Exception
            ' Display error message
            Label1.Text = "Error ! Please try again "
        Finally
            ' Close the connection
            myConn.Close()
            'Response.Redirect("SamplesCollector.aspx")
        End Try
    End Sub

Test.aspx


    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test.aspx.vb" Inherits="Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True">
            <asp:ListItem>test 1</asp:ListItem>
            <asp:ListItem>test2</asp:ListItem>
            <asp:ListItem>test 3</asp:ListItem>
            <asp:ListItem>test 4</asp:ListItem>
            <asp:ListItem>test 5</asp:ListItem>
            <asp:ListItem>test 6</asp:ListItem>
            <asp:ListItem>test 7</asp:ListItem>
            <asp:ListItem>test 8</asp:ListItem>

        </asp:CheckBoxList>
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </form>
</body>
</html>

【问题讨论】:

    标签: vb.net checkboxlist


    【解决方案1】:

    您的数据库表只容纳三个字段值。

    查询:

    INSERT INTO TestTBL (field_1, field_2, field_3) VALUES (@field_1, @field_2, @field_3)
    

    仅包含三个参数(@field_1、@field_2 和 @field_3)。

    所以,在你的循环中:

    For i As Integer = 0 To CheckBoxList1.Items.Count - 1
            If CheckBoxList1.Items(i).Selected Then
    
    
                cmd.Parameters.Add("@field_" + i.ToString(), Data.SqlDbType.NVarChar)
                cmd.Parameters("@field_" + i.ToString()).Value = CheckBoxList1.Items(i).Text
            End If
    
        Next
    

    尽管这将(根据您的清单定义)迭代 8 个项目,只有“test2”、“test 3”和“test 4” " 将永远被传递到数据库,当它们被选中时。

    这是因为它们在清单中的位置与您的参数名称相匹配(即“test2”是第 1 项(从零开始的列表)并将匹配到@field_1)。

    为了存储所有选定的选项,您确实应该更改数据库表结构以允许任意数量的项目。在最坏的情况下,您需要在表中添加额外的列来说明其余可用的清单项目(最多为 field_8,并且您还需要考虑到列表中的第一个项目的索引为0 而不是 1。

    例如,在 Question 和 SelectedOptions 表之间创建一对多关系。

    问题:

    QuestionId | UserId
    1          | 1
    

    SelectedOptions:

    SelectedOptionId | QuestionId | SelectedValue
    1                | 1          | test2
    2                | 1          | test 3
    

    然后,您的插入循环将为每个检查的值插入一条记录到SelectedOptions。然后,您可以支持任意数量的选定选项。

    【讨论】:

    • 感谢您的努力,您能否建议我执行此操作的最佳实践.. 将选定的 CheckBoxList 值插入 SQL 数据库?知道当尝试在数据库中添加其他列以适应清单项目(最多一个 field_8)它不再起作用了
    • gr8 逻辑非常感谢您 :) 感谢您的帮助
    猜你喜欢
    • 2017-01-29
    • 2013-02-13
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多