Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
i use below coading for set image from database, its give error please help..!!
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 26-Sep-12 0:09am
Edited 26-Sep-12 0:25am
v2
Comments
vaibhav mahajan at 26-Sep-12 5:53am
   
dont use ie6...
Rate this: bad
good
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).
  Permalink  
Comments
Member 9444634 at 26-Sep-12 5:20am
   
i insert using SP and use properties so, how can save using this code...!!
OriginalGriff at 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 at 26-Sep-12 5:20am
   
how to know image is loaded or not before assign to picturebox
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hey,
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".
            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
 
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
  Permalink  
v2
Comments
Member 9444634 at 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 at 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

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

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 BillWoodruff 220
4 Mathew Soji 195
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,817
2 DamithSL 5,461
3 Manas Bhardwaj 4,946
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2014
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