reader is closed how can i solve this
-
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); } }
-
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); } }
Looks messy. When do you get the error ? Which catch block is it in ?
Christian Graus Driven to the arms of OSX by Vista.
-
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); } }
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.
-
Looks messy. When do you get the error ? Which catch block is it in ?
Christian Graus Driven to the arms of OSX by Vista.
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
-
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
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.
-
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.
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
-
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); } }
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