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. reader is closed how can i solve this

reader is closed how can i solve this

Scheduled Pinned Locked Moved C#
helpquestioncomannouncement
7 Posts 5 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.
  • C Offline
    C Offline
    cutedrew
    wrote on last edited by
    #1

    here's my sample program in my addProducts.. if i click my save button.. when i run it, i prompts an error message that reader is closed.. can anyone help me? private void btnSaveChangesObject_Click(object sender, EventArgs e) { try { clsPublic.objConn.Open(); OdbcCommand com = new OdbcCommand("Select Name from Products", clsPublic.objConn); OdbcDataReader read; read = com.ExecuteReader(); while (read.Read()) { if (read[0].ToString() == txtprodname.Text) { MessageBox.Show("Error!"); } else try { String strSQL; if (clsPublic.BoolUpdateProductsRec == true) { strSQL = "Update products Set Id = '" + txtprodId.Text + "', Name = '" + txtprodname.Text + "', Description = '" + txtdesc.Text + "', Type = '" + txttype.Text + "', Price = '" + txtprice.Text + "', Brand = '" + txtbrand.Text + "' where Id = '" + txtprodId.Text + "'"; } else { strSQL = "Insert Into products Values('" + txtprodId.Text + "', '" + txtprodname.Text + "', '" + txtdesc.Text + "', '" + txttype.Text + "', '" + txtprice.Text + "', '" + txtbrand.Text + "')"; } SaveObjectRecord(strSQL); btnCloseSaveChangesObject.PerformClick(); } catch (NullReferenceException NRE) { MessageBox.Show("Error:" + NRE.Message, "ARTM", MessageBoxButtons.OK, MessageBoxIcon.Error); } }

    C G G 3 Replies Last reply
    0
    • C cutedrew

      here's my sample program in my addProducts.. if i click my save button.. when i run it, i prompts an error message that reader is closed.. can anyone help me? private void btnSaveChangesObject_Click(object sender, EventArgs e) { try { clsPublic.objConn.Open(); OdbcCommand com = new OdbcCommand("Select Name from Products", clsPublic.objConn); OdbcDataReader read; read = com.ExecuteReader(); while (read.Read()) { if (read[0].ToString() == txtprodname.Text) { MessageBox.Show("Error!"); } else try { String strSQL; if (clsPublic.BoolUpdateProductsRec == true) { strSQL = "Update products Set Id = '" + txtprodId.Text + "', Name = '" + txtprodname.Text + "', Description = '" + txtdesc.Text + "', Type = '" + txttype.Text + "', Price = '" + txtprice.Text + "', Brand = '" + txtbrand.Text + "' where Id = '" + txtprodId.Text + "'"; } else { strSQL = "Insert Into products Values('" + txtprodId.Text + "', '" + txtprodname.Text + "', '" + txtdesc.Text + "', '" + txttype.Text + "', '" + txtprice.Text + "', '" + txtbrand.Text + "')"; } SaveObjectRecord(strSQL); btnCloseSaveChangesObject.PerformClick(); } catch (NullReferenceException NRE) { MessageBox.Show("Error:" + NRE.Message, "ARTM", MessageBoxButtons.OK, MessageBoxIcon.Error); } }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Looks messy. When do you get the error ? Which catch block is it in ?

      Christian Graus Driven to the arms of OSX by Vista.

      P 1 Reply Last reply
      0
      • C cutedrew

        here's my sample program in my addProducts.. if i click my save button.. when i run it, i prompts an error message that reader is closed.. can anyone help me? private void btnSaveChangesObject_Click(object sender, EventArgs e) { try { clsPublic.objConn.Open(); OdbcCommand com = new OdbcCommand("Select Name from Products", clsPublic.objConn); OdbcDataReader read; read = com.ExecuteReader(); while (read.Read()) { if (read[0].ToString() == txtprodname.Text) { MessageBox.Show("Error!"); } else try { String strSQL; if (clsPublic.BoolUpdateProductsRec == true) { strSQL = "Update products Set Id = '" + txtprodId.Text + "', Name = '" + txtprodname.Text + "', Description = '" + txtdesc.Text + "', Type = '" + txttype.Text + "', Price = '" + txtprice.Text + "', Brand = '" + txtbrand.Text + "' where Id = '" + txtprodId.Text + "'"; } else { strSQL = "Insert Into products Values('" + txtprodId.Text + "', '" + txtprodname.Text + "', '" + txtdesc.Text + "', '" + txttype.Text + "', '" + txtprice.Text + "', '" + txtbrand.Text + "')"; } SaveObjectRecord(strSQL); btnCloseSaveChangesObject.PerformClick(); } catch (NullReferenceException NRE) { MessageBox.Show("Error:" + NRE.Message, "ARTM", MessageBoxButtons.OK, MessageBoxIcon.Error); } }

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        I suppose that the SaveObjectRecord method closes the connection? Then you will get the error after saving a record when you try to continue to read the rest of the records. Do you really indend to update/insert the same record over and over again, once for each record in the Products table? Or did you accidentally put the code that saves the record inside the loop instead of after the loop?

        Despite everything, the person most likely to be fooling you next is yourself.

        1 Reply Last reply
        0
        • C Christian Graus

          Looks messy. When do you get the error ? Which catch block is it in ?

          Christian Graus Driven to the arms of OSX by Vista.

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #4

          Christian Graus wrote:

          Looks messy.

          Yes, it does. Not to mention the sql injection attack vulnerability :eek:

          "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

          C 1 Reply Last reply
          0
          • P Paul Conrad

            Christian Graus wrote:

            Looks messy.

            Yes, it does. Not to mention the sql injection attack vulnerability :eek:

            "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            I've given up on pointing that out. People who hire teams who have no idea how to write secure code, deserve what they get.

            Christian Graus Driven to the arms of OSX by Vista.

            P 1 Reply Last reply
            0
            • C Christian Graus

              I've given up on pointing that out. People who hire teams who have no idea how to write secure code, deserve what they get.

              Christian Graus Driven to the arms of OSX by Vista.

              P Offline
              P Offline
              Paul Conrad
              wrote on last edited by
              #6

              Christian Graus wrote:

              given up on pointing that out

              Same here. Used to point people towards Colin's article and some were grateful and others, well, they didn't really care ( at least until someone tosses a

              '; drop database master; --

              or some crap like that into the query string ).

              "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

              1 Reply Last reply
              0
              • C cutedrew

                here's my sample program in my addProducts.. if i click my save button.. when i run it, i prompts an error message that reader is closed.. can anyone help me? private void btnSaveChangesObject_Click(object sender, EventArgs e) { try { clsPublic.objConn.Open(); OdbcCommand com = new OdbcCommand("Select Name from Products", clsPublic.objConn); OdbcDataReader read; read = com.ExecuteReader(); while (read.Read()) { if (read[0].ToString() == txtprodname.Text) { MessageBox.Show("Error!"); } else try { String strSQL; if (clsPublic.BoolUpdateProductsRec == true) { strSQL = "Update products Set Id = '" + txtprodId.Text + "', Name = '" + txtprodname.Text + "', Description = '" + txtdesc.Text + "', Type = '" + txttype.Text + "', Price = '" + txtprice.Text + "', Brand = '" + txtbrand.Text + "' where Id = '" + txtprodId.Text + "'"; } else { strSQL = "Insert Into products Values('" + txtprodId.Text + "', '" + txtprodname.Text + "', '" + txtdesc.Text + "', '" + txttype.Text + "', '" + txtprice.Text + "', '" + txtbrand.Text + "')"; } SaveObjectRecord(strSQL); btnCloseSaveChangesObject.PerformClick(); } catch (NullReferenceException NRE) { MessageBox.Show("Error:" + NRE.Message, "ARTM", MessageBoxButtons.OK, MessageBoxIcon.Error); } }

                G Offline
                G Offline
                Giorgi Dalakishvili
                wrote on last edited by
                #7

                Don't build query string on the fly, use parametrized queries: Parametrized Queries VS String Concatenation[^]

                Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

                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