user1724708
user1724708

Reputation: 1469

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. 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. 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. Could anyone provide some assistance as to what I am doing wrong? I provided my code below:

/******************** *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*

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

        } // end foreach

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

Upvotes: 0

Views: 1758

Answers (1)

Evan Phillips
Evan Phillips

Reputation: 261

Your internal conditional statements are based off of "lstcompanyAffected.SelectedValue", which is always the same item each time the for loop repeats. Change the if statements to look at your "item" loop variable instead.

Upvotes: 2

Related Questions