ImageFetch.ashx?Size=2&ImageID=11

Last post 10-03-2009 2:59 PM by MaineOne. 7 replies.

Sort Posts:

  • ImageFetch.ashx?Size=2&ImageID=11

    09-22-2009, 4:49 PM
    • Member
      4 point Member
    • Sal
    • Member since 04-02-2007, 2:55 AM
    • New Orleans, LA
    • Posts 11

     My Event Viewer keeps logging a NullReferenceException every so many minutes and the offending line is "ImageFetch.ashx?Size=2&ImageID=11". I looked at the code all day long up and down and cant seem to find anything that I know of. Anybody have any suggestions?

  • Re: ImageFetch.ashx?Size=2&ImageID=11

    09-22-2009, 5:09 PM

    Could you provide relevant code your trying in ImageFetch.ashx handler class?

  • Re: ImageFetch.ashx?Size=2&ImageID=11

    09-22-2009, 5:55 PM
    • Member
      4 point Member
    • Sal
    • Member since 04-02-2007, 2:55 AM
    • New Orleans, LA
    • Posts 11

    Here is the SqlDataSource:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:NewsCenterConnectionString %>" SelectCommand="SELECT top 5 [id], [itemdate], [title], [description], [photo] FROM [Announcements] WHERE (previewonly = 0) ORDER BY itemdate DESC"></asp:SqlDataSource>


     

    Here is the Repeater Code:

    <asp:Repeater ID="DataList1" DataSourceID="SqlDataSource1" runat="server">
    <ItemTemplate>
    <div class="NewsCenter_listitem">
    <div class="NewsCenter_Title"><a href='<%# "newscenter/News_View.aspx?Articleid=" + Convert.ToString(Eval("ID"))%>'>
    <asp:Label ID="Label4" runat="server" Text='<%# Eval("title") %>' />
    </a></div>
    <div class="NewsCenter_thumbnail"><a href='<%# "newscenter/News_View.aspx?Articleid=" + Convert.ToString(Eval("ID"))%>'><DCC:ImageThumbnail ID="ImageThumbnail2" runat="server" NoPhotoImg="App_Themes/Theme_NewsCenter/images/background_thumbnail.jpg" PhotoID='<%# Eval("photo") %>' ImageSize="FullSize" />
    </a></div>
    <div>
    <asp:Label ID="Label2" CssClass="ListItemDescription" runat="server" Text='<%# SharedRoutines.truncate((string)Eval("description")) %>' />
    </div>
    <div class="lblDate" style="margin:3px">
    <asp:Label ID="lblDate" runat="server" Text='<%# Eval("itemdate","{0:D}") %>' />
    </div>
    <a href='<%# "newscenter/News_View.aspx?Articleid=" + Convert.ToString(Eval("ID")) %>' style="text-decoration:underline"> read more »</a>
    <div style="text-align: right; padding-top:2px; padding-bottom:2px;"> <img alt="" hspace="0" align="center" border="0" src="assets/images/bar2.gif" /></div>
    <div class="NewsCenter_clearlist"> </div>
    </div>
    </ItemTemplate>
    </asp:Repeater>


       

    Event Viewer Message:

    Event code: 3005

    Event message: An unhandled exception has occurred.

    Event time: 9/22/2009 4:35:14 PM

    Event time (UTC): 9/22/2009 9:35:14 PM

    Event ID: 9f2e25c17fd240e284185d11722f523a

    Event sequence: 1777

    Event occurrence: 17

    Event detail code: 0

     

    Application information:

        Application domain: /LM/W3SVC/1/ROOT-85-128981282593510557

        Trust level: Full

        Application Virtual Path: /

        Application Path: e:\Inetpub\wwwroot\

        Machine name: MYWEBSERVER

     

    Process information:

        Process ID: 4144

        Process name: w3wp.exe

        Account name: NT AUTHORITY\NETWORK SERVICE

     

    Exception information:

        Exception type: NullReferenceException

        Exception message: Object reference not set to an instance of an object.

     

    Request information:

        Request URL: http://www.mywebsite.com/ImageFetch.ashx?Size=2&amp;ImageID=19

        Request path: /ImageFetch.ashx

        User host address: xxx.xxx.xxx.xxx

        User: 

        Is authenticated: False

        Authentication Type: 

        Thread account name: NT AUTHORITY\NETWORK SERVICE

     

    Thread information:

        Thread ID: 7

        Thread account name: NT AUTHORITY\NETWORK SERVICE

        Is impersonating: False

        Stack trace:    at ImageFetch.writeSingleImage(Int32 ImageID, Int32 size, Stream output)

       at ImageFetch.ProcessRequest(HttpContext context)

       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

     

  • Re: ImageFetch.ashx?Size=2&amp;ImageID=11

    09-22-2009, 6:09 PM
    • Participant
      1,402 point Participant
    • prashkumar22
    • Member since 03-31-2008, 4:01 AM
    • Chicago, IL
    • Posts 223

    Seeing your ImageFetch.ashx handler would be helpful , can you post that?

    Also try keeping a breakpoint in the handler and make sure you are not getting null values whereever you need it.

    looking at the error log there seems to a error at this method

    ImageFetch.writeSingleImage()


    Prashant
  • Re: ImageFetch.ashx?Size=2&amp;ImageID=11

    09-22-2009, 7:14 PM
    • Member
      4 point Member
    • Sal
    • Member since 04-02-2007, 2:55 AM
    • New Orleans, LA
    • Posts 11

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Here is the ImageFetch.ashx (It's pretty much straight from the "ClubSite" starter kit.
    I dont recall adjusting anything here. I just took the pieces and parts and learned from them.

     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    ImageFetch.ashx

    <%@ WebHandler Language="C#" Class="ImageFetch" %>
    
    using System;
    using System.Web;
    using System.Data.SqlClient;
    using System.Data;
    using System.IO;
    
    public class ImageFetch : IHttpHandler
    {
        const int BUFFERSIZE = 1024;
    
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    
        public void ProcessRequest(HttpContext context)
        {
            HttpResponse response = context.Response;
            HttpRequest request = context.Request;
            response.ContentType = "image/jpeg";
            response.Cache.SetCacheability(HttpCacheability.Public);
            response.BufferOutput = false;
            writeSingleImage(Convert.ToInt32(request.QueryString["ImageID"]), Convert.ToInt32(request.QueryString["Size"]), response.OutputStream);
            response.End();
        }
    
        public void writeSingleImage(int ImageID, int size, Stream output)
        {
            string cxnstr = System.Configuration.ConfigurationManager.ConnectionStrings["NewsCenterConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(cxnstr);
            string query;
            if (size == 0)
            {
                query = "select largeimage from images where id=@item_id";
            }
            else if (size == 1)
            {
                query = "select thumbimage from images where id=@item_id";
            }
            else if (size == 2)
            {
                query = "select origimage from images where id=@item_id";
            }
            else
            {
                query = "select largeimage from images where id=@item_id";
            }
            SqlCommand command = new SqlCommand(query, connection);
            SqlParameter param0 = new SqlParameter("@item_id", SqlDbType.Int);
            param0.Value = ImageID;
            command.Parameters.Add(param0);
            connection.Open();
    
            byte[] d = ((byte[])(command.ExecuteScalar()));
            output.Write(d, 0, d.Length);
            connection.Close();
        }
    }

     

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Here is the ImageThumbnail.ascx
    The only thing I did here was comment out the last line which I cant imagine it causing the problem.

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    <%@ Control Language="C#" ClassName="ImageThumbnail" %>
    
    <script runat="server">
        public object PhotoID
        {
            get
            {
                object o = ViewState["PhotoID"];
                return (o != null) ? (int)o : 0;
            }
            set
            {
                ViewState["PhotoID"] = (value != null && value !=DBNull.Value) ? Convert.ToInt32(value) : 0;     
            }
        }
    
        public ImageSizes ImageSize
        {
            get
            {
                object o = ViewState["ImageSize"];
                return (o != null) ? (ImageSizes)o : ImageSizes.Thumb;
            }
            set
            {
                ViewState["ImageSize"] = value;
            }
        }
        
        public enum ImageSizes
        {
            Large = 0,
            Thumb = 1,
            FullSize = 2
        }
    
        public string NoPhotoImg
        {
            get
            {
                object o = ViewState["NoPhotoImg"];
                return (o != null) ? (string)o : null;
            }
            set
            {
                ViewState["NoPhotoImg"] = value;
            }
        }
    
        protected void Page_PreRender(object sender, System.EventArgs e)
        {
            if (Convert.ToInt32(PhotoID) == 0)
            {
                if (NoPhotoImg != null)
                {
                    Image1.ImageUrl = NoPhotoImg;
                }
                else
                {
                    Image1.Visible = false;
                }
            }
            else
            {
                Image1.ImageUrl = "ImageFetch.ashx?Size=" + Convert.ToInt32(ImageSize) + "&ImageID=" + Convert.ToString(PhotoID);
            }
        }
    </script>
    <asp:Image ID="Image1" runat="server" CssClass="photo" />
    <%--<asp:Image ID="Image1" runat="server" CssClass="photo" BorderWidth="1" />--%>



     

     

  • Re: ImageFetch.ashx?Size=2&amp;ImageID=11

    09-23-2009, 6:32 AM

    Well, seeing your handler code, it seems to be alright. First thing I would suggest you to do is to use breakpoint in your ProcessRequest method. And make sure you are passing right parameter to HttpContext to ProcessRequest. Secondly, it could be because you are giving sort of rights to the method 'writeSingleImage' to write stream rather 'ProcessRequest' doing it. It might do not permit to write stream. So, try doing this:

    Bitmap newBmp = ConvertToBitmap(ShowEmpImage((long)Convert.ToInt32(ImageId)));
            if (newBmp != null)
            {
                
                newBmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                newBmp.Dispose();
            
    public void ProcessRequest(HttpContext context)  
        { 
            //   Retrieve ImageId id etc
            Bitmap newBmp = ConvertToBitmap(ShowEmpImage((long)Convert.ToInt32(ImageId)));
            if (newBmp != null)
            {
                newBmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                newBmp.Dispose();
            }
        }
    
        // Convert byte array to Bitmap (byte[] to Bitmap)
        protected Bitmap ConvertToBitmap(byte[] bmp)
        {
            if (bmp != null)
            {
                TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
                Bitmap b = (Bitmap)tc.ConvertFrom(bmp);
                return b;
            }
            return null;
        }
    
        public byte[] ShowEmpImage(long ImageId)
        {
            //  Get Bytes[] from database.
            return Pic;
        }

    }
     

    public void ProcessRequest(HttpContext context)  
        { 
            //   Retrieve ImageId id etc
            Bitmap newBmp = ConvertToBitmap(ShowEmpImage((long)Convert.ToInt32(ImageId)));
            if (newBmp != null)
            {
                newBmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                newBmp.Dispose();
            }
        }
    
        // Convert byte array to Bitmap (byte[] to Bitmap)
        protected Bitmap ConvertToBitmap(byte[] bmp)
        {
            if (bmp != null)
            {
                TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
                Bitmap b = (Bitmap)tc.ConvertFrom(bmp);
                return b;
            }
            return null;
        }
    
        public byte[] ShowEmpImage(long ImageId)
        {
            //  Get Bytes[] from database.
            return Pic;
        }



  • Re: ImageFetch.ashx?Size=2&amp;ImageID=11

    09-23-2009, 6:41 AM

    Hi,

    I think you have not declared your Image Handler in  web.config.

    <add verb="*" path="" type=""/>

    and if you have declared then check the path and type of the same.

     

     

  • Re: ImageFetch.ashx?Size=2&amp;ImageID=11

    10-03-2009, 2:59 PM
    • Participant
      1,419 point Participant
    • MaineOne
    • Member since 01-19-2006, 8:00 PM
    • Maine
    • Posts 343

    Its has been awhile since I worked with the club starter kit, but it sounds like you have a photo(id=11) linked to an event.  I believe one of the problems with this was that if you delete a photo it does not clear that link in the event, therefore when the event tries to load in the events list or default page you will get a null error.

    Check your images table and make sure you have a photo with an Id of 11, if not go into events table and clear the photo column, if you cannot clear it give it a valid id.

    aspsksolutions.com

Page 1 of 1 (8 items)