0

I am trying to insert an image into database with its properties and a couple of textboxes. Here is the HTML code:

          <span class="label"><label for="FirstName">First Name: </label></span>
          <asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
          </div>
           <div class="row">
          <span class="label"><label for="Surname">Surname: </label></span>
          <asp:TextBox ID="Surname" runat="server"></asp:TextBox>
          </div>
          <div class="row">
          <span class="label"><label for="PhotoUpload">Photo: </label></span>
          <asp:FileUpload ID="PhotoUpload" runat="server" />
          </div>
          <div class="row">
          <span class="label">&nbsp;</span>
          <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />

The code-behind:

    protected void Button1_Click(object sender, EventArgs e)
    {
if (PhotoUpload.HasFile)
    {
      Stream photoStream = PhotoUpload.PostedFile.InputStream;
      int photoLength = PhotoUpload.PostedFile.ContentLength;
      string photoMime = PhotoUpload.PostedFile.ContentType;
      string photoName = Path.GetFileName(PhotoUpload.PostedFile.FileName);
      byte[] photoData = new byte[photoLength - 1];
      photoStream.Read(photoData, 0, photoLength);

   string CS = ConfigurationManager.ConnectionStrings["mm"].ConnectionString;
   using (SqlConnection con = new SqlConnection(CS))
   {
       con.Open();
       SqlCommand cmd = new SqlCommand("spLogo", con);
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.Parameters.AddWithValue("@FName", FirstName.Text);
       cmd.Parameters.AddWithValue("@SName", Surname.Text);
       cmd.Parameters.AddWithValue("@PData", photoData);
       cmd.Parameters.AddWithValue("@PName", photoName);
       cmd.Parameters.AddWithValue("@PLength", photoLength);
       cmd.Parameters.AddWithValue("@PMime", photoMime);
       cmd.ExecuteNonQuery();
   }
}

What could be the problem here?

This is my stored procedure:

       alter procedure spLogo
       @FName nvarchar(50),
       @SName nvarchar(50),
       @PData VarBinary(max),
       @PName nvarchar(50),
       @PLength int,
       @PMime nvarchar(50) 
       as
       begin
       Insert into Employee2 values(@FName,@SName,@PData,@PName,@PLength,@PMime)
       END

I am working on an image for the first time so I'm not sure which datatype would be the best but this is the best code I can come up with.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mash
  • 167
  • 1
  • 7
  • 25
  • In which line you get the error? photoStream.Read(photoData, 0, photoLength); ?? – Mate Jun 29 '14 at 01:35
  • photoData is smaller than PhotoUpload.PostedFile.InputStream... maybe? – Mate Jun 29 '14 at 01:48
  • so how can i make sure it supports images of all size.I don't know if using VarBinary(max) plays any role here. – Mash Jun 29 '14 at 01:53
  • check this to save fileupload to byte [] , http://stackoverflow.com/questions/3068303/fileupload-to-filestream , Varbinary(max) does not seem to be the problem... and maybe you could try/catch this block... – Mate Jun 29 '14 at 01:56

1 Answers1

0

This line could be the root cause, as you are creating an array of byte with length lesser than image length.

byte[] photoData = new byte[photoLength - 1];

lets say, the photolength is 1000, so you are creating byte array (photoData) of 999 size, now when the below line tries to copy 1000th byte to photoData, it will fail.

photoStream.Read(photoData, 0, photoLength);

You should create the byte array as same as Image length. Hope it works.

byte[] photoData = new byte[photoLength];
Vinit
  • 2,540
  • 1
  • 15
  • 22