Click here to Skip to main content
Sign Up to vote bad
good
See more: C#.NET
i tried below code
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
 
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 17 Feb '13 - 18:58
Edited 17 Feb '13 - 19:04

Comments
Karthik Harve - 18 Feb '13 - 1:05
[Edit] added pre tags.

3 solutions

more solutions pleassssseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
  Permalink  
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: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
private void fillGrid(DataTable dt)
{
    myGrid.DataSource = dt;
    myGrid.DataBind()
       
}
 
3. Implement IHttpHandler for image
<%@ 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;
        }
    }
 
}
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 243
1 Rohan Leuva 220
2 Sergey Alexandrovich Kryukov 208
3 Abhinav S 168
4 Mahesh Bailwal 165
0 Sergey Alexandrovich Kryukov 8,494
1 OriginalGriff 6,799
2 CPallini 3,603
3 Rohan Leuva 2,923
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid