6
Answers

how to retrieve image from db

I am displaying sme images...some of them are displaying half part..some of them not displaying....
pls solve this
aspx:
html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title></title>
<%--<script language="javascript" type="text/javascript">
    function validate() {
        var result = false;
        var upfile = document.getElementById("FileUpload1").value;
        if (upfile != "") {
            var accept = "png,gif,jpg,jpeg".split(',');
            var getExtention = upfile.split('.');
            var extention = getExtention[getExtention.length - 1];
            for (i = 0; i < accept.length; i++) {
                if (accept[i] == extention) {
                    result = true;
                    break;
                }
            }
            if (!result) {
                alert("allowed file extention are png,gif,jpg,jpeg");
            }
        }
        else {
            alert("select image to Upload");
        }
        return result;
    }
        
</script>--%>

</head>
<body>
<form id="form1" runat="server">
<div>
Select file to save into the database: 
<asp:FileUpload runat="server" ID="FileUpload1" />
<asp:Button runat="server" ID="btnSave" OnClick="SaveToTheDatabase" 
Text="Save to the database"   onclientclick="return validate();" />
<p><asp:Label ID="lblMessage" runat="server" EnableViewState="false" /></p>

    <asp:Button ID="Button1" runat="server" Text="Preview" 
        onclick="Button1_Click" />

<%--<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>--%>
<asp:Panel ID="Panel1" runat="server" BorderStyle="Groove" Height="200px" Width="200px">
<asp:Image ID="ImID1" runat="server" 
   Height="200px" Width="200px" Visible="true"/>
   </asp:Panel>
<%--</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>--%>
</div>
</form>
</body>
</html>


codebehind:
 protected void Button1_Click(object sender, EventArgs e)
    {
        ImID1.ImageUrl = "Handler.ashx?imgFromDB=" + Session["id"];

       }

handler.ashx:
<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using eBizLink_DAL;
using eBizLink_BLL;
//using System.Collections;

public class Handler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
       
        SqlDataReader dr = null;
        string image = "";
        if (context.Request.QueryString["imgFromDB"] != null)
            image = (context.Request.QueryString["imgFromDB"]).ToString();

        //string image = Session["ConnMemID"];
        if (image != "")
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            connection.Open();
            SqlCommand command = new SqlCommand("select top(1) Image from tbl_Images where MemId=5 order by ImgId desc", connection);
            dr = command.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    context.Response.BinaryWrite((byte[])dr["Image"]);
                    context.Response.Flush();
                    context.Response.End();

                    //SqlDataReader dr = null;
                    //dr = SqlHelper.ExecuteReader(CommonSetting.GetConnectionString(true), CommandType.StoredProcedure, "sp_getImg");
                    //if (dr.Read())
                    //{
                    //    Response.BinaryWrite((byte[])dr["Image"]);
                    
                }
            }

            else
            {

                context.Response.WriteFile("Images/default-profile.jpg");
                context.Response.End();

            }
            connection.Close();


        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
Answers (6)