0

I am trying to upload an image to a SQL database using C#. I found some code from another site that worked perfectly in a test project. But when I copied and pasted the code and put it into my actual project, it gives me an 'Object reference not set to an instance of an object' error. This makes no sense to me because it worked just fine in the test project. The code is as follows...

protected void btnAddDevice_Click(object sender, EventArgs e)
{
    byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
    HttpPostedFile Image = FileUpload1.PostedFile;
    Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);
    SqlConnection myConnection = new SqlConnection(cnString);
    SqlCommand storeimage = new SqlCommand("insert into ImageTable (img,description,width,height) values(" + "@image,'Description',@imagesize,@imageheight)", myConnection);
    storeimage.Parameters.Add("@image", SqlDbType.Image, myimage.Length).Value = myimage;
    System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
    storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value = img.Width;
    storeimage.Parameters.Add("@imageheight", SqlDbType.BigInt, 99999).Value = img.Height;
    myConnection.Open();
    storeimage.ExecuteNonQuery();
    myConnection.Close();
}

I'm getting the error on the first line inside the method when the byte is being set. I've looked everywhere and tried many different things, but I have no idea why I'm getting this error. Any help would be much appreciated.

Donut
  • 110,061
  • 20
  • 134
  • 146

2 Answers2

0

Put a breakpoint on the first line, and look at the variables involved. One will be null when your expecting a value.

Most likely FileUpload1.PostedFile is null. Insure that a file has been specified before trying to read its bytes.

asawyer
  • 17,642
  • 8
  • 59
  • 87
  • Second that check for if (FileUpload1.PostedFile != null) before executing the remaining code. – Phill Jun 15 '11 at 14:34
  • Ok, I have the breakpoint set and it's stopped on the breakpoint, now how do I see/know which variable is null? – thebetteranonymous Jun 15 '11 at 14:44
  • Look in your local's window, or add it to the Watch window, or mouse over the values. I'm partial to the Immediate window myself, "? FileUpload.PostedFile" – asawyer Jun 15 '11 at 14:47
  • Ok,I got the error to go away by adding an If statement. I wish I would've known to do that forever ago. But why did I not need to do that in my test application? – thebetteranonymous Jun 15 '11 at 14:59
  • I imagine you never tested just clicking upload without specifying a file before. – asawyer Jun 15 '11 at 15:01
  • But I have a file specified. I made sure there was a file in there. And now when there's a file in there, it doesn't get uploaded to the database because it for some reason never gets inside the if statement. – thebetteranonymous Jun 15 '11 at 15:04
  • Check your aspx page. Do you have more then 1 file upload control? Maybe your looking at the wrong one here? – asawyer Jun 15 '11 at 15:14
  • Nah, there's only one. And it must be getting into the if statement because I have an else if saying that if(FileUpload1.PostedFile == null) then set the text of a label. But that label text doesn't change so it's doing something. It's just not updating the database. – thebetteranonymous Jun 15 '11 at 15:27
  • Put a break point at the top and step through each line of code. Without it in front of me, or more code posted I dont know what else to tell you. – asawyer Jun 15 '11 at 15:28
0

Possible issue:

You are reading from HttpPostedFile.InputStream twice without resetting the position.

leppie
  • 115,091
  • 17
  • 196
  • 297