Click here to Skip to main content
15,917,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I'm doing project in asp.net. & 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:
VB
Protected Sub ListBoxstarters_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBoxstarters.SelectedIndexChanged

        Dim x As String
        x = ListBoxstarters.SelectedItem.Text
        'MsgBox(x)
    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()
        'MsgBox(y)
    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()
        'MsgBox(z)
    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
        'MsgBox(t)
    End Sub
Protected Sub submitbtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submitbtn.Click
        Try
            cm = New OleDbCommand("insert into Orderingform values(@ono,@oname,@ocity,@oadd,@oemail,@ostarters,@omaincourse,@ofastfood,@odessert,@oord,@oprice,@oquantity,@acc,@oank)", cn)
            MsgBox("cm")

            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
            cm.ExecuteNonQuery()
            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())
            Response.Redirect("spatr.aspx")
        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
Updated 22-Nov-12 2:18am
v3

1 solution

User SelectionMode = Multiple properties and see following Code

C#
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)
           {
               List.Add(ListBox1.Items[i].ToString());
           }
           str = String.Join(",", List.ToArray());
       }


I hope this will help You
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900