Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am able to view my images but i keep on getting this

error: "An exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll but was not handled in user code

Additional information: Could not find a part of the path 'C:\Users\Mella\Documents\My LiveAdvert Project(July-August)\LiveAds\LiveAdverts\LiveAdverts\Images\'."


Here's my code:

C#
public class ImageHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        {
            System.Data.SqlClient.SqlDataReader rdr = null;
            System.Data.SqlClient.SqlConnection conn = null;
            System.Data.SqlClient.SqlCommand selcmd = null;
            try
            {
                //string path =""
               conn = new System.Data.SqlClient.SqlConnection
              (System.Configuration.ConfigurationManager.ConnectionStrings["LiveAdsConnectionString"].ConnectionString);
              selcmd = new System.Data.SqlClient.SqlCommand("select Image from Adverts where AdvertsID=" +
              context.Request.QueryString["id"], conn);
              conn.Open();
              rdr = selcmd.ExecuteReader();
              while (rdr.Read())
                {
                    context.Response.ContentType = "image/jpg";
                    context.Response.ContentType = "image/png";
                    context.Response.WriteFile("/Images/" + rdr["Image"].ToString());

              }
                if (rdr != null)
                    rdr.Close();
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }

        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
Posted
Updated 17-Sep-15 1:01am
v2
Comments
Richard MacCutchan 17-Sep-15 7:03am    
The message is quite clear, it throws an exception if the path is not valid. You need to catch the exception and deal with it.
Member 11942189 18-Sep-15 3:23am    
Yes, the error is clear it's just that what i don't understand is why is it giving me that error because the pictures are showing and all of them are showing.
Richard MacCutchan 18-Sep-15 3:38am    
Then you need to do some debugging to find out what is happening in your code, and exactly what it is trying to access when the error occurs. Your first step is to catch the exception and look at the information it contains. If you aspire to be a developer then this is possibly the most important part of the job.
Member 11942189 18-Sep-15 3:44am    
Thank you.
ZurdoDev 17-Sep-15 7:07am    
What exactly is your question? The error is pretty clear isn't it?

1 solution

You need to use MapPath to get the correct physical path of your file. Something like this;

context.Response.WriteFile(Server.MapPath("~/Images/" + rdr["Image"].ToString()));
 
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