Click here to Skip to main content
12,405,270 members (61,386 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
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:
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 22-Nov-12 2:13am
Updated 22-Nov-12 2:18am
v3

1 solution

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

I hope this will help You
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 2 Dec 2012
Copyright © CodeProject, 1999-2016
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