Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I work in windows form c# working with access 2007

i need to make function to update image in access where = UserID

BUT I need to know how to do function and how to call it

public string updateimg(string UserID,string path)
       {
           string valuereturn = null;
           var oleDbConnection = new OleDbConnection(connection);
           var oleDbCommand = oleDbConnection.CreateCommand();
           oleDbCommand.CommandText = "Update AllPrinting set  qrimg=@qrimg where UserID=@UserID)";
           oleDbCommand.Parameters.AddWithValue("@UserID", "UserID");
           byte[] yourPhoto = path;
           oleDbCommand.Parameters.AddWithValue("@photo", yourPhoto);
           using (oleDbConnection)
           {
               oleDbConnection.Open();
               valuereturn= oleDbCommand.ExecuteNonQuery().ToString();
           }
           return valuereturn;
       }

problem how to assign value for byte in function
and how to call this function
i assign data type for field qrimg to OLEOBJECT
please help me

What I have tried:

How to make function update image to access 2007
Posted
Updated 7-Mar-17 22:35pm

1 solution

It depends if you want to store a path to an image file or the image itself.

The path is stored as any other string.

The image must be stored as BLOB (Binary Large OBject). Read the image file into a byte buffer and store that:
C#
byte[] buffer = File.ReadAllBytes(filename);
// ...
oleDbCommand.Parameters.AddWithValue("@photo", buffer);


[EDIT]
There is also an error in your UPDATE statement. It must be
SQL
"Update AllPrinting set  qrimg=@qrimg where UserID=@UserID;"
instead of
SQL
"Update AllPrinting set  qrimg=@qrimg where UserID=@UserID)"

[/EDIT]
 
Share this answer
 
v2
Comments
ahmed_sa 8-Mar-17 8:03am    
i do as below but update not happen
please help me
byte[] buffer = File.ReadAllBytes(path);


OleDbConnection oleDbConnection = new OleDbConnection(connection);
string str = "Update AllPrinting set qrimg=@qrimg where UserID=@UserID";
OleDbCommand oleDbCommand = new OleDbCommand();
oleDbConnection.Open();
oleDbCommand.Connection = oleDbConnection;
oleDbCommand.CommandText = str;
oleDbCommand.Parameters.AddWithValue("@UserID", label4.Text);
byte[] yourPhoto = buffer;
oleDbCommand.Parameters.AddWithValue("@qrimg", yourPhoto);
using (oleDbConnection)
{

oleDbCommand.ExecuteNonQuery();

}
oleDbConnection.Close();
Jochen Arndt 8-Mar-17 8:07am    
Any error message?

Catch exceptions to detect errors and get error messages (see https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbexception(v=vs.110).aspx).

You are making an UPDATE. Does your table has a column 'qrimg' of type BLOB?
ahmed_sa 8-Mar-17 8:18am    
what type Blob i dont have type blob i set qrimg column to datatype oleobject
also these two columns exist in table AllPrinting
Jochen Arndt 8-Mar-17 8:20am    
BLOB is an object type. I just asked to be sure.

What about the error message?
ahmed_sa 8-Mar-17 8:24am    
i make catch it not give me any error
please help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900