Invali attempt to call metadata when reader is closed c# winform -
this edited code removed try catch see getting exceptions. alse marked edited code. code getting on nerves have been googling problem , tried found. checked every connection other code there seems no problem them. each , every connection closed.
querystring = "select * product prd_code = @c or prd_name=@pn "; sqlcommand command = new sqlcommand(querystring, con); command.parameters.addwithvalue("@c", convert.toint32(txtpcode.text)); command.parameters.addwithvalue("@pn", txtpname.text.tostring()); con.open(); //edited here using (sqldatareader dr = command.executereader()) { // , here if (dr != null && fr.hasrows && dr.read() == true) { if (txtpcode.text == "") { txtpcode.text = dr["prd_code"].tostring(); } else if (txtpname.text == "") { txtpname.text = dr["prd_name"].tostring(); } txtpcompany.text = dr["prd_company"].tostring(); txtunitprice.text = dr["prd_price"].tostring(); txtdiscount.text = dr["prd_dis"].tostring(); txtfinalrate.text = dr["prd_final"].tostring(); } else { messagebox.show("no such record exists"); if (txtpname.text == "") { txtpcode.text = ""; } else if (txtpcode.text == "") { txtpname.text = ""; } con.close(); } } }
using (var reader = command.executereader()) { if (reader.hasrows) { if(reader.read()) { if (txtpcode.text == "") { txtpcode.text = dr["prd_code"].tostring(); } else if (txtpname.text == "") { txtpname.text = dr["prd_name"].tostring(); } txtpcompany.text = dr["prd_company"].tostring(); txtunitprice.text = dr["prd_price"].tostring(); txtdiscount.text = dr["prd_dis"].tostring(); txtfinalrate.text = dr["prd_final"].tostring(); } } }
Comments
Post a Comment