Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8067827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:27:34+00:00 2026-06-05T12:27:34+00:00

I am creating a web application in asp.net 4.0 I have a web-form in

  • 0

I am creating a web application in asp.net 4.0

I have a web-form in which there is a ListBox control which adds a string list on Page_Load event. If I select any list item from ListBox and want to compute it by using Listbox.SelectedValue it throws Object reference not set to an instance of an object exception on Listbox.SelectedValue.

By using “QuickWatch” (in Visual studio 2010) i did some findings that i can get value by giving index (say Listbox.Items[2]) but i got null or -1 if i use Listbox.SelectedValue or Listbox.SelectedIndex

My problem is that why ListBox control is showing null exception error upon selection of item as this Listbox is not empty?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:27:36+00:00Added an answer on June 5, 2026 at 12:27 pm

    According to my understanding of the situation you are checking Listbox.SelectedValue after Page_Load has re-populated the ListBox so the SelectedIndex is reset i.e -1.
    and then you’re trying to check the SelectedValue or SelectedIndex in the event handler,so it will be -1.

    Suggestions:
    1. Populate ListBox on first time Page_Load.

    protected void Page_Load(object sender, EventArgs e)
    {
     if(!IsPostBack) 
      {
        //Bind it once on first time page load
        MyListbox.DatSource = SqlDataSource1();
        MyListbox.DataBind();
      }
    }
    

    2. Then after handling the event repopulate the MyListBox.
    3. You should make a protected method for populating ListBox.

    Note: “null exception” is due to the fact that no item is selected i.e -1 (which is actually reset on Page_Load)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small C# ASP.NET web application which generates several PNG image files
I have been creating an ASP.net web application for my client. This will be
I'm creating an asp.net web application. In the web application I have a web
I am creating a web chat application using asp.net vb. I have got the
I have an ASP .NET web application which uses Forms Authentication. Let's call this
I am creating an ASP.NET MVC 3 web application which is used for looking
I am creating a web application in ASP.net/VB.NET and I have an issue with
I am thinking about creating my new asp.net mvc web application compatible with native
I have a asp.net web application and I create a master page when i
I've got an ASP.NET application that I installed by creating a web setup. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.