FileUpload(attachment) error when FileUpload fields are not visible

Discussion in 'ASP.NET / ASP.NET Core' started by vandiermen, Jan 25, 2007.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. If fixed the problem with this code:

    If not AttachmentBlank1.PostedFile Is Nothing Then
    mm.Attachments.Add(New Attachment(AttachmentBlank1.PostedFile.InputStream, AttachmentBlank1.FileName))
    End if

    If not AttachmentBlank2.PostedFile Is Nothing Then
    mm.Attachments.Add(New Attachment(AttachmentBlank2.PostedFile.InputStream, AttachmentBlank2.FileName))
    End if
     
  2. The problem seems to come up when a FileUpload(attachment) field is not visible in the document on submit, I have been making a draft andchoose to only have some file upload fields visible depending on what the user has chosen to upload, Like in this form that?s not working anymore http://www.budgetwebdesign.com.au/msresume/SuperStarter.aspx
    This use to work fine and it just changed (and I don?t know why) and similar pages with this functionality work fine. These pages use to get sent fine whether the file upload fields were visible or not

    Heres my error or you may see it on the above link, my VB and HTML code is under this:
    Object reference not set to an instance of an object.


    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:




    Code:
    Line 315:
    Line 316:        'Attach the file
    Line 317:        mm.Attachments.Add(New Attachment(
    AttachmentBlank2.PostedFile.InputStream, AttachmentBlank2.FileName))
    Line 318:

    Source File: D:\websites\207.97.194.114\msresume.com.au\SuperStarter.aspx.vb Line: 317

    Stack Trace:





    Code:
    [NullReferenceException: Object reference not set to an instance of an object.]   
    _Default.SendEmail_Click(Object sender, EventArgs e) in D:\websites\207.97.194.114\
    msresume.com.au\SuperStarter.aspx.vb:317   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
     +105   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107  
     System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
    (String eventArgument) +7   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler 
    sourceControl, String eventArgument) +11   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection 
    postData) +33   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean 
    includeStagesAfterAsyncPoint) +5102

    __________________________________________________________________________________________

    VB CODE:



    Imports System.Net.Mail<O:p></O:p>
    Imports System.Text<O:p></O:p>
    <O:p></O:p>
    Partial Class _Default<O:p></O:p>
    Inherits System.Web.UI.Page<O:p></O:p>
    <O:p></O:p>
    Protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click<O:p></O:p>
    '!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS<O:p></O:p>
    Const ToAddress As String = "[email protected]"<O:p></O:p>
    <O:p></O:p>
    '(1) Create the MailMessage instance<O:p></O:p>
    Dim mm As New MailMessage(UsersEmail.Text, ToAddress)<O:p></O:p>
    <O:p></O:p>
    Dim sb As StringBuilder = New StringBuilder()<O:p></O:p>
    <O:p></O:p>
    sb.Append("Name: " &amp; Name.Text &amp; "")<O:p></O:p>
    sb.Append("Phone Number: " &amp; Phone.Text &amp; "")<O:p></O:p>
    sb.Append("Mobile: " &amp; Mobile.Text &amp; "")<O:p></O:p>
    sb.Append("Work Number: " &amp; TextBox18.Text &amp; "")<O:p></O:p>
    sb.Append("Fax Number: " &amp; TextBox19.Text &amp; "")<O:p></O:p>
    sb.Append("Email Address: " &amp; UsersEmail.Text &amp; "" &amp; "")<O:p></O:p>
    <O:p></O:p>
    sb.Append("Address: " &amp; "" &amp; AddressLine1.Text &amp; "")<O:p></O:p>
    sb.Append(AddressLine2.Text &amp; "")<O:p></O:p>
    sb.Append(AddressLine3.Text &amp; "")<O:p></O:p>
    sb.Append("Postcode: " &amp; Postcode.Text &amp; "")<O:p></O:p>
    sb.Append("Country: " &amp; TextBox5.Text &amp; "")<O:p></O:p>
    sb.Append("Address Type: " &amp; DropDownList3.Text &amp; "" &amp; "")<O:p></O:p>
    <O:p></O:p>
    '(2) Assign the MailMessage's properties<O:p></O:p>
    mm.Subject = "Message from form"<O:p></O:p>
    mm.Body = sb.ToString()<O:p></O:p>
    mm.IsBodyHtml = True<O:p></O:p>
    <O:p></O:p>
    'Attach the file<O:p></O:p>
    mm.Attachments.Add(New Attachment(AttachmentBlank1.PostedFile.InputStream, AttachmentBlank1.FileName))<O:p></O:p>
    <O:p></O:p>
    'Attach the file<O:p></O:p>
    mm.Attachments.Add(New Attachment(AttachmentBlank2.PostedFile.InputStream, AttachmentBlank2.FileName))<O:p></O:p>
    <O:p></O:p>
    <O:p></O:p>
    <O:p></O:p>
    '(3) Create the SmtpClient object<O:p></O:p>
    Dim smtp As New SmtpClient<O:p></O:p>
    <O:p></O:p>
    '(4) Send the MailMessage (will use the Web.config settings)<O:p></O:p>
    smtp.Send(mm)<O:p></O:p>
    <O:p></O:p>
    'Show the EmailSentForm Panel and hide the EmailForm Panel<O:p></O:p>
    EmailSentForm.Visible = True<O:p></O:p>
    CoverLetterForm.Visible = False<O:p></O:p>
    NamePanel.Visible = False<O:p></O:p>
    ResumeForm.Visible = False<O:p></O:p>
    SubmitPanel.Visible = False<O:p></O:p>
    HeadPanel.Visible = False<O:p></O:p>
    <O:p></O:p>
    End Sub<O:p></O:p>
    <O:p></O:p>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load<O:p></O:p>
    If Not Page.IsPostBack Then<O:p></O:p>
    'On the first page load, hide the EmailSentForm Panel<O:p></O:p>
    EmailSentForm.Visible = False<O:p></O:p>
    End If<O:p></O:p>
    End Sub<O:p></O:p>
    <O:p></O:p>
    Protected Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged<O:p></O:p>
    PanelExp2.Visible = True<O:p></O:p>
    CheckBox2.Visible = False<O:p></O:p>
    End Sub<O:p></O:p>
    <O:p></O:p>
    Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged<O:p></O:p>
    AttachmentBlank1.Visible = True<O:p></O:p>
    LabelR.Visible = True<O:p></O:p>
    NamePanel.Visible = True<O:p></O:p>
    ResumeForm.Visible = True<O:p></O:p>
    SubmitPanel.Visible = True<O:p></O:p>
    End Sub<O:p></O:p>
    <O:p></O:p>
    Protected Sub CheckBox11_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox11.CheckedChanged<O:p></O:p>
    NamePanel.Visible = True<O:p></O:p>
    CoverLetterForm.Visible = True<O:p></O:p>
    SubmitPanel.Visible = True<O:p></O:p>
    Labelx.Visible = True<O:p></O:p>
    AttachmentBlank2.Visible = True<O:p></O:p>
    End Sub<O:p></O:p>
    <O:p></O:p>
    End Class<O:p></O:p>


    ______________________________________________________________________________________


    HTML CODE:



    <%@ Page Language="VB" AutoEventWireup="true"<O:p></O:p>
    CodeFile="SuperStarter.aspx.vb" Inherits="_Default" Title="My Super Résumé" %><O:p></O:p>
    <%@ Import Namespace="System.Net.Mail" %><O:p></O:p>
    <%@ Import Namespace="System.Text" %><O:p></O:p>
    <O:p></O:p>
    <html xmlns="http://www.w3.org/1999/xhtml" ><O:p></O:p>
    <O:p></O:p>
    <head id="Head1" runat="server"><O:p></O:p>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><O:p></O:p>
    <O:p></O:p>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><O:p></O:p>
    <O:p></O:p>
    <script runat="server"><O:p></O:p>
    <O:p></O:p>
    </script>
    </head><O:p></O:p>
    <O:p></O:p>
    <form id="form1" runat="server"><O:p></O:p>
    <O:p></O:p>







    <O:p></O:p>
    <asp:Label ID="LabelR" runat="server" Font-Size="10pt" ForeColor="#666666" Text="Attach your Resume:"<O:p></O:p>
    Visible="False"></asp:Label></span></td><O:p></O:p>
    <td><O:p></O:p>
    <asp:FileUpload ID="AttachmentBlank1" runat="server" Width="225px" Visible="False" /></td><O:p></O:p>
    </tr><O:p></O:p>
    <tr><O:p></O:p>
    <td><O:p></O:p>
    <span style="font-size: 10pt; color: #666666"><O:p></O:p>
    <asp:Label ID="Labelx" runat="server" Text="Attach Cover Letter:"<O:p></O:p>
    Visible="False" ForeColor="#666666" font-size="10pt"></asp:Label></span></td><O:p></O:p>
    <td><O:p></O:p>
    <asp:FileUpload ID="AttachmentBlank2" runat="server" Width="225px" Visible="False" /></td><O:p></O:p>
    </tr><O:p></O:p>
    <O:p></O:p>
    </td><O:p></O:p>
    </tr><O:p></O:p>
    </table><O:p></O:p>
    </div><O:p></O:p>
    <O:p></O:p>
    </div><O:p></O:p>
    </form><O:p></O:p>
    </body><O:p></O:p>
    </html>
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page