Solved

aspx.vb "Object variable or With block variable not set"

Posted on 2014-04-21
6
1,666 Views
Last Modified: 2014-04-21
Hi,

Can someone help me with my aspx.vb code?  at line 24 of he code below?


System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object variable or With block variable not set.
  Source=Microsoft.VisualBasic
  StackTrace:
       at Microsoft.VisualBasic.CompilerServices.Symbols.Container..ctor(Object Instance)
       at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
       at _Default.Button1_Click(Object sender, EventArgs e) in D:\Reports\Default.aspx.vb:line 24
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  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

Open in new window

0
Comment
Question by:patelbg2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 34

Expert Comment

by:ste5an
ID: 40012478
It's line 23: If (ftxt.HasFile) Then

You've declared ftxt, but not assigned an object to it with the method HasFile.
0
 
LVL 6

Author Comment

by:patelbg2001
ID: 40012510
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.PostedFile) within Button1_Click?

then

Sub SaveFile(ByVal file As HttpPostedFile)
0
 
LVL 34

Assisted Solution

by:ste5an
ste5an earned 500 total points
ID: 40012564
No, line 16 and 17 is only your method declaration. As it is an event, you'll get two parameters here. By default, sender is the button which the click comes from and e contains the default information about that click event, which is pretty useless for most of all buttons.

When I need to guess: ftxt should refer to a FileUpload object. So you should have placed one on your form. Use its name in your code and remove the redeclartion of ftxt in line 19.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 40012568
[Deleted Post]
Never mind, just a duplicate of what is already being said...
0
 
LVL 6

Author Comment

by:patelbg2001
ID: 40012666
so if  I have this line in my front page code

<asp:FileUpload ID="FileUploadControl" Name="ftxt" runat="server" />

Open in new window


I dont need to declare in the aspx.vb file?
0
 
LVL 34

Accepted Solution

by:
ste5an earned 500 total points
ID: 40012694
Just remove the , ftxt in line 19 from Dim fname, ftxt
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question