Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ListView SelectedIndexChanged exception

ListView SelectedIndexChanged exception

Scheduled Pinned Locked Moved C#
databasehelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    piticcotoc
    wrote on last edited by
    #1

    Hello. I have a listview where I load data from sql database. ListView has two columns (name, surname) where data is loaded. On the selectedindexchanged event, when an item from the listview is selected it gets the other data from the database, based on selection, and writes it to several textboxes... basicly a phonebook. If I select someone from the list the info is displayed in the txtboxes, but if i click smeone a second time i get an exception "NullRefferenceException" "Object reference not set to an instance of an object."

    private void lvNameList_SelectedIndexChanged(object sender, EventArgs e)
    {
    ClearSearch();
    select = "SELECT * FROM agenda_telefonica WHERE nume = '" + lvNameList.FocusedItem.Text + "' AND prenume = '" + lvNameList.FocusedItem.SubItems[1].Text + "'";
    SqlDataReader dr = realSql.Select(select);
    if (!dr.HasRows)
    {
    MessageBox.Show("Information not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
    while (dr.Read())
    {
    tbName.Text = dr[1].ToString().Trim() + " " + dr[2].ToString().Trim();
    tbPhoneNumber1.Text = dr[3].ToString().Trim();
    tbPhoneNumber2.Text = dr[4].ToString().Trim();
    tbPhoneNumber3.Text = dr[5].ToString().Trim();
    tbFaxNumber.Text = dr[6].ToString().Trim();
    tbEmail.Text = dr[7].ToString().Trim();
    tbCompany.Text = dr[8].ToString().Trim();
    tbService.Text = dr[9].ToString().Trim();
    tbEquipment.Text = dr[10].ToString().Trim();
    }
    }
    dr.Close();
    realSql.Close();
    }
    }

    The exception is raised the second time i click an item in the list and points to the select string. Does it have something to do with the FocusedItem?

    D H 2 Replies Last reply
    0
    • P piticcotoc

      Hello. I have a listview where I load data from sql database. ListView has two columns (name, surname) where data is loaded. On the selectedindexchanged event, when an item from the listview is selected it gets the other data from the database, based on selection, and writes it to several textboxes... basicly a phonebook. If I select someone from the list the info is displayed in the txtboxes, but if i click smeone a second time i get an exception "NullRefferenceException" "Object reference not set to an instance of an object."

      private void lvNameList_SelectedIndexChanged(object sender, EventArgs e)
      {
      ClearSearch();
      select = "SELECT * FROM agenda_telefonica WHERE nume = '" + lvNameList.FocusedItem.Text + "' AND prenume = '" + lvNameList.FocusedItem.SubItems[1].Text + "'";
      SqlDataReader dr = realSql.Select(select);
      if (!dr.HasRows)
      {
      MessageBox.Show("Information not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
      else
      {
      while (dr.Read())
      {
      tbName.Text = dr[1].ToString().Trim() + " " + dr[2].ToString().Trim();
      tbPhoneNumber1.Text = dr[3].ToString().Trim();
      tbPhoneNumber2.Text = dr[4].ToString().Trim();
      tbPhoneNumber3.Text = dr[5].ToString().Trim();
      tbFaxNumber.Text = dr[6].ToString().Trim();
      tbEmail.Text = dr[7].ToString().Trim();
      tbCompany.Text = dr[8].ToString().Trim();
      tbService.Text = dr[9].ToString().Trim();
      tbEquipment.Text = dr[10].ToString().Trim();
      }
      }
      dr.Close();
      realSql.Close();
      }
      }

      The exception is raised the second time i click an item in the list and points to the select string. Does it have something to do with the FocusedItem?

      D Offline
      D Offline
      Druuler
      wrote on last edited by
      #2

      Before doing anything else check the if the SelectedItems.Count property is > 0.

      P 1 Reply Last reply
      0
      • D Druuler

        Before doing anything else check the if the SelectedItems.Count property is > 0.

        P Offline
        P Offline
        piticcotoc
        wrote on last edited by
        #3

        Thanks. :)

        1 Reply Last reply
        0
        • P piticcotoc

          Hello. I have a listview where I load data from sql database. ListView has two columns (name, surname) where data is loaded. On the selectedindexchanged event, when an item from the listview is selected it gets the other data from the database, based on selection, and writes it to several textboxes... basicly a phonebook. If I select someone from the list the info is displayed in the txtboxes, but if i click smeone a second time i get an exception "NullRefferenceException" "Object reference not set to an instance of an object."

          private void lvNameList_SelectedIndexChanged(object sender, EventArgs e)
          {
          ClearSearch();
          select = "SELECT * FROM agenda_telefonica WHERE nume = '" + lvNameList.FocusedItem.Text + "' AND prenume = '" + lvNameList.FocusedItem.SubItems[1].Text + "'";
          SqlDataReader dr = realSql.Select(select);
          if (!dr.HasRows)
          {
          MessageBox.Show("Information not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
          else
          {
          while (dr.Read())
          {
          tbName.Text = dr[1].ToString().Trim() + " " + dr[2].ToString().Trim();
          tbPhoneNumber1.Text = dr[3].ToString().Trim();
          tbPhoneNumber2.Text = dr[4].ToString().Trim();
          tbPhoneNumber3.Text = dr[5].ToString().Trim();
          tbFaxNumber.Text = dr[6].ToString().Trim();
          tbEmail.Text = dr[7].ToString().Trim();
          tbCompany.Text = dr[8].ToString().Trim();
          tbService.Text = dr[9].ToString().Trim();
          tbEquipment.Text = dr[10].ToString().Trim();
          }
          }
          dr.Close();
          realSql.Close();
          }
          }

          The exception is raised the second time i click an item in the list and points to the select string. Does it have something to do with the FocusedItem?

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          You are closing realSql which is not opened in the method you have posted. It could therefore be realSql (null after closing) that is causing the exception.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          P 1 Reply Last reply
          0
          • H Henry Minute

            You are closing realSql which is not opened in the method you have posted. It could therefore be realSql (null after closing) that is causing the exception.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            P Offline
            P Offline
            piticcotoc
            wrote on last edited by
            #5

            realSql is a dll refference to a sql class i've created. it has an select(string select) which open an sql connection then returns a datareader ("SqlDataReader dr = realSql.Select(select)"). So the realSql.Close() closes the connection opened by the Select() method.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups