Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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

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



imageHandler.aspx

C#
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:

C#
try
           {
               //gets the barcode image
              img = b.Encode(encodingType, Request.QueryString.Get("c"));
           }
           catch (Exception ee)
           {
               throw;
           }
Posted
Updated 7-Aug-12 2:22am
v2
Comments
Sergey Alexandrovich Kryukov 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

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
C#
try {
    img = b.Encode(encodingType, Request.QueryString.Get("c"));
} catch (Exception ee) {
    throw;
}

is strictly equivalent to
C#
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
 
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