Hi I am trying to upload images and keep getting an error,

The code that I am using:

Protected Sub dvPictureInsert_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dvPictureInsert.ItemInserted
        'If the record was successfully inserted, save the picture
        If e.AffectedRows > 0 Then
            'Determine the maximum PictureID for this user
            Dim results As DataView = CType(maxPictureIDDataSource.Select(DataSourceSelectArguments.Empty), DataView)

            Dim pictureIDJustAdded As Integer = CType(results(0)(0), Integer)

            'Reference the FileUpload control
            Dim imageUpload As FileUpload = CType(dvPictureInsert.FindControl("imageUpload"), FileUpload)

            If imageUpload.HasFile Then

                Dim baseDirectory As String = Server.MapPath("~/UploadedImages/")

                imageUpload.SaveAs(baseDirectory & pictureIDJustAdded & ".jpg")
            End If
        End If
    End Sub

The error that I am receiving:

Object reference not set to an instance of an object

On lines:

Line 26: Dim imageUpload As FileUpload = CType(dvPictureInsert.FindControl("imageUpload"), FileUpload)
Line 27:
Line 28: If imageUpload.HasFile Then
Line 29:
Line 30: Dim baseDirectory As String = Server.MapPath("~/UploadedImages/")

Any help on this would be great.

Recommended Answers

All 3 Replies

hi,

what control u r using.try like this

Dim imageUpload As FileUpload = CType( e.Item.FindControl("imageUpload"), FileUpload)

Thank you!
I had made a silly mistake and dragged a input file control instead of file upload control!whoops!!;)
working fine now!

hi,

if u r probelm is solved mark it as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.