0

I use Entity Framework and LINQ.

Simply, I want to take image file from fileupload control, and save it in a SQL Server database. But I get an error..

Table: Images

imageId int
imageData varbinary(max)

Stored procedure:

ALTER proc [dbo].[UploadImage]
(
    @img varbinary(max)
)
as
    insert into Images(imageData)
    values(@img)

C# code (after choosing file and clicking the button):

while (FileUpload1.HasFile)
{
    System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
    byte[] imgByteArray = new byte[FileUpload1.PostedFile.InputStream.Length];
    FileUpload1.PostedFile.InputStream.Read(imgByteArray, 0, (int)FileUpload1.PostedFile.InputStream.Length);

    using (DummyDBEntities context=new DummyDBEntities())
    {
        context.UploadImage2(imgByteArray);
    }
}

Can you help me figuring out the problem?

Thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
perlynsparks
  • 401
  • 3
  • 7
  • 20
  • 1
    What exception are you getting, and which line of code throws the exception? – Matt M Jul 08 '14 at 19:46
  • in model.context.cs file, 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll but was not handled in user code Additional information: An error occurred while executing the command definition. See the inner exception for details. – perlynsparks Jul 08 '14 at 19:50
  • and in this line of code throws the exception: return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("UploadImage2", imgParameter); – perlynsparks Jul 08 '14 at 19:51
  • Look at the inner exception. What does that say? – Matt M Jul 08 '14 at 19:54
  • I tryed try-catch blog for exception there was nothing.Then I looked from View Detail section and innerexception comes null. – perlynsparks Jul 08 '14 at 20:34
  • Can you post your UploadImage2 function code? – Matt M Jul 08 '14 at 20:56
  • public virtual int UploadImage2(byte[] img) { var imgParameter = img != null ? new ObjectParameter("img", img) : new ObjectParameter("img", typeof(byte[])); return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("UploadImage2", imgParameter); } – perlynsparks Jul 08 '14 at 21:03
  • I checked again the innerexception. it says : "Cannot insert the value NULL into column 'imageId'. Thats because I forgot to make Identity Specification in SQL table. Thank you so much for reminding me to look at the innerexception! and for your help! :) – perlynsparks Jul 09 '14 at 07:47
  • No problem! Glad you figured it out. – Matt M Jul 09 '14 at 12:04

0 Answers0