This is my first try at uploading files command. I get this error in the web page when I hit the upload button. What am I doing wrong.
Server Error in '/' Application.
Object variable or With block variable not set.
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 variable or With block variable not set.
Source Error:
Line 18:
Line 19: Protected Sub UploadButton_Click(sender As Object, e As EventArgs) Handles UploadButton.Click
Line 20: FileName.InnerHtml = FileField.PostedFile.FileName
Line 21: FileContent.InnerHtml = FileField.PostedFile.ContentType
Line 22: FileSize.InnerHtml = FileField.PostedFile.Contentlength
Source File: C:\Users\Trevor\Documents\Visual Studio 2013\WebSites\WebSite4\Default2.aspx.vb Line: 20
Stack Trace:
[NullReferenceException: Object variable or With block variable not set.]
Microsoft.VisualBasic.CompilerServices.Container..ctor(Object Instance) +1308864
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack) +191
Default2.UploadButton_Click(Object sender, EventArgs e) in C:\Users\Trevor\Documents\Visual Studio 2013\WebSites\WebSite4\Default2.aspx.vb:20
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9614758
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.33440
The code I have for the command.
Partial Class Default2
Inherits System.Web.UI.Page
Dim FileField As Object
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Uppath As String
Dim Upname As String
Uppath = "C:\Users\Trevor\Desktop\File Uplaod\UploadedFiles"
Upname = Dir(Uppath, vbDirectory)
If (Upname = "") Then
MkDir("C:\Users\Trevor\Desktop\File Uplaod\UploadedFiles")
End If
End Sub
Protected Sub UploadButton_Click(sender As Object, e As EventArgs) Handles UploadButton.Click
FileName.InnerHtml = FileField.PostedFile.FileName
FileContent.InnerHtml = FileField.PostedFile.ContentType
FileSize.InnerHtml = FileField.PostedFile.Contentlength
UploadDetails.Visible = True
Dim myfilename As String
myfilename = FileField.PostedFile.FileName()
Dim c As String = System.IO.Path.GetFileName(myfilename)
Try
FileField.PostedFile.SaveAs("C:\Users\Trevor\Desktop\File Uplaod\UploadedFiles" + c)
Span1.InnerHtml = "File Upload Successful."
Catch ex As Exception
Span1.InnerHtml = "File Upload FAILED."
UploadDetails.Visible = False
End Try
End Sub
End Class
This could be the result of a typo as the name of your field is currently "FileFeild" and it should be "FileField" based on the code that you have provided.
You are currently encountering a null reference exception which means that you are attempting to access a property or field of a null object (likely your file). I would recommend performing the necessary null checking to ensure that your File in
fact exists (through the
HasFile method) and then add the appropriate logic within that area :
Protected Sub UploadButton_Click(sender As Object, e As EventArgs) Handles UploadButton.Click
'Ensure that a File was present within the FileField'
If FileField.HasFile Then
'Grab the appropriate fields from your File'
FileName.InnerHtml = FileField.PostedFile.FileName
FileContent.InnerHtml = FileField.PostedFile.ContentType
FileSize.InnerHtml = FileField.PostedFile.ContentLength
End If
'Set your Details area to Visible'
UploadDetails.Visible = True
'Grab the path for your File'
Dim c As String = System.IO.Path.GetFileName(FileField.PostedFile.FileName)
Try
'Attempt to save the file'
FileField.PostedFile.SaveAs(Path.Combine("C:\Users\Trevor\Desktop\File Uplaod\UploadedFiles",c))
Span1.InnerHtml = "File Upload Successful."
Catch ex As Exception
Span1.InnerHtml = "File Upload FAILED."
UploadDetails.Visible = False
End Try
End Sub
You might want to consider placing a breakpoint within your Button Click event so that you can step through your code line-by-line and determine where exactly the error is occurring.
Member
3 Points
7 Posts
Uploading Files.
Jan 06, 2014 03:10 PM|TrevorReimer|LINK
This is my first try at uploading files command. I get this error in the web page when I hit the upload button. What am I doing wrong.
Server Error in '/' Application.
Object variable or With block variable not set.
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 variable or With block variable not set.
Source Error:
Line 18: Line 19: Protected Sub UploadButton_Click(sender As Object, e As EventArgs) Handles UploadButton.Click Line 20: FileName.InnerHtml = FileField.PostedFile.FileName Line 21: FileContent.InnerHtml = FileField.PostedFile.ContentType Line 22: FileSize.InnerHtml = FileField.PostedFile.Contentlength
Source File: C:\Users\Trevor\Documents\Visual Studio 2013\WebSites\WebSite4\Default2.aspx.vb Line: 20
Stack Trace:
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.33440
The code I have for the command.
upload
All-Star
114593 Points
18503 Posts
MVP
Re: Uploading Files.
Jan 06, 2014 03:19 PM|Rion Williams|LINK
This could be the result of a typo as the name of your field is currently "FileFeild" and it should be "FileField" based on the code that you have provided.
You are currently encountering a null reference exception which means that you are attempting to access a property or field of a null object (likely your file). I would recommend performing the necessary null checking to ensure that your File in fact exists (through the HasFile method) and then add the appropriate logic within that area :
You might want to consider placing a breakpoint within your Button Click event so that you can step through your code line-by-line and determine where exactly the error is occurring.
upload