Click here to Skip to main content
16,004,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,



i am upload the images in database by using Bytes format .when i am retrive that image by using handler.ashx Here i am geting error is

"Failed to load the Give Url "


I writen code Is in .Cs file is..

C#
Image1.ImageUrl = "Handler.ashx?ProfileId=" + "MC_3967";


In ASPX CODE
ASP
<asp:Image ID="Image1" style="width:150px" Runat="server" />


In handler Code....
C#
try
{
    MemoryStream memoryStream = new MemoryStream();
    SqlDataAdapter cmd = new SqlDataAdapter("select ima from ca where  carprofile='" + pid + "'", CON);
    DataSet ds = new DataSet();
    cmd.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {
        if (ds.Tables[0].Rows[0][0].ToString() != "")
        {
            byte[] file = (byte[])ds.Tables[0].Rows[0][0];
            memoryStream.Write(file, 0, file.Length);
            context.Response.Buffer = true;
            if (file.Length > 0)
            {
                context.Response.ContentType = "image/jpeg";
                context.Response.BinaryWrite(file);
                memoryStream.Dispose();
            }
        }
        else
        {

        }
    }
    else
    {

    }
}
catch (Exception ex)
{ }



But Iam Geting This Error "Failed to load the Give Url "

Give a solution

Thank you
Posted
Updated 13-Mar-13 2:11am
v2
Comments
max_nowak 13-Mar-13 8:23am    
First, I don't know what goes wrong here. But have you considered just to store the path of the image, and not the image itself in your database? The image would then lie somewhere on your webserver.

1 solution

Some questions and pointers:
1. Instead of using if (ds.Tables[0].Rows[0][0].ToString() != "") wich will fail anyway if the value is null use the following:
C#
if (ds.Tables[0].Rows[0][0] != DBNull.Value)


2. Next you would want to indicate what line causes the crash

3. look here[^] perhaps that helps you on the way.

4. don't write else statements that are emtpy, they're useless.
5. don't swallow Exceptions with empty catch statements, often done to avoid an application crash, but the very least you should log the error and inform the user.

6. "select ima from ca where carprofile='" + pid + "'" --> use parametrized queries !

Hope this helps.
 
Share this answer
 
v2

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