Solved

help with exceptions

Posted on 2006-07-07
12
270 Views
Last Modified: 2012-03-15
Hey experts,

I'm still trying to teach myself c# and web programming, and I'm stuck on trying to figure out how to get this exception to work.

I have a web form that allows the user to select an attachment to include with the email. I want to put a limit on the file size to 10mb. When the user has entered in the information and selected the attachment, the user then clicks on submit and the email is created and sent out. What I would like to do in theory is if the attachment is too large, notify the user before sending the email without the attachment so he or she can make arrangements to get the attachment to the person in a different way. What I have below doesn't work. I stepped through the code and it goes to the catch statement, but I guess I just don't understand how to use the response.write correctly, as it just continues on with the rest of the code with no message.

try
   {
   if (filename.PostedFile.ContentLength > 18000)
          throw new Exception();
   else
          mMessage.Attachments.Add(new MailAttachment(@filename.PostedFile.FileName));
   }
catch
   {
          Response.Write("test");
   }

Any help is appreciated. Thanks


Bryan
0
Comment
Question by:bng0005
  • 7
  • 5
12 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17060894
Have you let the page finish processing ? It may just be that the Response is being buffered so you won't see the output right away.
0
 
LVL 1

Author Comment

by:bng0005
ID: 17061002
Yes I have, never saw my "test" string anywhere. I'll post the rest of my code after that section, as the redirect might be a reason why it doesn't show. This is the code that immediately follows what I already posted, and the end of the function. Without debugging and adding a file that is too large, I click submit, it goes to the success page and the email is sent without the attachment

try
{
    SmtpMail.Send(mMessage);
}
catch
{
    Response.Write("There was an error sending the notification e-mail. Please contact the System Administrator.");
}

Session.Add("_rn", (counter=="" ? "#ERROR" : counter));
Response.Redirect("Success.aspx");  
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17061052
That'll be it then. Your page will redirect before you see the message. To verify, take out the redirect and see if you get the message as expected.
0
 
LVL 1

Author Comment

by:bng0005
ID: 17061141
ahah, it shows up now.

Now if you don't mind me expanding on the question, is there an easy way to get the message to show up like a dialog box and continue with the redirect after user clicks ok?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17061210
Do you want a javascript prompt or a HTML message in the page ?
0
 
LVL 1

Author Comment

by:bng0005
ID: 17061365
Hmm, probably the prompt
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17061437
Ok, just before the Redirect try adding:

    Response.Write("<script language=""javascript"">alert('Your message');</script>");
    Response.Flush();
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17061459
Scratch that, it won't work.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 300 total points
ID: 17061474
Take out the Redirect then add this instead:

        Response.Write("<script language=\"javascript\">");
        Response.Write("alert('My Message');");
        Response.Write("document.location.href='success.aspx';");
        Response.Write("</script>");

This will show your message and then perform a client-side redirect instead.
0
 
LVL 1

Author Comment

by:bng0005
ID: 17061496
No luck.

HttpException

Cannot redirect after http headers have been sent.

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17061512
Thats why I said to scratch the first one. Try the second option.
0
 
LVL 1

Author Comment

by:bng0005
ID: 17061562
Second option worked perfectly, didn't see it until after I posted. Thanks for the help.


Bryan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now