Click here to Skip to main content
11,922,905 members (48,302 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET
I'm doing project in & I want to select multiple options from Listbox and store the data in ms-access seprated by comma. Can anyone help me with this pls ?
I have done it for single item selection.
Here is the code I used:
Protected Sub ListBoxstarters_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBoxstarters.SelectedIndexChanged
        Dim x As String
        x = ListBoxstarters.SelectedItem.Text
    End Sub
    Protected Sub ListBoxmaincourse_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBoxmaincourse.SelectedIndexChanged
        Dim y As String
        y = ListBoxmaincourse.SelectedItem.Text()
    End Sub
    Protected Sub ListBoxfastfood_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBoxfastfood.SelectedIndexChanged
        Dim z As String
        z = ListBoxfastfood.SelectedItem.Text()
    End Sub
    Protected Sub ListBoxdessert_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBoxdessert.SelectedIndexChanged
        Dim t As String
        t = ListBoxdessert.SelectedItem.Text
    End Sub
Protected Sub submitbtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submitbtn.Click
            cm = New OleDbCommand("insert into Orderingform values(@ono,@oname,@ocity,@oadd,@oemail,@ostarters,@omaincourse,@ofastfood,@odessert,@oord,@oprice,@oquantity,@acc,@oank)", cn)
            With cm.Parameters
                .Add("@ono", OleDbType.Integer).Value = txtcid.Text
                .Add("@oname", OleDbType.VarChar).Value = txtname.Text
                .Add("@ocity", OleDbType.VarChar).Value = txtcity.Text
                .Add("@oadd", OleDbType.VarChar).Value = txtaddress.Text
                .Add("@oemail", OleDbType.VarChar).Value = txtemail_id.Text
                'cm.Parameters.Add("@otype", OleDbType.VarChar).Value = ListBoxfood.SelectedItem.Text
                .Add("@ostarters", OleDbType.VarChar).Value = ListBoxstarters.SelectedItem.Text
                .Add("@omaincourse", OleDbType.VarChar).Value = ListBoxmaincourse.SelectedItem.Text
                .Add("@ofastfood", OleDbType.VarChar).Value = ListBoxfastfood.SelectedItem.Text
                .Add("@odessert", OleDbType.VarChar).Value = ListBoxdessert.SelectedItem.Text
.Add("@oarr", OleDbType.Date).Value = txtorderdt.Text
                .Add("@oprice", OleDbType.Integer).Value = txtprice.Text
                .Add("@oquantity", OleDbType.Integer).Value = txtquantity.Text
                .Add("@acc", OleDbType.Integer).Value = txtacct.Text
                .Add("@oank", OleDbType.VarChar).Value = txtbankname.Text
            End With
            txtcid.Enabled = True
            Session("Ordering_id") = txtcid.Text
            Session("Name") = txtname.Text
            Session("Email") = txtemail_id.Text
            Session("Order Date") = txtorderdt.Text
            Session("Amount Payable") = txtprice.Text
            Session("Account No") = txtacct.Text
            Session("Bank Name and Address") = txtbankname.Text
            MsgBox("Your Ordering ID no is :" & Session("Ordering_id").ToString())
        Catch ex As Exception
            ' MsgBox(ex.Message)
        End Try
        txtcid.Text = ""
        txtname.Text = ""
        txtcity.Text = ""
        txtaddress.Text = ""
        txtemail_id.Text = ""
        'ListBoxfood.SelectedItem.Text = ""
        ListBoxstarters.SelectedItem.Text = ""
        ListBoxmaincourse.SelectedItem.Text = ""
        ListBoxfastfood.SelectedItem.Text = ""
        ListBoxdessert.SelectedItem.Text = ""
        txtorderdt.Text = ""
        txtprice.Text = ""
        txtbankname.Text = ""
    End Sub
Posted 22-Nov-12 3:13am
Edited 22-Nov-12 3:18am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

User SelectionMode = Multiple properties and see following Code

protected void Button1_Click(object sender, EventArgs e)
           List<string> List = new List<string>();
           string str = "";
           int[] indices = ListBox1.GetSelectedIndices();
           foreach (int i in indices)
           str = String.Join(",", List.ToArray());

I hope this will help You
Nelek 2-Dec-12 5:12am
OP's comment moved from non-solution below
Thank You very much it worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151125.1 | Last Updated 2 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100