Click here to Skip to main content
12,699,807 members (35,013 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.0 C#
i use below coading for set image from database, its give error please help..!!
byte[] imageSource = ((byte[])dtCompany.Rows[0]["CompanyLogo"]);
                            Bitmap image;
                            using (MemoryStream stream = new MemoryStream(imageSource))
                               image = new Bitmap(stream);
                            pbCompanyLogo.Image = image;
Posted 26-Sep-12 0:09am
Updated 26-Sep-12 0:25am
vaibhav mahajan 26-Sep-12 5:53am
dont use ie6...
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Normally, the problem you get is "Parameter is not valid." which indicates that the data in teh database is not a valid image. Often the problem is actually caused when the image is loaded into the database, particularly if the image is saved by concatenating strings to form an SQL command:
string sql = "INSERT INTO myTable (imageData) VALUES ('" + myImage + "')";
Actually generates an sql string:
INSERT INTO myTable (imageData) VALUES ('System.Drawing.Bitmap')
Which won't throw an error, but also doesn't store the actual image data.

Check your DB, and if this is the case use parametrized queries to load the DB instead of string concatenation (you should be doing that anyway as a matter of course).
Member 9444634 26-Sep-12 5:20am
i insert using SP and use properties so, how can save using this code...!!
OriginalGriff 26-Sep-12 5:34am
Doesn't matter - if you pass a string through to the DB when you store it rather than the actual byte data, it will work, but not store what you want.
Can you show the c# code you use to store the image?
Is "Parameter not valid" the error message you are getting? If not, what are you getting, and where?
Member 9444634 26-Sep-12 5:20am
how to know image is loaded or not before assign to picturebox
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I wish that this is the solution that you are looking for

use this code to Insert A pic into DB

//We are using SQL express.
            //My database name is "PictureDb".
            SqlConnection con = new SqlConnection
                               ("Server=.;database=PictureDb;integrated security=true");
            //I have used a table named "tblUsers" and fill the fields
            SqlCommand com = new SqlCommand("insert into tblUsers
                            (fldCode,fldPic) values(1,@Pic)", con);

            //In here, I have to save the picturebox image to tblUsers
            //because you were not able to send the PictureBox1.Image
            //to field "fldPic" that is of kind
            //"image" in SQL SERVER EXPRESS, then you should do something else
            // and that is converting
            //your picture box image to an array of bytes and then sending
            //that array to database.
            //Below, we have used a stream which will be used to
            //contain our picturebox image bytes.
            MemoryStream stream=new MemoryStream();
            //through the instruction below, we save the
            //image to byte in the object "stream".

            //Below is the most important part, actually you are
            //transferring the bytes of the array
            //to the pic which is also of kind byte[]
            byte[] pic=stream.ToArray();

            com.Parameters.AddWithValue("@Pic", pic);
            catch (Exception ex)

And This Code To recieve the picture from DB

SqlConnection connect = new SqlConnection
                             ("Server=.;database=PictureDb;integrated security=true");
            SqlCommand command = new SqlCommand
                                ("select fldPic from tblUsers where fldCode=1", connect);
            //for retrieving the image field in SQL SERVER EXPRESS
            //Database you should first bring
            //that image in DataList or DataTable
            //then add the content to the byte[] array.
            //That's ALL!
            SqlDataAdapter dp = new SqlDataAdapter(command);
            DataSet ds = new DataSet("MyImages");

            byte[] MyData = new byte[0];

            dp.Fill(ds, "MyImages");
            DataRow myRow;
            myRow = ds.Tables["MyImages"].Rows[0];

            MyData = (byte[])myRow["fldPic"];

            MemoryStream stream = new MemoryStream(MyData);
            //With the code below, you are in fact converting the byte array of image
            //to the real image.
            pictureBox2.Image = Image.FromStream(stream);

Member 9444634 26-Sep-12 5:28am
use this code but also give same error:

Object reference not set to an instance of an object.
Ahmed Mandur 26-Sep-12 5:53am
Please Check this link which contains an example to save and receive a picture from and to picturebox
Member 11399192 25-Jun-15 5:24am
use these code still error paramate is not valid

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

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100