Problems with Images and SQL !

O

Ola Myrgart

Hi!

This is the errorcode I get when I try to insert some new data inot my
SQL DB.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.


Line 125: Dim connectionString As String =
"server='localhost'; user id='sa'; password='xxxxx'; Database='xxxxx'"
Line 126: Dim ImageStream As Stream =
FotoFile.PostedFile.InputStream
Line 127: Dim byteData(FotoFile.PostedFile.ContentLength) As
Byte


The problem is on Line 126.

What´s the problem? Can´t find it out myself.
Is there anyone who has any ideas ?

OM "Myggan"
 
H

Hermit Dave

First check to see if the FotoFile is not null

Attaching some code that i use to upload files

HttpFileCollection myFiles = Request.Files;
CodePlayDB myCode = new CodePlayDB();
if(myFiles.Count != 0)
{
try
{
HttpPostedFile myFile = myFiles[0];
if( myFile != null)
{
if(myFile.FileName != "")
{
try
{
int fileLength = myFile.ContentLength;
byte[] fileData = new byte[fileLength];
Stream myStream = myFile.InputStream;
myStream.Read(fileData, 0, fileLength);

string fileContentType = myFile.ContentType;
myCode.AddImageToDB(fileContentType, fileData);
}
catch (Exception ex1)
{
Label1.Text = "Error uploading file '" + myFile.FileName +
"'<br>Error: " + ex1.ToString();
}
}
}
}
catch (Exception ex)
{
Label1.Text = "Error uploading file '" +
fileUpload1.PostedFile.FileName + "'<br>Error: " + ex.ToString();
}
}
else
Label1.Text = "Please enter a valid file";
}
 
O

Ola Myrgart

Hi!

Thank you for your answer. Maybe you have some code in VB.Net for the
same application.

Regards

OM "Myggan"
 
K

Kevin Spencer

The NullReferenceException means that one or more objects you referred to in
the offending line of code are null. In this case, the offending line of
code is:
Dim ImageStream As Stream = FotoFile.PostedFile.InputStream

The left-hand side of the assignment can't be the problem, because all it
does is declare a variable. On the right-hand side, you are referring to
three distinct objects:

FotoFile
FotoFile.PostedFile
FotoFile.PostedFile.InputStream

Since every HttpPostedFile class has an InputStream property, the third can
be eliminated immediately, leaving:

FotoFile
FotoFile.PostedFile

I will assume that "FotoFile" is an HtmlInputFile Control. Assuming that the
Control was not added dynamically, it is unlikely that this is null,
leaving:

FotoFile.PostedFile

Did you check to see whether any file was uploaded using that Control?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
O

Ola Myrgart

Hi !

Thank you for your answer. I think the problem lies within the fact that
I have a label that I use to give the user a opportunity to preview and
change their supplied information before submit. When the label comes up
the Inputfiled goes blank. It seems like viewstate doesn´t survive the
change. So when the user submits ther information the errormessage comes
up.

OM "Myggan"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top