Click here to Skip to main content
15,908,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi iam yogesh i saved image in sql table image datatype.now when iam retriving it to the page iam getting the blank page without image with the text displaying at the upper left corner--> System.Byte[]. please answer me step by step. i have searched many times for solution but failed.... pls reply soon friends.
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using salefunctions;
namespace ADVANCED_FORUM
{
    public partial class imageview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            methods.createcon("forum"); /* this is method to create connection and forum is database name*/
            SqlCommand dispimgcmd=new SqlCommand("select * from reginfo where userid=" + "'" + Session["mlinkquid"] + "'", methods.connection);
            SqlDataReader drdispimg = dispimgcmd.ExecuteReader();
            drdispimg.Read();
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = (string)drdispimg["imgstrtype"];
            Response.OutputStream.Write((byte[])drdispimg["imgdata"],0,(int)drdispimg["imgsize"]);
            methods.connection.Close();
            Response.End();
        }
    }
}

please tell me what's wrong with this code. everything is ok. i have checked connection, contenttype ,contentlength,etc. everything is ok but still image is not displayed on the page.
displayed
Posted
Updated 7-Aug-11 9:30am
v2
Comments
Herman<T>.Instance 7-Aug-11 15:44pm    
If you debug your code where does it fail and with what exception?

make a ashx handler file and put similar code in it

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DemoCode
{
    /// <summary>
    /// Summary description for Demo
    /// </summary>
    public class Demo : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            methods.createcon("forum"); /* this is method to create connection and forum is database name*/
            SqlCommand dispimgcmd = new SqlCommand("select * from reginfo where userid=" + "'" + Session["mlinkquid"] + "'", methods.connection);
            SqlDataReader drdispimg = dispimgcmd.ExecuteReader();
            drdispimg.Read();
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = (string)drdispimg["imgstrtype"];
            Response.OutputStream.Write((byte[])drdispimg["imgdata"], 0, (int)drdispimg["imgsize"]);
            methods.connection.Close();
            Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


then add an image to you page and point it to the handler file for it's source

<pre lang="HTML"><img src="/Demo.ashx" />


obviously you'd want to add querystrings after ashx eg: src='/Demo.ashx?imageID=45'.

Also please note that the select * should be on the main page and you would in the handler file receive imageID from the querystring and that's the image you'd write out
 
Share this answer
 
 
Share this answer
 
Comments
yogesh a sharma 8-Aug-11 12:52pm    
thanks but i tried both your link but failed to get solution. one thing when i saved image to my hard drive using filestream, image is being saved but when i open it. it says, file appears to be damaged of corrupted. i used jpg. pls give me solution....plllllllllllllsssss
Monjurul Habib 8-Aug-11 17:50pm    
use try catch and debug your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900