Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

I try to retrieve an image from access database using following code:
C#
private void button3_Click(object sender, EventArgs e)
{
  OleDbConnection cn = new OleDbConnection(strCn);
  cn.Open();
  OleDbCommand cmd = new OleDbCommand("SELECT Iname,Oimage FROM [Image]", cn);
  OleDbDataAdapter da = new OleDbDataAdapter(cmd);
  DataSet ds = new DataSet();
  da.Fill(ds, "Image");
  int c = ds.Tables["Image"].Rows.Count;
  if (c > 0)
  {
    Byte[] byteOimage = new Byte[0];
    byteOimage = System.Text.Encoding.Unicode.GetBytes((ds.Tables["Image"].Rows[c - 1]["Oimage"]).ToString());
    MemoryStream strOimage = new MemoryStream(byteOimage);
    pictureBox1.Image = Image.FromStream(strOimage);//here i got error message Parameter is not valid.
  }
  cn.Close();
}

I mention in the last to second line of my code that I get the error.
So,how can I solve it?

Please help me.

Thanks in advance.
Posted
Updated 28-Dec-10 8:30am
v4
Comments
Marc A. Brown 28-Dec-10 13:54pm    
Formatted code block.

You are trying to use a zero length byte array, there is nothing in it

Byte[] byteOimage = new Byte[0];


C# Photo Album Viewer[^]

Look at the Display the image section. Although it is SQL Server, the same techniques should work with Access.

BTW, Hungarian notation (byteOimage, strOimage) hasn't bee used in years.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 28-Dec-10 14:52pm    
It's one of these useless initializations as he's overwriting the byteOimage variable in the next line :)
I too faced the same issue and solution given below :-

C#
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\rmaheswari\Documents\TestImage.accdb;Persist Security Info=False;"); 
            cn.Open();  OleDbCommand cmd = new OleDbCommand("SELECT Iname,Oimage FROM [Table1]", cn); 
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);  
            DataSet ds = new DataSet();  da.Fill(ds, "Image");  
            int c = ds.Tables["Image"].Rows.Count;  if (c > 0)
            {
                byte[] rawData = (byte[])ds.Tables["Image"].Rows[c - 1]["Oimage"];
                MemoryStream ms = new MemoryStream();
                Bitmap bm;
                ms.Write(rawData, 78, rawData.Length - 78);
                bm = new Bitmap(ms);
                pictureBox1.Image = bm;
               
            }  cn.Close();}
 
Share this answer
 
v2
Comments
JOAT-MON 28-Dec-10 15:28pm    
Added <pre> tags
jhon275 28-Dec-10 21:52pm    
thank for reply,

but the same error i get at code
bm=new Bitmap(ms);

please help........
maheswariragu 29-Dec-10 14:21pm    
I create a record manually in access database and use the above code to retrieve, it works for me.
if possible send me the access db file only with the Image table , will take a look.
This is your problem code:

C#
byteOimage = System.Text.Encoding.Unicode.GetBytes((ds.Tables["Image"].Rows[c - 1]["Oimage"]).ToString());


Access stores the data as a byte array. But then you take that byte array and convert it to a string.

Step through your code and put a breakpoint in at that line. Then, add a watch for ds.Tables["Image"].Rows[c - 1]["0image"]).ToString() and see what it says.

Just get rid of the ToString() on that line. You may also have to type cast it as in the other answer.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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