bng0005
asked on
help with exceptions
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.Conte ntLength > 18000)
throw new Exception();
else
mMessage.Attachments.Add(n ew MailAttachment(@filename.P ostedFile. FileName)) ;
}
catch
{
Response.Write("test");
}
Any help is appreciated. Thanks
Bryan
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.Conte
throw new Exception();
else
mMessage.Attachments.Add(n
}
catch
{
Response.Write("test");
}
Any help is appreciated. Thanks
Bryan
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.
ASKER
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");
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
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.
ASKER
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?
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?
Do you want a javascript prompt or a HTML message in the page ?
ASKER
Hmm, probably the prompt
Ok, just before the Redirect try adding:
Response.Write("<script language=""javascript"">al ert('Your message');</script>");
Response.Flush();
Response.Write("<script language=""javascript"">al
Response.Flush();
Scratch that, it won't work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No luck.
HttpException
Cannot redirect after http headers have been sent.
HttpException
Cannot redirect after http headers have been sent.
Thats why I said to scratch the first one. Try the second option.
ASKER
Second option worked perfectly, didn't see it until after I posted. Thanks for the help.
Bryan
Bryan