By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

Object variable or With block variable not set

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
BTW, that's one hell of a signature you've got !

Mar 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.