patelbg2001
asked on
aspx.vb "Object variable or With block variable not set"
Hi,
Can someone help me with my aspx.vb code? at line 24 of he code below?
System.NullReferenceExcept ion was unhandled by user code
HResult=-2147467261
Message=Object variable or With block variable not set.
Source=Microsoft.VisualBasic
StackTrace:
at Microsoft.VisualBasic.Comp ilerServic es.Symbols .Container ..ctor(Obj ect Instance)
at Microsoft.VisualBasic.Comp ilerServic es.NewLate Binding.La teGet(Obje ct Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at _Default.Button1_Click(Obj ect sender, EventArgs e) in D:\Reports\Default.aspx.vb :line 24
at System.Web.UI.WebControls. Button.OnC lick(Event Args e)
at System.Web.UI.WebControls. Button.Rai sePostBack Event(Stri ng eventArgument)
at System.Web.UI.WebControls. Button.Sys tem.Web.UI .IPostBack EventHandl er.RaisePo stBackEven t(String eventArgument)
at System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData)
at System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt)
InnerException:
Can someone help me with my aspx.vb code? at line 24 of he code below?
System.NullReferenceExcept
HResult=-2147467261
Message=Object variable or With block variable not set.
Source=Microsoft.VisualBasic
StackTrace:
at Microsoft.VisualBasic.Comp
at Microsoft.VisualBasic.Comp
at _Default.Button1_Click(Obj
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.Process
InnerException:
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Linq
Imports System.Web
Imports System.Web.UI.Page
Imports System.Web.UI.WebControls
Imports System.Management.Automation
Imports System.Management.Automation.Runspaces
Imports System.IO
Imports System.Text
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim fname, ftxt
Dim fstatus As String
Dim fileOK As Boolean = False
If (ftxt.HasFile) Then
Dim fileExtension As String
fileExtension = System.IO.Path. _
GetExtension(ftxt.FileName).ToLower()
Dim allowedExtensions As String() = _
{".txt"}
For i As Integer = 0 To allowedExtensions.Length - 1
'Dim Filesize As Integer = ftxt.PostedFileContent
If fileExtension = allowedExtensions(i) Then
fileOK = True
End If
Next
If fileOK Then
Try
Dim objFSO, strFolder
objFSO = CreateObject("Scripting.FileSystemObject")
strFolder = objFSO.CreateFolder(Server.MapPath("D:\Reports\uploads\" & fname))
Dim fpath As String = Server.MapPath(strFolder)
Dim fname = Path.GetFileName(ftxt.Filename)
Dim fnameWithoutExt = Path.GetFileNameWithoutExtension(ftxt.FileName)
Dim Ext = Path.GetExtension(ftxt.FileName)
Dim fnametoSave = fnameWithoutExt & "_" & Date.Now.ToString("dd-MM-yyyy") & Ext
ftxt.PostedFile.SaveAs(fpath & ftxt.fnametoSave)
fstatus = "Upload status: File uploaded!"
Catch ex As Exception
fstatus = "Upload status: Only text files are under 100kb are accepted"
End Try
Else
fstatus = "Upload status: Cannot Accept File"
End If
End If
End Sub
End Class
ASKER
How do i assign an object to the hasfile? Sorry confused, as this is my first attempt at aspx.vb
I thought it would be assigned under line 16 & 17?
do I split it out into two subs
SaveFile(FileUpload1.Poste dFile) within Button1_Click?
then
Sub SaveFile(ByVal file As HttpPostedFile)
I thought it would be assigned under line 16 & 17?
do I split it out into two subs
SaveFile(FileUpload1.Poste
then
Sub SaveFile(ByVal file As HttpPostedFile)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
[Deleted Post]
Never mind, just a duplicate of what is already being said...
Never mind, just a duplicate of what is already being said...
ASKER
so if I have this line in my front page code
I dont need to declare in the aspx.vb file?
<asp:FileUpload ID="FileUploadControl" Name="ftxt" runat="server" />
I dont need to declare in the aspx.vb file?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You've declared ftxt, but not assigned an object to it with the method HasFile.