0

I am using Listbox in asp.net to select multiple option but it is not working for multiple selection for "program". Only first value among that is selected is being inserted into database. Code behind is .aspx.cs. Below is part of my code for multiple option selection and insertion. I am using multiple selection for "program":

Solution: Updated code behind and it worked.

.aspx page:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:TestConnectionString %>" 
     DeleteCommand="DELETE FROM [Database_table] WHERE [ID] = @ID"    
     InsertCommand="INSERT INTO [Database_table] ([FirstName], [LastName], [address],[program] VALUES (@FirstName, @LastName, @address, @program)" 
     SelectCommand="SELECT * FROM [Database_table]">
         <DeleteParameters>
             <asp:Parameter Name="ID" Type="Int32" />
         </DeleteParameters>
         <InsertParameters>
             <asp:Parameter Name="FirstName" Type="String" />
             <asp:Parameter Name="LastName" Type="String" />
             <asp:Parameter Name="address" Type="String" />
             <asp:Parameter Name="program" Type="String" />
         </InsertParameters>
</asp:SqlDataSource>

<asp:ListBox id="program" rows="10"  SelectionMode="Multiple" runat="server">
    <asp:ListItem selected="true"></asp:ListItem>
    <asp:ListItem>Option A</asp:ListItem>
    <asp:ListItem>Option B</asp:ListItem>
    <asp:ListItem>Option C</asp:ListItem>
    <asp:ListItem>Option D</asp:ListItem>
    <asp:ListItem>Option E</asp:ListItem>
    <asp:ListItem>Option F</asp:ListItem>
    <asp:ListItem>Option G</asp:ListItem>
    <asp:ListItem>Option H</asp:ListItem>
    <asp:ListItem>Option I</asp:ListItem>
</asp:ListBox>

Updated Code Behind .aspx.cs:

if (error_type == "")
{
    SqlDataSource1.InsertParameters["FirstName"].DefaultValue = FirstName.Text;
    SqlDataSource1.InsertParameters["LastName"].DefaultValue = LastName.Text;
    SqlDataSource1.InsertParameters["address"].DefaultValue = address.Text;

    string items = string.Empty;
    string items_updated = string.Empty;

    foreach (ListItem item in program.Items)
    {
        if (item.Selected)
        {
            items += item.Text + ",";
        }
    }

    if (!string.IsNullOrEmpty(items))
    {
        items_updated = items.Remove(items.LastIndexOf(","));   
    }

    SqlDataSource1.InsertParameters["program"].DefaultValue = items_updated;
    SqlDataSource1.InsertParameters["program"].DefaultValue = program.SelectedItem.Text;
    SqlDataSource1.Insert();

    Response.Redirect("http://www.example.org/Success.aspx", false);
}
else
{
    Response.Redirect("http://www.example.org/Failure.aspx", false);
}

For program I also tried like below which didn't work for selecting multiple options:

SqlDataSource1.InsertParameters["program"].DefaultValue = program.Text;

Can you please suggest if I am missing something here?

2 Answers 2

3

I am not really sure of what you want to achieve here, I think you did not provide enough information for your objective, although, at a first sight, it seems your are missing a loop on your code to iterate between selected items.

If you are trying to insert one row per option in your database, you should use something like:

foreach (ListItem item in program.Items)
{
    if(item.Selected)
    {

         SqlDataSource1.InsertParameters["FirstName"].DefaultValue = FirstName.Text;
         SqlDataSource1.InsertParameters["LastName"].DefaultValue = LastName.Text;
         SqlDataSource1.InsertParameters["address"].DefaultValue = address.Text;
         SqlDataSource1.InsertParameters["program"].DefaultValue = item.Text;

         SqlDataSource1.Insert();

    }
}

Otherwise, if you want to put all options in a single database row, I recommend you to change your database structure and add a "OptionTable". If this is not an option for you, and you want all options in one row, you could use something like:

string items = string.Empty;
foreach (ListItem item in program.Items)
{
    if(item.Selected)
    {
        items &= item.Text;
    }
}
SqlDataSource1.InsertParameters["FirstName"].DefaultValue = FirstName.Text;
SqlDataSource1.InsertParameters["LastName"].DefaultValue = LastName.Text;
SqlDataSource1.InsertParameters["address"].DefaultValue = address.Text;
SqlDataSource1.InsertParameters["program"].DefaultValue = items;
SqlDataSource1.Insert();
Sign up to request clarification or add additional context in comments.

5 Comments

Hello Abe, I am trying to achieve multiple selection just for "program" not for FirstName, LastName, address. Listbox is used just for "program" as listed in my above code.
Hello TechPro. That part I got, and the first part of the code should do it for you, although, if you have multiple options linked to the same user, consider creating a table for options and use primary keys and relationships to link the options to the person. Please read this wiki to better understand what I am saying.
Hello Abe, Your second solution suggestion helped me to get multiple selection to be inserted in database now. One more question, I have to separate each option entry by comma , I added in code like this: if (item.Selected) { items += item.Text + ","; } Please see the above updated codebehind .aspx.cs code. This is putting comma after last selected item also, Example: option A, option B, Any idea for this one, how to not get comma after last option selected?
Hello Abe, For comma seperated issue, I added in code like this and it worked: if (!string.IsNullOrEmpty(items)) { items_updated = items.Remove(items.LastIndexOf(",")); } SqlDataSource1.InsertParameters["program"].DefaultValue = items_updated;
Have you thought about using the String.Join() method? Check it at msdn.
2

You use program.SelectedItem.Text which gets the First selected item's text. Please review: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listbox.selectionmode%28v=vs.110%29.aspx

Essentially, you need to do the following:

foreach (ListItem item in program.Items)
{
    if(item.Selected)
    {
        // put your query here
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.