Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i tried below code
C#
public void imagefetch()
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        SqlConnection imagecon = new SqlConnection(onlineexam_Conn);
        string imagequery = "select rcimage,permitimage,pollutionimage,fitnessimage,taximage,insuranceimage from gps_vehicledetails where registration='" + vehicleSelect.SelectedItem.ToString() + "'";
        SqlDataAdapter da = new SqlDataAdapter(imagequery, imagecon);
        da.Fill(ds);
        dt = ds.Tables[0];
        showimages.DataSource = dt;
        showimages.DataBind();
        imagecon.Close();
    }


showimages is a grid control but i am unable to fetch image in grid. plese help me

C#
public void ProcessRequestt(HttpContext context)
    {
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection(onlineexam_Conn);
        SqlCommand command = new SqlCommand("select rcimage,permitimage,pollutionimage,fitnessimage,taximage,insuranceimage from gps_vehicledetails where registration='AP 29 TA 1966'", connection);
        connection.Open();
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((Byte[])dr[0]);
        showimages.DataSource = dr;
        showimages.DataBind();
        connection.Close();
        context.Response.End();
    }  

in page load i am using image control property to retrieve image from database but i am unable to fetch image in image control. Please help me
Posted
Updated 17-Feb-13 19:04pm
v3
Comments
Karthik Harve 18-Feb-13 1:05am    
[Edit] added pre tags.

hi dear,
first of all device your code into 3 part.

1. design a grid
2. Bind Your Code
3. Implement IHttpHandler for image


1. Design
ASP
<asp:templatefield headertext="Photo" xmlns:asp="#unknown">
     <itemtemplate>
           <asp:image id="empImage" runat="server" imageurl="<%# "ImageHandler.ashx?ImID="+ Eval("ID") %>" width="100" height="120" alternatetext="Student Photo" descriptionurl="~/image/no-image-a.jpg" />
     </itemtemplate>
</asp:templatefield>


2. Bind Your Code
C#
private void fillGrid(DataTable dt)
{
    myGrid.DataSource = dt;
    myGrid.DataBind()
       
}


3. Implement IHttpHandler for image
C#
<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

public class ImageHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        string imageid = Convert.ToString(context.Request.QueryString["ImID"]);
        if (imageid != null && imageid.Trim() != "")
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ToString());
            connection.Open();
            SqlCommand command = new SqlCommand("select PHOTO from EmpMst where ID=" + imageid, connection);
            SqlDataReader dr = command.ExecuteReader();
            dr.Read();
            try
            {
                    context.Response.BinaryWrite((Byte[])dr[0]);
                    connection.Close();
                    //context.Response.End();
            }
            catch
            {
                Byte[] b = File.ReadAllBytes(context.Server.MapPath("~/image/no-image-a.jpg"));
                context.Response.BinaryWrite(b);
                connection.Close();
                context.Response.End();
            }
        }
        else
        {
            Byte[] b = File.ReadAllBytes(context.Server.MapPath("~/image/no-image-a.jpg"));
            context.Response.BinaryWrite(b);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
 
Share this answer
 
v3
more solutions pleassssseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
 
Share this answer
 

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