Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTMLInputFile giving "object reference not set to instance of object" 1

Status
Not open for further replies.

lanm

Programmer
Jul 7, 2005
244
US
The subroutine getting called is:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Call Upload()
Dim FileInput As HtmlInputFile

If FileInput.PostedFile Is "" Then
Label2.Text = "No File specified"
Else
Try
Dim ServerFileName As String
ServerFileName = Path.GetFileName(FileInput.PostedFile.FileName)
FileInput.PostedFile.SaveAs("C:\TestSaving\" & ServerFileName)
Label2.Text = "File " & ServerFileName & " uploaded successfully."
Catch ex As Exception
Label2.Text = ex.Message
End Try
End If
End Sub

 
The error happens @:

If FileInput.PostedFile is "" Then

**************************

...I've also tried the following code:

I've got:
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents myfile As System.Web.UI.HtmlControls.HtmlInputFile

.....the routine is:

Dim getfile As HtmlInputFile
If IsNothing(getfile.PostedFile) Then Label2.Text = "please select a file to opload" : Exit Sub
If getfile.PostedFile.ContentLength = 0 Then Label2.Text = "cannot upload zero length file" : Exit Sub
Dim myFile As HttpPostedFile = getfile.PostedFile
'myFile.SaveAs(Server.MapPath("." & "\upload\template.xls"))
Dim ServerFileName As String = Path.GetFileName(getfile.PostedFile.FileName)
myFile.SaveAs("C:\TestSaving\" & ServerFileName)

I get the object reference error on the line for:
"If IsNothing(getfile.PostedFile) Then Label2.Text = "please select a file to opload......"
 
K...just did:
Dim FileInput As New HtmlInputFile
If FileInput.PostedFile Is "" Then
Label2.Text = "No File specified"
Else
Try
Dim ServerFileName As String
ServerFileName = Path.GetFileName(FileInput.PostedFile.FileName)
FileInput.PostedFile.SaveAs("C:\TestSaving\" & ServerFileName)
Label2.Text = "File " & ServerFileName & " uploaded successfully."
Catch ex As Exception
Label2.Text = ex.Message
End Try
End If


...and this time, I got the label2.Text to show me the message of "Object reference not set to an instance of an object." Before, it was basically the "Run Time" page error.
 
Try using this

If Not (FileInput.PostedFile Is Nothing) Then
..
End IF
 
Hmmmm....just did:
Dim FileInput As New HtmlInputFile
If Not (FileInput.PostedFile Is Nothing) Then
Try
Dim ServerFileName As String
ServerFileName = Path.GetFileName(FileInput.PostedFile.FileName)
FileInput.PostedFile.SaveAs("C:\TestSaving\" & ServerFileName)
Label2.Text = "File " & ServerFileName & " uploaded successfully."
Catch ex As Exception
Label2.Text = ex.Message
End Try
Else
Label2.Text = "No File specified"
End If

..and got "No File Specified" even though I selected a .doc file for testing.
 
Finally got this to work:

Dim getmyFile As HttpPostedFile = myfile.PostedFile
Label2.Text = "Line 76"
If IsNothing(getmyFile) Then
Label2.Text = "Please select a file to upload"
Label2.Text = "Line 78"
Else
If getmyFile.ContentLength = 0 Then
Label2.Text = "Cannot upload zero length File"
Else
Dim ServerFileName As String = Path.GetFileName(myfile.PostedFile.FileName)
getmyFile.SaveAs("C:\TestSaving\" & ServerFileName)
Label2.Text = "Successful upload to C:\TestSaving\" & ServerFileName
End If
End If
 
if u r referring to a control in code behind then have to delcare it likeso:
protected WithEvents ControlName as ControlType

so this would work:
Protected WithEvents myFile As HttpPostedFile

Known is handfull, Unknown is worldfull
 
I have:

Protected WithEvents myfile as System.Web.UI.HTMLControls.HTMLInputFile

Thanks!
 
it works???

Known is handfull, Unknown is worldfull
 
Yes, I was finally able to get it to go this morning after working on it all day yesterday.
 
cheers ;)...

Known is handfull, Unknown is worldfull
 
Just as a bit of a side note, rather that just check if the posted file is nothing, I would suggest a few more checks such as the minumum and maximum file size. e.g.
Code:
    Public Function FileFieldSelected(ByVal FileField As System.Web.UI.HtmlControls.HtmlInputFile) As Boolean
        If FileField.PostedFile Is Nothing Then Return False
        If FileField.PostedFile.ContentLength = 0 Then Return False
        If FileField.PostedFile.ContentLength > 5120000 Then Return False
        Return True
    End Function


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
The reason why u get that error is because you need to set the HTML File Input control to "run as server control";

if you r using vs .net right click and check and check "run as server control".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top