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. _ID field goes wrong

_ID field goes wrong

Scheduled Pinned Locked Moved C#
csharpasp-netdatabasesql-serversysadmin
8 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.
  • M Offline
    M Offline
    misCafe
    wrote on last edited by
    #1

    I have coded to retrieve data from SQL Server to ListView on C Sharp. Unfortunately, ID field goes wrong. Obviously, it has four records then it display on a listview with double recode. See my codes .... dbConnection.sqlCnn.Open(); string sSQL = "SELECT * FROM TBL_USER"; SqlCommand sCommand = new SqlCommand(sSQL, dbConnection.sqlCnn); SqlDataReader sReader = sCommand.ExecuteReader(); while (sReader.Read() == true) { _ID.Add(sReader[0].ToString()); for (int i = 0; i < _ID.Count; i++) //for (int i = 0; i < sReader.FieldCount; i++ ) { lstItems = new ListViewItem((i + 1).ToString()); lstItems.SubItems.Add(sReader[1].ToString()); lstItems.SubItems.Add(sReader[2].ToString()); lstItems.SubItems.Add(sReader[3].ToString()); lstItems.SubItems.Add(sReader[4].ToString()); lstItems.SubItems.Add(sReader[5].ToString()); this.lsv_user.Items.Add(lstItems); } } sReader.Close(); dbConnection.sqlCnn.Close(); Thanks

    Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

    L F 2 Replies Last reply
    0
    • M misCafe

      I have coded to retrieve data from SQL Server to ListView on C Sharp. Unfortunately, ID field goes wrong. Obviously, it has four records then it display on a listview with double recode. See my codes .... dbConnection.sqlCnn.Open(); string sSQL = "SELECT * FROM TBL_USER"; SqlCommand sCommand = new SqlCommand(sSQL, dbConnection.sqlCnn); SqlDataReader sReader = sCommand.ExecuteReader(); while (sReader.Read() == true) { _ID.Add(sReader[0].ToString()); for (int i = 0; i < _ID.Count; i++) //for (int i = 0; i < sReader.FieldCount; i++ ) { lstItems = new ListViewItem((i + 1).ToString()); lstItems.SubItems.Add(sReader[1].ToString()); lstItems.SubItems.Add(sReader[2].ToString()); lstItems.SubItems.Add(sReader[3].ToString()); lstItems.SubItems.Add(sReader[4].ToString()); lstItems.SubItems.Add(sReader[5].ToString()); this.lsv_user.Items.Add(lstItems); } } sReader.Close(); dbConnection.sqlCnn.Close(); Thanks

      Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, your code does not make sense to me. the while loop iterates over the records, that is OK. the for loop should not be there: IMO each record only provides data for one item in the ListView. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      M 1 Reply Last reply
      0
      • M misCafe

        I have coded to retrieve data from SQL Server to ListView on C Sharp. Unfortunately, ID field goes wrong. Obviously, it has four records then it display on a listview with double recode. See my codes .... dbConnection.sqlCnn.Open(); string sSQL = "SELECT * FROM TBL_USER"; SqlCommand sCommand = new SqlCommand(sSQL, dbConnection.sqlCnn); SqlDataReader sReader = sCommand.ExecuteReader(); while (sReader.Read() == true) { _ID.Add(sReader[0].ToString()); for (int i = 0; i < _ID.Count; i++) //for (int i = 0; i < sReader.FieldCount; i++ ) { lstItems = new ListViewItem((i + 1).ToString()); lstItems.SubItems.Add(sReader[1].ToString()); lstItems.SubItems.Add(sReader[2].ToString()); lstItems.SubItems.Add(sReader[3].ToString()); lstItems.SubItems.Add(sReader[4].ToString()); lstItems.SubItems.Add(sReader[5].ToString()); this.lsv_user.Items.Add(lstItems); } } sReader.Close(); dbConnection.sqlCnn.Close(); Thanks

        Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

        F Offline
        F Offline
        fly904
        wrote on last edited by
        #3

        misCafe wrote:

        while (sReader.Read() == true) { _ID.Add(sReader[0].ToString()); for (int i = 0; i < _ID.Count; i++) //for (int i = 0; i < sReader.FieldCount; i++ ) { lstItems = new ListViewItem((i + 1).ToString()); lstItems.SubItems.Add(sReader[1].ToString()); lstItems.SubItems.Add(sReader[2].ToString()); lstItems.SubItems.Add(sReader[3].ToString()); lstItems.SubItems.Add(sReader[4].ToString()); lstItems.SubItems.Add(sReader[5].ToString()); this.lsv_user.Items.Add(lstItems); } }

        With that, you are adding the same record to the ListView n(sReader.FieldCount) times. Your problem is the line in bold, you are adding the same ID/name ((i + 1).ToString()) for each record. Each ListViewItem should have a different name or be blank (no parameter). I think I know what you are trying to do, so I hope this helps:

        if (sReader.FieldCount > 0)
        {
        _ID.Add(sReader[0].ToString());
        lstItems = new ListViewItem(sReader[0].ToString()); //Notice: If this is your ID column then it should be unique each record.
        for (int i = 1; i < sReader.FieldCount; i++)
        {
        lstItems.SubItems.Add(sReader[i].ToString());
        }
        this.lsv_user.Items.Add(lstItems);
        }

        You just need to replace the content of your while loop.

        My failometer is detecting vast quantities of FAIL! "Its SQL - hardly programming..." (Caslen)

        M 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, your code does not make sense to me. the while loop iterates over the records, that is OK. the for loop should not be there: IMO each record only provides data for one item in the ListView. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          M Offline
          M Offline
          misCafe
          wrote on last edited by
          #4

          Well, I have commented on //for (int i = 0; i < sReader.FieldCount; i++ ) so, it isn't used. Exactly, if we do a right code, it will display a correct recode (only four records on listview). In db it has only four records. After I have coded (code i have posted), it displays many records (duplicate records) on the listview. One record in db it shows many records on listview. Do you make sense? :)

          Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

          F L 2 Replies Last reply
          0
          • M misCafe

            Well, I have commented on //for (int i = 0; i < sReader.FieldCount; i++ ) so, it isn't used. Exactly, if we do a right code, it will display a correct recode (only four records on listview). In db it has only four records. After I have coded (code i have posted), it displays many records (duplicate records) on the listview. One record in db it shows many records on listview. Do you make sense? :)

            Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

            F Offline
            F Offline
            fly904
            wrote on last edited by
            #5

            misCafe wrote:

            Do you make sense?

            Yes he does, you on the other hand don't. It seems that you are confussing 'records' and 'fields', records are the rows in the database and fields are the columns. The code that you have provided shows that you are adding the same row for every field (column) you have. sReader is the record (row), sReader[i] is the field (column). Hence why you go through the DataReader record by record (sReader.ReadLine()), and for each record you then go through it field by field, as shown in my other post. I hope that clears things up.

            My failometer is detecting vast quantities of FAIL! "Its SQL - hardly programming..." (Caslen)

            L 1 Reply Last reply
            0
            • F fly904

              misCafe wrote:

              Do you make sense?

              Yes he does, you on the other hand don't. It seems that you are confussing 'records' and 'fields', records are the rows in the database and fields are the columns. The code that you have provided shows that you are adding the same row for every field (column) you have. sReader is the record (row), sReader[i] is the field (column). Hence why you go through the DataReader record by record (sReader.ReadLine()), and for each record you then go through it field by field, as shown in my other post. I hope that clears things up.

              My failometer is detecting vast quantities of FAIL! "Its SQL - hardly programming..." (Caslen)

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              :rose:

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              1 Reply Last reply
              0
              • M misCafe

                Well, I have commented on //for (int i = 0; i < sReader.FieldCount; i++ ) so, it isn't used. Exactly, if we do a right code, it will display a correct recode (only four records on listview). In db it has only four records. After I have coded (code i have posted), it displays many records (duplicate records) on the listview. One record in db it shows many records on listview. Do you make sense? :)

                Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                you have two for loops, only one of them is commented out. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                1 Reply Last reply
                0
                • F fly904

                  misCafe wrote:

                  while (sReader.Read() == true) { _ID.Add(sReader[0].ToString()); for (int i = 0; i < _ID.Count; i++) //for (int i = 0; i < sReader.FieldCount; i++ ) { lstItems = new ListViewItem((i + 1).ToString()); lstItems.SubItems.Add(sReader[1].ToString()); lstItems.SubItems.Add(sReader[2].ToString()); lstItems.SubItems.Add(sReader[3].ToString()); lstItems.SubItems.Add(sReader[4].ToString()); lstItems.SubItems.Add(sReader[5].ToString()); this.lsv_user.Items.Add(lstItems); } }

                  With that, you are adding the same record to the ListView n(sReader.FieldCount) times. Your problem is the line in bold, you are adding the same ID/name ((i + 1).ToString()) for each record. Each ListViewItem should have a different name or be blank (no parameter). I think I know what you are trying to do, so I hope this helps:

                  if (sReader.FieldCount > 0)
                  {
                  _ID.Add(sReader[0].ToString());
                  lstItems = new ListViewItem(sReader[0].ToString()); //Notice: If this is your ID column then it should be unique each record.
                  for (int i = 1; i < sReader.FieldCount; i++)
                  {
                  lstItems.SubItems.Add(sReader[i].ToString());
                  }
                  this.lsv_user.Items.Add(lstItems);
                  }

                  You just need to replace the content of your while loop.

                  My failometer is detecting vast quantities of FAIL! "Its SQL - hardly programming..." (Caslen)

                  M Offline
                  M Offline
                  misCafe
                  wrote on last edited by
                  #8

                  Thank you very much again. I understand what you said and it is solved now. :)

                  Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

                  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