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. Web Development
  3. ASP.NET
  4. Not showing image from sql database?

Not showing image from sql database?

Scheduled Pinned Locked Moved ASP.NET
databasecsharpasp-nethelpquestion
22 Posts 3 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.
  • A AdamskiR

    It looks like there could be something wrong with the way the image is being uploaded as in the _comBuf property it is showing as < undefined value >

    M Offline
    M Offline
    minhpc_bk
    wrote on last edited by
    #21

    Hi Adam, One more thing that I failed to remind you that the DataReader object works "on-line" and there is no data in its buffer until you really invoke the dr object with the column name or column index to pull data from that column in DB. Take a look at the sample code below:

    1: SqlDataReader dr = command.ExecuteReader();
    2: if ( dr.Read())
    3: {
    4: Response.ContentType = "image/GIF";
    5: byte[] buffer = (byte[])dr[0];
    6: Response.BinaryWrite(buffer);
    7: }
    8: sqlConnection1.Close();

    Before the execution reachs the line 6, you still see the < undefined value > in the _comBuf property. Once you invoke the code to pull data from the first column dr[0] or dr["ImageFile"] at line 5, then the image data by that time is actually pulled out of DB and stored in the buffer of the dr datareader object. So in general, when the line 6 gets reached, you can look up the _comBuf property to see the image size. [Edit] In addition to the Quick Watch window, when you run your application in debug mode at runtime you can make use of the Locals window to watch the value of the dr object. For more information, you can see Using the Locals Window[^]

    S 1 Reply Last reply
    0
    • M minhpc_bk

      Hi Adam, One more thing that I failed to remind you that the DataReader object works "on-line" and there is no data in its buffer until you really invoke the dr object with the column name or column index to pull data from that column in DB. Take a look at the sample code below:

      1: SqlDataReader dr = command.ExecuteReader();
      2: if ( dr.Read())
      3: {
      4: Response.ContentType = "image/GIF";
      5: byte[] buffer = (byte[])dr[0];
      6: Response.BinaryWrite(buffer);
      7: }
      8: sqlConnection1.Close();

      Before the execution reachs the line 6, you still see the < undefined value > in the _comBuf property. Once you invoke the code to pull data from the first column dr[0] or dr["ImageFile"] at line 5, then the image data by that time is actually pulled out of DB and stored in the buffer of the dr datareader object. So in general, when the line 6 gets reached, you can look up the _comBuf property to see the image size. [Edit] In addition to the Quick Watch window, when you run your application in debug mode at runtime you can make use of the Locals window to watch the value of the dr object. For more information, you can see Using the Locals Window[^]

      S Offline
      S Offline
      sfawcett
      wrote on last edited by
      #22

      when you get an image out it may be too big to come in one go so this is how i get an image out using a web service [WebMethod] public byte[] GetPic(int iId) { MemoryStream ms = new MemoryStream(); try { string sSql="select Picture from tblPics where id="+iId.ToString(); SqlConnection objConn = new SqlConnection(msConnection); SqlCommand objCommand = new SqlCommand(sSql,objConn); objConn.Open(); SqlDataReader objData = objCommand.ExecuteReader(CommandBehavior.SequentialAccess); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; if(objData.Read()) { long startIndex = 0; long retval = objData.GetBytes(0, startIndex, buffer, 0,bufferSize); while (retval == bufferSize) { ms.Write(buffer,0,buffer.Length); startIndex += bufferSize; retval = objData.GetBytes(0, startIndex, buffer, 0, bufferSize); } ms.Write(buffer,0,buffer.Length); } objData.Close(); objConn.Close(); } catch(Exception ex) { Debug.WriteLine(ex.Message); } return ms.GetBuffer(); }

      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