Click here to Skip to main content
15,884,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i use below coading for set image from database, its give error please help..!!
C#
byte[] imageSource = ((byte[])dtCompany.Rows[0]["CompanyLogo"]);
                            MessageBox.Show(Convert.ToString(imageSource));
                            Bitmap image;
                            using (MemoryStream stream = new MemoryStream(imageSource))
                            {
                               image = new Bitmap(stream);
                            }
                            pbCompanyLogo.Image = image;
Posted
Updated 25-Sep-12 23:25pm
v2
Comments
vaibhav mahajan 26-Sep-12 5:53am    
dont use ie6...

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

use this code to Insert A pic into DB

C#
//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".
            pictureBox1.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);

            //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);
            try
            {
                con.Open();
                com.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }



And This Code To recieve the picture from DB

C#
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);



Regards,
A.Mandour
 
Share this answer
 
v2
Comments
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
http://www.mediafire.com/download.php?9icgk3gxgo2xayi
Member 11399192 25-Jun-15 5:24am    
use these code still error paramate is not valid
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:
C#
string sql = "INSERT INTO myTable (imageData) VALUES ('" + myImage + "')";
Actually generates an sql string:
SQL
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).
 
Share this answer
 
Comments
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

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