sql server - How to display image stored in a database table in a picturebox when selecting a record in datagridview in c# -


this problem i'm having. have sql server table contains few columns. 1 of them of image datatype. image saved binary data.

on form have datagridview shows 3 columns table. none of them contains image. when click on record datagridview want image displayed in picturebox same form.

this code have far:

public void datagridview1_cellcontentclick(object sender, datagridviewcelleventargs e) {         sqlconnection cn = new sqlconnection(@" data source=home-d2cadc8d4f\sql;initial catalog=motociclete;integrated security=true");          if (e.rowindex >= 0) {             datagridviewrow row = datagridview1.selectedrows[0];             textbox1.text = row.cells["anf"].value.tostring();             textbox2.text = row.cells["putere"].value.tostring();             textbox3.text = row.cells["caprez"].value.tostring();             textbox4.text = row.cells["greutate"].value.tostring();             textbox5.text = row.cells["stoc"].value.tostring();             textbox6.text = row.cells["pret"].value.tostring();             textbox7.text = row.cells["garantie"].value.tostring();              sqlcommand cmd = new sqlcommand("select poza motociclete codm '" + datagridview1.selectedrows + "'", cn);              cn.open();              try             {                 sqldatareader dr = cmd.executereader();                  if (dr.read())                 {                    byte[] picarr = (byte[])dr["poza"];                     memorystream ms = new memorystream(picarr);                     ms.seek(0, seekorigin.begin);                     picturebox1.image = image.fromstream(ms);                }             }             catch (exception ex)             {                 messagebox.show(ex.message);             }             {                 cn.close();             }         }     } 

i think it's syntax error, don't know or how fix it.

i’d suggest save image in temporary folder , show using imagelocation property.

if (dr.read()) {        byte[] picarr = (byte[])dr["poza"];        string imagepath = "path temp folder/image123.jpg";        file.writeallbytes(imagepath ,picarr);        picturebox1.imagelocation = imagepath; } 

main advantage of approach can implement kind of caching don’t have retrieve images database every time if have been selected recently. if application busy can have strong impact on application performance.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -