Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using listbox for the first time in my asp.net C# website. I want the user to be able to select multiple items from the listbox and later on the button click event the selected items will be displayed in the label.
I have searched alot on google.. came across many solutions bt non of them worked for me.
I dnt know why m i getting this weird error out of nowhere.

my aspx code:

HTML
<asp:ListBox ID="ListBox1" SelectionMode="Multiple" AutoPostBack="true" DataTextField="area" 
            DataValueField="area" runat="server" Height="139px" Width="239px" 
            onselectedindexchanged="ListBox1_SelectedIndexChanged">
        

        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
       
        You Choosed<br />
        <asp:Label ID="Label1" runat="server">

my code behind aspx.cs code:
C#
protected void Page_Load(object sender, EventArgs e)
   {
       bindlist();
       if (!IsPostBack)
       {
           bindlist();
       }
   }
   protected void bindlist()
   {
       SqlConnection conn = new SqlConnection();
       conn.ConnectionString = "Data Source=SARU-PC\\SQLEXPRESS;Initial Catalog=jd;Integrated Security=True";
       SqlDataAdapter adp = new SqlDataAdapter("select area from area", conn);
       DataSet ds = new DataSet();
       adp.Fill(ds);
       ListBox1.DataSource = ds;
       ListBox1.DataBind();
   }
   protected void Button1_Click(object sender, EventArgs e)
   {
       ListItem item = ListBox1.SelectedItem;
       string a = item.Text;
       Label1.Text = a;
       //for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
       //{
       //    if (ListBox1.Items[i].Selected == true)
       //    {
       //        Label1.Text += ListBox1.Items[i].ToString();
       //    }
       //}
       //ListItem item = ListBox1.SelectedItem;
       //string a = item.ToString();
       //Label1.Text = a;
       //Label1.Text = "";
       //foreach (ListItem li in ListBox1.Items)
       //{
       //    if (li.Selected)
       //    {
       //        Label1.Text += li.ToString() + "<br />";
       //    }
       //}
   }

   protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
   {
       Label1.Text = "";
       string a = ListBox1.SelectedItem.ToString();
       Label1.Text += a + "<br />";
   }


1) I loaded my listbox from database in page load event later i replaced with existing code just in case.
2) I tried getting selected items on button click in label bt later i even tried getting it on selectedindexchanged event. it still doesnt work.
3) have tried diff ways of assigning selected items of listbox in label bt all in vain.

I am getting the"Object reference not set to an instance of an object" error on the line where i assign "listbox1.selecteditem" to a string or to the label itself.

Please help!!!!
Posted
Updated 5-Aug-14 22:45pm
v3

1 solution

Try this
C#
<asp:listbox id="ListBox1" selectionmode="Multiple" autopostback="true" datatextfield="area" >
DataValueField="area" runat="server" Height="139px" Width="239px"
>
</asp:listbox>



C#
protected void Page_Load(object sender, EventArgs e)
{
//dont call bindlist here
if (!IsPostBack)
{
bindlist();
}
}


protected void Button1_Click(object sender, EventArgs e)
{

foreach (ListItem li in ListBox1.Items)
{
 if (li.Selected)
 {
 Label1.Text += li.ToString() + ";

 }
}
}
 
Share this answer
 
v2
Comments
Ashi0891 6-Aug-14 5:01am    
that's it? It worked.
oh i wish i had used a lil bit more time to make it work on my own.
Thank you so much!! :)
pradiprenushe 6-Aug-14 5:05am    
Welcome

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

  Print Answers RSS


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