FormView Inserting event with FileUploader control

Discussion in 'ASP .Net' started by RosH, May 21, 2007.

  1. RosH

    RosH Guest

    Dear All,

    Please find below an Inserting Event code expected to trigger just
    before the insert event of a FormView control. Everything works just
    fine but the event throws an exception that the parameter Filename is
    null. As you can see in the code that I have tried to update the
    UploadedFileName textbox which is bound to the Filename column of the
    database connection. During execution of the command, it is not found
    to update the UploadedFileName textbox's text property even though I
    have explicitly declared the textbox variable "MyUploadedFileName".
    Please help me out.

    Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e
    As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles
    FormView1.ItemInserting

    Dim MyUploadedFileName As TextBox =
    FormView1.FindControl("UploadedFileName")

    Dim path As String = Server.MapPath("~/UploadedImages/")
    Dim fileOK As Boolean = False
    If FileUpload1.HasFile Then
    Dim fileExtension As String
    fileExtension = System.IO.Path. _
    GetExtension(FileUpload1.FileName).ToLower()
    Dim allowedExtensions As String() = _
    {".jpg", ".jpeg", ".png", ".gif"}
    For i As Integer = 0 To allowedExtensions.Length - 1
    If fileExtension = allowedExtensions(i) Then
    fileOK = True
    End If
    Next
    If fileOK Then
    Try
    FileUpload1.PostedFile.SaveAs(path & _
    FileUpload1.FileName)
    MyUploadedFileName.Text =
    FileUpload1.FileName.ToString()
    Catch ex As Exception
    Label1.Text = "File could not be uploaded."
    End Try
    Else
    Label1.Text = "Cannot accept files of this type."
    End If
    End If
    End If
    End Sub
     
    RosH, May 21, 2007
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.