473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Object variable or With block variable not set

I am trying to deal with an image in code-behind. I consistently get the
following error:

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:

An unhandled exception was generated during the execution of the current
web request. Information regarding the origin and location of the
exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object variable or With block variable not set.]
Microsoft.VisualBasic.CompilerServices.LateBinding .LateGet(Object o,
Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack) +934
TeamMetz.Add30_Preview(Object sender, EventArgs e) +73
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292
Version Information: Microsoft .NET Framework Version:1.1.4322.2300;
ASP.NET Version:1.1.4322.2300

What is happening is that I try to catch the image using the following:

Private Sub Add30_Preview(ByVal sender As Object, ByVal e As EventArgs)
Dim imgContent as Object = request.form("ImageUpLoad")
Dim imgStream as Stream
imgStream = imgContent.PostedFile.InputStream
It is clear that the error occurs at the last two lines, because if I
comment them out, everything goes fine (except that the images cannot be
"handled", and therefore do not come up in the preview).

TIA.
...Geshel
--
************************************************** *********************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
************************************************** *********************
* “I contend that we are both atheists. I just believe in one fewer *
* god than you do. When you understand why you dismiss all the other *
* possible gods, you will understand why I dismiss yours.” *
* - Stephen F. Roberts *
************************************************** *********************
* “Anyone who believes in Intelligent Design (“creationism”) is just *
* as ignorant, irrational and ill-educated as someone who believes *
* that the world is a flat disc, that the Sun circles the Earth or *
* that there really is a tooth fairy. Darwinism has an overwhelming *
* foundation of evidence that can be tested and reproduced. *
* *
* “Intelligent Design, on the other hand, has no evidence at all;not *
* one single shred of testable proof. As such, Intelligent Design is *
* Religious Mythology, and has no right whatsoever to be in our *
* Science classrooms.” - 99.99+% of Scientists *
************************************************** *********************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.” Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
************************************************** *********************
Mar 21 '06 #1
6 4207
Neo,

This is a class where is written "must inherit" so it cannot used as this,
beside that you have to instance a class as new. Would you not have to
overlook your code what was the base for this.

Cor

"Neo Geshel" <go****@geshel.org> schreef in bericht
news:ei*************@TK2MSFTNGP12.phx.gbl...
I am trying to deal with an image in code-behind. I consistently get the
following error:

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:

An unhandled exception was generated during the execution of the current
web request. Information regarding the origin and location of the
exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object variable or With block variable not set.]
Microsoft.VisualBasic.CompilerServices.LateBinding .LateGet(Object o,
Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack) +934
TeamMetz.Add30_Preview(Object sender, EventArgs e) +73
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292
Version Information: Microsoft .NET Framework Version:1.1.4322.2300;
ASP.NET Version:1.1.4322.2300

What is happening is that I try to catch the image using the following:

Private Sub Add30_Preview(ByVal sender As Object, ByVal e As EventArgs)
Dim imgContent as Object = request.form("ImageUpLoad")
Dim imgStream as Stream
imgStream = imgContent.PostedFile.InputStream
It is clear that the error occurs at the last two lines, because if I
comment them out, everything goes fine (except that the images cannot be
"handled", and therefore do not come up in the preview).

TIA.
....Geshel
--
************************************************** *********************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
************************************************** *********************
* "I contend that we are both atheists. I just believe in one fewer *
* god than you do. When you understand why you dismiss all the other *
* possible gods, you will understand why I dismiss yours." *
* - Stephen F. Roberts *
************************************************** *********************
* "Anyone who believes in Intelligent Design ("creationism") is just *
* as ignorant, irrational and ill-educated as someone who believes *
* that the world is a flat disc, that the Sun circles the Earth or *
* that there really is a tooth fairy. Darwinism has an overwhelming *
* foundation of evidence that can be tested and reproduced. *
* *
* "Intelligent Design, on the other hand, has no evidence at all; not *
* one single shred of testable proof. As such, Intelligent Design is *
* Religious Mythology, and has no right whatsoever to be in our *
* Science classrooms." - 99.99+% of Scientists *
************************************************** *********************
Mignon McLaughlin once said that "A nymphomaniac is a woman [who is] as
obsessed with sex as the average man." Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
************************************************** *********************
Mar 22 '06 #2
Hi,
Private Sub Add30_Preview(ByVal sender As Object, ByVal e As EventArgs) 1. >> Dim imgContent as Object = request.form("ImageUpLoad")
2. >> Dim imgStream as Stream
3. >> imgStream = imgContent.PostedFile.InputStream
It is clear that the error occurs at the last two lines, because if I
comment them out, everything goes fine (except that the images cannot be
"handled", and therefore do not come up in the preview)
IMHO, It is the first line and not the last 2 lines that have the
problem.
The HttpPostedFile.InputStream method *does* return a System.IO.Stream
object.

So, the problem in my view is how you are constructing your
HttpPostedFile object. Assuming that "request" is an HttpRequest
object, then it's Form property would return a NameValueCollection of
all the Form items. That is where you need to pay attention.

Cor wrote :
This is a class where is written "must inherit" so it cannot used as this,
beside that you have to instance a class as new.


Err... Cor, now I'm a little confused. I understand that a class that
is "MustInherit" cannot be instantiated directly, (using the New
keyword), but I think that it can still hold an object of that type.

For instance, is this code wrong ? :-
---------------------------------------------------
Dim MyFile As HttpPostedFile
Dim MyStream As System.IO.Stream

MyFileCollection = Request.Files
MyFile = MyFileCollection(0)

' Initialize the stream.
MyStream = MyFile.InputStream
---------------------------------------------------

Regards,

Cerebrus.

Mar 22 '06 #3
Cerebrus wrote:
Hi,
Private Sub Add30_Preview(ByVal sender As Object, ByVal e As EventArgs) 1. >> Dim imgContent as Object = request.form("ImageUpLoad")
2. >> Dim imgStream as Stream
3. >> imgStream = imgContent.PostedFile.InputStream
It is clear that the error occurs at the last two lines, because if I
comment them out, everything goes fine (except that the images cannotbe
"handled", and therefore do not come up in the preview)
IMHO, It is the first line and not the last 2 lines that have the
problem.
The HttpPostedFile.InputStream method *does* return a System.IO.Stream
object.

So, the problem in my view is how you are constructing your
HttpPostedFile object. Assuming that "request" is an HttpRequest
object, then it's Form property would return a NameValueCollection of
all the Form items. That is where you need to pay attention.

Cor wrote :
This is a class where is written "must inherit" so it cannot used as this,
beside that you have to instance a class as new.


Err... Cor, now I'm a little confused. I understand that a class that
is "MustInherit" cannot be instantiated directly, (using the New
keyword), but I think that it can still hold an object of that type.

For instance, is this code wrong ? :-
---------------------------------------------------
Dim MyFile As HttpPostedFile
Dim MyStream As System.IO.Stream

MyFileCollection = Request.Files
MyFile = MyFileCollection(0)

' Initialize the stream.
MyStream = MyFile.InputStream
---------------------------------------------------

Regards,

Cerebrus.


When I try out your code snippet there, I get the following error:

Compiler Error Message: BC30002: Type 'HttpPostedFile' is not defined.
Source Error:
Line 203: End Sub
Line 204: Private Sub Add30_Preview(ByVal sender As Object, ByVal e As
EventArgs)
Line 205:Dim MyFile As HttpPostedFile
Line 206:Dim imgStream As System.IO.Stream
Line 207:

So it appears that no, the code is not correct.

TIA.
...Geshel
--
************************************************** *********************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
************************************************** *********************
* “I contend that we are both atheists. I just believe in one fewer *
* god than you do. When you understand why you dismiss all the other *
* possible gods, you will understand why I dismiss yours.” *
* - Stephen F. Roberts *
************************************************** *********************
* “Anyone who believes in Intelligent Design (“creationism”) is just *
* as ignorant, irrational and ill-educated as someone who believes *
* that the world is a flat disc, that the Sun circles the Earth or *
* that there really is a tooth fairy. Darwinism has an overwhelming *
* foundation of evidence that can be tested and reproduced. *
* *
* “Intelligent Design, on the other hand, has no evidence at all;not *
* one single shred of testable proof. As such, Intelligent Design is *
* Religious Mythology, and has no right whatsoever to be in our *
* Science classrooms.” - 99.99+% of Scientists *
************************************************** *********************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.” Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
************************************************** *********************
Mar 23 '06 #4
Cerebrus wrote:
For instance, is this code wrong ? :-
---------------------------------------------------
Dim MyFile As HttpPostedFile
Dim MyStream As System.IO.Stream

MyFileCollection = Request.Files
MyFile = MyFileCollection(0)

' Initialize the stream.
MyStream = MyFile.InputStream
---------------------------------------------------

Regards,

Cerebrus.


As well, when I do the following:

Private Sub Add30_Preview(ByVal sender As Object, ByVal e As EventArgs)
If Nothing <> ImageUpLoad.PostedFile Then
Dim imgStream As Stream = ImageUpLoad.PostedFile.InputStream
Dim imgLen As Integer = ImageUpLoad.PostedFile.ContentLength
Dim imgType as String =
ImageUpLoad.PostedFile.ContentType.ToString().ToLo wer()
End If

I get the following error message:

Compiler Error Message: BC30451: Name 'ImageUpLoad' is not declared.
Source Error:
Line 203: End Sub
Line 204: Private Sub Add30_Preview(ByVal sender As Object, ByVal e As
EventArgs)
Line 205: If Nothing <> ImageUpLoad.PostedFile Then
Line 206: Dim imgStream As Stream = ImageUpLoad.PostedFile.InputStream
Line 207: Dim imgLen As Integer = ImageUpLoad.PostedFile.ContentLength
The problem is, ImageUpLoad is properly declared in the code!! It is
properly declared as an HtmlInputFile in the Page_Load in the following
manner:

Dim imgupload As New HtmlInputFile
myForm.Controls.Add(imgupload)
imgupload.ID = "ImageUpLoad"
imgupload.Accept = "image/jpg"
Please Help!
TIA.
...Geshel
--
************************************************** *********************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
************************************************** *********************
* “I contend that we are both atheists. I just believe in one fewer *
* god than you do. When you understand why you dismiss all the other *
* possible gods, you will understand why I dismiss yours.” *
* - Stephen F. Roberts *
************************************************** *********************
* “Anyone who believes in Intelligent Design (“creationism”) is just *
* as ignorant, irrational and ill-educated as someone who believes *
* that the world is a flat disc, that the Sun circles the Earth or *
* that there really is a tooth fairy. Darwinism has an overwhelming *
* foundation of evidence that can be tested and reproduced. *
* *
* “Intelligent Design, on the other hand, has no evidence at all;not *
* one single shred of testable proof. As such, Intelligent Design is *
* Religious Mythology, and has no right whatsoever to be in our *
* Science classrooms.” - 99.99+% of Scientists *
************************************************** *********************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.” Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
************************************************** *********************
Mar 23 '06 #5
Neo wrote :
So it appears that no, the code is not correct.
TIA.
...Geshel
Actually not, Neo, since the code was copied from an MSDN page, so it
*is* correct. HttpPostedFile is not recognized probably since you need
to import the System.Web namespace. When I asked Cor Ligthert if the
code was *wrong*, I didn't mean syntactically wrong, I just meant
Logically wrong.
It is properly declared as an HtmlInputFile in the Page_Load...


There you have a problem ! Declare it outside of any method, so it will
be a global variable. Then you can instantiate it within the Load
method. Since you have declared it within the Page_Load method, it goes
out of context the moment the Page_Load method completes. Therefore,
when you reference it within another method, the compiler will ring the
alarm bells, saying the variable is not declared !

Outside any method :
-------------------------------
Dim imgupload As New HtmlInputFile

Within Page_Load method :
----------------------------------------
imgUpload = New HtmlInputFile ()

Now go ahead and use it in your Add30_Preview method.

HTH,

Regards,

Cerebrus.

Mar 23 '06 #6
BTW, that's one hell of a signature you've got !

Mar 23 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Hung Jung Lu | last post by:
Hi, Does anybody know where this term comes from? "First-class object" means "something passable as an argument in a function call", but I fail to see the connection with "object class" or...
10
by: Michael McCracken | last post by:
Hi, I have a problem with unittest.TestCase that I could really use some help with. I have a class File in module File. The important thing about File for this discussion is that it's simple -...
8
by: Mark Neilson | last post by:
1. What is the best way to make a single instance of my top level class (DLL) internally available to all other members of the assembly? The top level object is where all other access is made in...
6
by: Neo Geshel | last post by:
I am trying to deal with an image in code-behind. I consistently get the following error: Server Error in '/' Application. Object variable or With block variable not set. Description: An...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
3
by: Richard Hollenbeck | last post by:
I've marked the line in this subroutine where I've been getting this error. It may be something stupid but I've been staring at this error trying to fix it for over an hour. I'm pretty sure the...
1
by: abhijmenbumca07 | last post by:
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...
4
by: BrianAdev | last post by:
Why It throw an error of <Object variable or With block variable not set> set objAccess = GetObject(strMdbPath) objAccess.DoCmd.SetWarning False objAccess.DoCmd.OpenReport strReportName, 0 'It...
3
by: Newbie19 | last post by:
I'm trying to get a list of all subfolders in a folder on a share drive, but I keep on getting this error message: Object variable or With block variable not set. Description: An unhandled...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.