Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi
when i am get the image from database by using Handler here i have a small Problem

In Local this code is executed successfully but when that code is uploaded in hostgator server I am getting The "Failed to load the Given Url"

The ASPX Code
XML
<asp:DataList ID="DataList1" runat="server"
            RepeatColumns="3" RepeatDirection="Horizontal">
            <ItemTemplate>
                <table>
                    <tr>
                        <td valign="middle" align="center" style="background-color:#cccccc;border:1px solid gray;width:150px;height:150px;"><%#DataBinder.Eval(Container.DataItem, "images") %></td>
                    </tr>
                </table>

            </ItemTemplate>
        </asp:DataList>

The ASPX.CS Code
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        BindDataList();
}
private void BindDataList()
{
    string sqlCmd = "SELECT carprofile FROM ca";
    DataBaseHelper DBHelper = new DataBaseHelper();
    DataTable dt = DBHelper.GetTable(sqlCmd);
    //adding new column to disply image
    DataColumn imageCol = new DataColumn("images", typeof(string));
    dt.Columns.Add(imageCol);        
    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i][imageCol] = string.Format("<img src='Handler.ashx?id={0}' alt='' style='width:100px' />", dt.Rows[i][0].ToString());
        }
    }
    DataList1.DataSource = dt;
    DataList1.DataBind();
}

Handler code
HTML
<%@ WebHandler Language="C#" Class="Handler" %>

C#
using System;
using System.Web;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        HttpRequest request = context.Request;
        if (!string.IsNullOrEmpty(request.QueryString["id"]))
        {
            //this hash table contain the SP parameter
            Hashtable hash = new Hashtable();
            hash.Add("@carprofile", request.QueryString["id"]);
            DataBaseHelper DBHelper = new DataBaseHelper();

            //DBHelper.SQLExecuteNonQuery(procedure_name,command_parameters) return the object data.
            // casting return value to byte[]
            byte[] imageByte = (byte[])DBHelper.SQLExecuteNonQuery("Get_images", hash);
            //checking byte[] 
            if (imageByte != null && imageByte.Length > 0)
            {
                context.Response.ContentType = "image/jpeg";
                context.Response.BinaryWrite(imageByte);
            }
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}

Plzzz Give me the Solution for this


Thank you
Posted
Updated 14-Mar-13 23:58pm
v2
Comments
Karthik Harve 15-Mar-13 5:58am    
[Edit] spelling and pre tags added.
Yaseer Arafat 22-Mar-13 8:29am    
http://stackoverflow.com/questions/8522503/failed-to-load-gived-url

1 solution

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