Solved

Problem to save picture file

Posted on 2013-11-27
6
697 Views
Last Modified: 2013-11-28
Hi,
when saving the picture file to Access DB, like

                    fl_nm = "c:\\inetpub\\VS2012\\App17\\" + lb_ph_file2.Text;
                    cmd = new OleDbCommand("update ite_tab set photo_file=@image where ite_abbr=@abbr", conn);

                    cmd.Parameters.Add("@abbr", OleDbType.VarChar).Value = tb_iteabbr.Text.Trim();
                    cmd.Parameters.Add("@image", OleDbType.Binary).Value = ImageToStream(fl_nm);
                    if (cmd.ExecuteNonQuery() >= 1) 
                    ...

        private byte[] ImageToStream(string fileName)
        {
            Bitmap image = new Bitmap(fileName);
            MemoryStream stream = new MemoryStream();
            image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            return stream.ToArray();
        }

Open in new window


I get this exception. why?
An UPDATE or DELETE query cannot contain a multi-valued field.Microsoft Access Database Engine
0
Comment
Question by:HuaMinChen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 21
ID: 39682782
AFAIK, you can use an append query to add an file to a field that ius a attachment data type.

Here is an VBA example using recordsets:

On Error GoTo Err_AddImage

Dim db As DAO.Database
Dim rsParent As DAO.Recordset2
Dim rsChild As DAO.Recordset2

Set db = CurrentDb
Set rsParent = Me.Recordset

rsParent.Edit

' open a second recordset on the multi-value (attachment) field
Set rsChild = rsParent.Fields("AttachmentTest").Value

rsChild.AddNew
rsChild.Fields("FileData").LoadFromFile ("c:\Sunset.jpg")

rsChild.Update
rsParent.Update

Exit_AddImage:

Set rsChild = Nothing
Set rsParent = Nothing
Exit Sub

Err_AddImage:

If Err = 3820 Then
MsgBox ("File already part of the multi-valued field!")
Resume Next

Else
MsgBox "Some Other Error occured!", Err.Number, Err.Description
Resume Exit_AddImage

End If

Open in new window

0
 
LVL 25

Expert Comment

by:chaau
ID: 39682788
I think you need to use OleDbType.LongVarBinary, like this:
QueryParameter par = new OleDbParameter("@image", OleDbType.LongVarBinary,
PictureByteArray.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current,
ImageToStream(fl_nm));
cmd.Parameters.Add(par);

Open in new window

0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 39682797
Sorry Chaau, I then get these 2 errors
Error      3      The type or namespace name 'QueryParameter' could not be found (are you missing a using directive or an assembly reference?)      C:\App17\App17\Default.aspx.cs      668      21      App17
Error      4      The name 'PictureByteArray' does not exist in the current context      C:\App17\App17\Default.aspx.cs      669      25      App17

due to this line
                    QueryParameter par = new OleDbParameter("@image", OleDbType.LongVarBinary,
                        PictureByteArray.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current,
                        ImageToStream(fl_nm));

Open in new window

0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 25

Expert Comment

by:chaau
ID: 39682805
sorry, I meant this:
OleDbParameter par = new OleDbParameter....

Open in new window

Actually, change the code like this:
byte[] PictureByteArray = ImageToStream(fl_nm);
OleDbParameter par = new OleDbParameter("@image", OleDbType.LongVarBinary,
PictureByteArray.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current,
PictureByteArray);
cmd.Parameters.Add(par);

Open in new window

0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 39682813
Sorry, I still get the same error
An UPDATE or DELETE query cannot contain a multi-valued field.Microsoft Access Database Engine

due to these
                    fl_nm = "c:\\inetpub\\VS2012\\App17\\" + lb_ph_file2.Text;
                    cmd = new OleDbCommand("update ite_tab set photo_file=@image where ite_abbr=@abbr", conn);

                    cmd.Parameters.Add("@abbr", OleDbType.VarChar).Value = tb_iteabbr.Text.Trim();
                    byte[] PictureByteArray = ImageToStream(fl_nm);
                    OleDbParameter par = new OleDbParameter("@image", OleDbType.LongVarBinary,
                    PictureByteArray.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current,
                    PictureByteArray);
                    cmd.Parameters.Add(par);
                    if (cmd.ExecuteNonQuery() >= 1) 
                    ...

Open in new window

0
 
LVL 21

Accepted Solution

by:
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP earned 145 total points
ID: 39683857
The attachment data type is a type of sub recordset. Even within Access, to add image with code  you must use DAO not an update query.


From a fellow Microsoft VB MVP:

I don't know whether you can insert into an Access Attachment column using ADO.NET. The only solution that I am aware of requires DAO (Project...Add Reference...COM tab...Microsoft DAO 3.6 Object Library).

How to: Work With Attachments In DAO
at http://msdn.microsoft.com/en-us/library/bb258184%28office.12%29.aspx

Paul ~~~~ Microsoft MVP (Visual Basic)



Boyd Trimmell, Microsoft Access MVP
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question