c# - Retrieve Images from sql server database Error -
i storing images database on table called test contain (id,name,image), when try retrieve images using code :
sqlconnection cn = new sqlconnection(constring); sqlcommand cmd = new sqlcommand("select * test id ='"+txtid.text+"'", cn); sqldatareader myreader; try { cn.open(); myreader = cmd.executereader(); if (myreader.hasrows) { txtid.text = myreader[0].tostring(); txtname.text = myreader[1].tostring(); byte[] img = (byte[])(myreader[2]); if (img == null) picturebox1.image = null; else { memorystream ms = new memorystream(img); picturebox1.image = image.fromstream(ms); } } else { messagebox.show("do not found"); } cn.close(); } catch (exception ex) { messagebox.show(ex.message); }
i have error : invalid attempt read when no data present
.
hasrows
determines whether there rows. need read()
reader advance.
something should work (assuming want 1 row).
myreader = cmd.executereader(); if (myreader.read()) { ...
also sure parameterize sql select - @ moment vulnerable sql injection attacks, , dispose
of things memorystream
, datareader
Comments
Post a Comment