Click here to Skip to main content
16,019,619 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a windows form with a picturebox and i have an access database that is linked to the form with a table adapter. The database has a field that is of type OLE Object and stores bitmap images.
I need to be able to retrieve the image from the database to the picturebox control but the code i have is presenting an error- Parameter is not valid. Please Help Me
//Method
public Bitmap GetPhoto(byte[] photoBits)
        {
            if (photoBits != null && photoBits.Length > 0)
            {
                using (MemoryStream stream = new MemoryStream(photoBits))
                {
                    return new Bitmap(stream);
                }
            }
            else
            {
                return null;
            }
        }
cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "select M_Photo from MemberPhoto where M_id='" + mSearch.Text + '";
DataSet ds = new DataSet();                    
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cm;
da.Fill(ds, "MemberPhoto");                 
pictureBox1.Image = GetPhoto((byte[])dd.Tables[0].Rows[0]["M_Photo"]);
Posted
Updated 26-May-10 1:24am
v2

1 solution

This may not be correct since I am judging simply on the syntax colouring applied by the Code Project <pre></pre> tags, which incidentally I have had to apply on your behalf).

This part appears not to be correctly formatted (everything after mSearch.Text + is considered to be one string.
cmd.CommandText = "select M_Photo from MemberPhoto where M_id='" + mSearch.Text + '";
DataSet ds = new DataSet();                    
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cm;
da.Fill(ds, "MemberPhoto");                 
pictureBox1.Image = GetPhoto((byte[])dd.Tables[0].Rows[0]["M_Photo"]);


whereas
cmd.CommandText = "select M_Photo from MemberPhoto where M_id='" + mSearch.Text + "'"; 
DataSet ds = new DataSet();                    
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cm;
da.Fill(ds, "MemberPhoto");                 
pictureBox1.Image = GetPhoto((byte[])dd.Tables[0].Rows[0]["M_Photo"]);


appears correctly formatted.

All of this could have been avoided by your using a 'parameterized query' (just Google that for examples). Doing so will also help to avoid SQLInjection attacks.
 
Share this answer
 
v2
Comments
AhirRamesh 28-May-10 4:02am    
Reason for my vote of 1
thank you but i didnot Binding Picturebox..

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