Solved

Problem to save picture file

Posted on 2013-11-27
6
675 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 21
Comment Utility
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 24

Expert Comment

by:chaau
Comment Utility
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 10

Author Comment

by:HuaMinChen
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 24

Expert Comment

by:chaau
Comment Utility
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 10

Author Comment

by:HuaMinChen
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now