简体   繁体   English

如何将ListBox中的多个值插入SQL数据库

[英]How do I insert multiple values from a ListBox into SQL Database

I developed a form that consists of asp.net textboxes and one ListBox control set to “Multiple” that has four companies. 我开发了一个由asp.net文本框和一个ListBox控件组成的表单,该控件设置为“Multiple”,有四个公司。 When the submit button is clicked, the form inserts the inputted data into two different tables in a database, and sends an email to the respective company(ies) – all works well … Except when I attempt to select more than one company – my code will only submit the first company selected, but inserts the first company selected data based on the number of companies selected in the ListBox. 单击提交按钮时,表单将输入的数据插入到数据库中的两个不同的表中,并向相应的公司发送电子邮件 - 一切正常...除非我尝试选择多个公司 - 我的代码将仅提交所选的第一家公司,但会根据ListBox中选择的公司数量插入第一家公司选定的数据。 For example, if I select two companies, the first company selected is entered twice. 例如,如果我选择两家公司,则选择第一家公司两次。 4 times, if I select all four companies – Here is my code/logic. 4次,如果我选择所有四家公司 - 这是我的代码/逻辑。 Could anyone provide some assistance as to what I am doing wrong? 任何人都可以提供一些帮助,说明我做错了什么? I provided my code below: 我在下面提供了我的代码:

/ * ** * ** * ** * ** * ** * **** * aspx: * / * ** * ** * ** * ** * ** * **** * aspx: *

<p><b>Company Affected:</b><br />
<asp:ListBox 
   ID="lstcompanyAffected" 
   runat="server"
   SelectionMode="Multiple">
   <asp:ListItem Text="Select Company" Value="SelectCompany" />
   <asp:ListItem Text="CompanyI" Value="CompanyI" />
   <asp:ListItem Text="CompanyII" Value="CompanyII" />
   <asp:ListItem Text="CompanyIII" Value="CompanyIII" />
   <asp:ListItem Text="CompanyIV" Value="CompanyIV" />
   </asp:ListBox></p>

/ * ** * ** * ** * aspx.cs * / * ** * ** * ** * aspx.cs *

lstcompanyAffected.SelectionMode = ListSelectionMode.Multiple;
        foreach (ListItem item in locationAffected.Items)
        {
            if (item.Selected) { 
                if ((lstcompanyAffected.SelectedValue.ToString() == "CompanyI"))
                {
                    outageId.Text = "1";
                    txtEmailAddresses.Text = "CompanyI@aol.com";
                }
                else if ((lstcompanyAffected.SelectedValue.ToString() == "CompanyII"))
                {
                    outageId.Text = "2";
                    txtEmailAddresses.Text = "CompanyII@aol.com";
                }
                else if ((lstcompanyAffected.SelectedValue.ToString() == "CompanyIII"))
                {
                    outageId.Text = "3";
                    txtEmailAddresses.Text = "CompanyIII@aol.com";
                }
                else if ((lstcompanyAffected.SelectedValue.ToString() == "CompanyIV"))
                {
                    outageId.Text = "4";
                    txtEmailAddresses.Text = "CompanyIV@aol.com";
                }
         //call insertOutage Function
                InsertOuage();
                //call InsertOutageDetail Function
                InsertOutageDetail();
                //call sendEmail Function
                sendEmail();
            }

        } // end foreach

       panelSendEmail.Visible = false;
       panelMailSent.Visible = true;
    }

Your internal conditional statements are based off of "lstcompanyAffected.SelectedValue", which is always the same item each time the for loop repeats. 您的内部条件语句基于“lstcompanyAffected.SelectedValue”,每次for循环重复时,它始终是相同的项。 Change the if statements to look at your "item" loop variable instead. 更改if语句以查看“item”循环变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM