Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# image binary
Hi all,
 
I am busy writing an application that displays the generated images from a barcode library I found on Code Project.
The library is quite smart, and will return good errors if something goes wrong, like wrong encoding type selected etc.
 
However, I don't know how to bubble the errors up to my Aspx page from the 'handler' page... I can get the error on the imageHandler.aspx page, but no idea how to send it up to the Default.aspx page.
 
Any ideas?
 
Thanx!
Michelle
 
---
 
My code goes like this:
 

Default.aspx
 
protected void Button1_Click(object sender, EventArgs e)
       {
          Image1.ImageUrl = "imageHandler.aspx?c=" + TextBox1.Text + "&enc=" + ddlEncodingTypes.SelectedValue;
       }
 

imageHandler.aspx
 
 public partial class imageHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            BarcodeLib.Barcode b = new BarcodeLib.Barcode();
 
           //get encoding type
            BarcodeLib.TYPE encodingType = BarcodeLib.TYPE.CODE128;            
            switch (Request.QueryString.Get("enc"))
            {
                case "Code 39": { encodingType = BarcodeLib.TYPE.CODE39; } break;
                case "Code 39 Extended": { encodingType = BarcodeLib.TYPE.CODE39Extended; } break;
                case "Code 128": { encodingType = BarcodeLib.TYPE.CODE128; } break;
                case "Code 128-A": { encodingType = BarcodeLib.TYPE.CODE128A; } break;
                case "Code 128-B": { encodingType = BarcodeLib.TYPE.CODE128B; } break;
                case "Code 128-C": { encodingType = BarcodeLib.TYPE.CODE128C; } break;
            }
 
            System.Drawing.Image img = null;
            try
            {
                //gets the barcode image
               img = b.Encode(encodingType, Request.QueryString.Get("c"));
            }
            catch (Exception ee)
            {
                throw;
            }
            //converts the array to send back
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] bytes = ms.ToArray();
            TransmitBytes(bytes, "barcode.jpg");
        }
 
        private void TransmitBytes(byte[] bytes, string outFileName)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + outFileName);
            Response.AddHeader("Content-Length", bytes.Length.ToString());
            Response.ContentType = "image/jpeg";
            Response.BinaryWrite(bytes);
            Response.End();
        }
    }
 
The place where the errors get caught in the handler:
 
try
           {
               //gets the barcode image
              img = b.Encode(encodingType, Request.QueryString.Get("c"));
           }
           catch (Exception ee)
           {
               throw;
           }
Posted 6-Aug-12 23:08pm
Edited 7-Aug-12 3:22am
RyanDev129.8K
v2
Comments
Sergey Alexandrovich Kryukov at 7-Aug-12 12:36pm
   
You really need to read about exception handling, because right now you have no clue. You even talk about "errors", not "exceptions", which is not appropriate. You even say "return". Exceptions are not returned. I would have trouble to help you before you lean this topic. Even is someone decides to show you right way, it want help you. This is one of rare cases where you need some education. Using exceptions is much easier than all the obsolete techniques with "return" of "error status" or something else.
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question: you really need to learn how exception work. They have nothing to do with your understanding of "errors". Please don't consider my answer as an attempt to resolve your problem: it is not possible before you learn things properly.
 
But, to start with: this
try {
    img = b.Encode(encodingType, Request.QueryString.Get("c"));
} catch (Exception ee) {
    throw;
}
is strictly equivalent to
img = b.Encode(encodingType, Request.QueryString.Get("c"));
 
It's totally pointless to catch exception and throw it again. Besides, this code gives you a compiler warning, because ee is not used. You should not do this, ever, but you should not also block propagation of exception by not re-throwing it or throwing another exception. As a rule of thumb, the best way is not to catch exceptions at all, except the very top of stack of each thread. When exception is thrown, let it go until it jumps up the stack to the place where you know how to handle it specifically.
 
Exceptions is a time machine: you mark a place on a stack, and let exceptions go. If an exception is thrown, the execution will ignore the piece of stack between "try" (not by code structure but by time) and will jump back to the try-catch content, as if nothing happened. If you still did not get the idea (most likely you did not, but it would be OK), you need to start from the very basics. You will never sorry about the time spent, trust me.
 
—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 7 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100