ListBox.SelectedValue not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbewers1
    New Member
    • Feb 2009
    • 68

    ListBox.SelectedValue not working

    Has anyone had any problems with the SelectedValue property not functioning properly within the ListBox? If so, how to fix it?

    Basically, I am binding a list of values retrieved from a select statement in MySQL:

    Code:
    /*
         * Display a list of all the papers stored within the user's database
         */ 
        protected void Page_Load(object sender, EventArgs e)
        {
            using (MySqlConnection sqlCon = new MySqlConnection(ConnectionString))
            {
                sqlCon.Open();
                MySqlCommand command = sqlCon.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = @"SELECT Title FROM Paper";
    
                List<String> titles = new List<String>();
                using (MySqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (reader[0].ToString() != string.Empty)
                        {
                            titles.Add(reader[0].ToString());
                        }
                    }
                }
                fileList.DataSource = titles;
                fileList.DataBind();
            }
        }
    And I am trying to use fileList.Select edValue to retrieve the item which the user has selected before posting this value to another site, which the user is automatically redirected to when they click on a button.

    Code:
    protected void Button2_Click(object sender, EventArgs e)
        {
            string file = fileList.SelectedItem.Text;
            Response.Redirect(string.Format("EditRecordDetails.aspx?Title={0}", file));
        }
    As mentioned, no item is selected from the list box.
    I also tried using SelectedItem, suggested by GaryTexmo, as well as using SelectedItem.Va lue, declaring a new ListBox in the Button2_Click method with the same name and hard-coding values into the ListBox but none have retrieved the value which I select from the list box

    I'm sure I've used this property in a listBox before but somehow, it seems to give me an 'object reference is not set to an instance of an object error'.

    Any ideas for how to solve what should be, a simple problem?

    Regards
    Matt
  • nispio
    New Member
    • Mar 2010
    • 3

    #2
    Your question provides no useful information at all. Please provide some information about what your program is doing, and include some code that shows how you are using it. Many times when I think that something is not functioning properly, it turns out to be an oversight on my part, and having a third party look at the code is the quickest way to spot such mistakes.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Has anyone had any problems with the SelectedValue property not functioning properly within the ListBox
      Nope. It works fine. Thanks for asking.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        I'm going to go out on a limb here and guess that you're not getting anything back with SelectedValue? There's a couple of cases here...

        1. You're just adding strings to your ListBox and trying to see what's selected at any given moment. If this is true, try using SelectedItem instead.

        2. You're binding to some kind of data type (or perhaps developing web) and need SelectedValue, chances are you're using it incorrectly. Google found a few links... give google a try in the future, it's generally kind to stumped programmers :)

        The home for technical questions and answers at Microsoft. Get started asking, answering, and browsing questions about products like .Net, Azure, or Teams.


        Gets the value of the selected item in the list control, or selects the item in the list control that contains the specified value.


        If this helps you, great, but if not and for future reference anyway, more details on your problem goes a long way for people being able to help you. A code snippet and a detailed description of your error/problem gives people a place to start. A simple "it's broken!" doesn't help at all.

        Comment

        • nispio
          New Member
          • Mar 2010
          • 3

          #5
          One problem I see in your posted code is that ListBox.Selecte dItem and ListBox.Selecte dValue both return an object, and therefore have no ".Text" property:

          Code:
          string file = fileList.SelectedItem.Text;
          You might consider trying:

          Code:
          string file = fileList.SelectedItem.ToString();

          Comment

          • tiangy
            New Member
            • Mar 2010
            • 7

            #6
            the listBox control's datasource must be the key-value pairs collection, the listbox control's display member is the value part and the listBox control's value member is the key part.
            Your list contains the values part and no key part, so listBox's value member is none,

            you can get the listBox's display memeber, such as listBox.Text, not listBox.selecte dValue.

            Comment

            • kumarsemail
              New Member
              • Oct 2015
              • 1

              #7
              Please make sure you are calling the above scripts within
              if (!IsPostBack)
              {
              // Your code goes here

              } of the page_Load method, otherwise you will not be able to get the selected dropdown list values.

              Without this condition check, you are repopulating the dropdownlist on every page load and you will not be able to get the selected dropdown list values.

              Comment

              Working...