Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello Sir,
For storing the image I used byte format & datatype of image field is varchar(MAX).But in crystal report image is not displayed.And I am developing this is in WINDOWS FORM.
Please help me.
Posted
Updated 22-Aug-17 8:58am
v2

Hello,

Have a look at this[^] article on ASP Snippets.

Regards,
 
Share this answer
 
public byte[] convertImageToByteArray(System.Drawing.Image image)
        {
            MemoryStream ms = new MemoryStream();
            
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                // or whatever output format you like
                return ms.ToArray();
            
        }
//Save Data
        private void button1_Click(object sender, EventArgs e)
        {
            byte[] imgbnry = convertImageToByteArray(pictureBox1.Image);
 
            string str = "insert into tbl_img (id,photo)values('" + textBox1.Text + "',@img)";
 
            string ConStr = @"Server=COMP7;Database=ImageTest;User Id=sa;Password=cos123";
 
            SqlConnection con = new SqlConnection(ConStr); 
            con.Open();
            SqlCommand cmd = new SqlCommand(str, con);
 
            cmd.Parameters.AddWithValue("@img", imgbnry);
            int n = cmd.ExecuteNonQuery();
            if (n > 0)
            {
                MessageBox.Show("Successfully inserted");
            }
            else
            {
                MessageBox.Show("Not inserted");
            }
            
        }


//retrive Data
 Collapse | Copy Code
private void button2_Click(object sender, EventArgs e)
        {
            string str = "select photo from tbl_img where id='" + textBox2.Text + "'";
 
            string ConStr = @"Server=COMP7;Database=ImageTest;User Id=sa;Password=cos123";
 
            SqlConnection con = new SqlConnection(ConStr); 
 
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(str, con);
            DataTable dt = new DataTable();
 
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                byte[] imgdata = new byte[0];
                imgdata = (byte[])dt.Rows[0][0];
                MemoryStream ms = new MemoryStream(imgdata);
                pictureBox2.Image = Image.FromStream(ms);      
            }
            else
            {
                MessageBox.Show("No images in a table");
            }
        }


use image data type to store image at sql server.
attach this table to crystal report.
 
Share this answer
 
Comments
Member 11335460 24-Jan-15 4:39am    
Thank you for reply.
While retrieving the image it gives Exception ,
System ArgumentException"Parameter is not valid"
Sid_Joshi 24-Jan-15 4:55am    
what data type are u using?
use Image Data type for storing image at sql server
Member 11335460 24-Jan-15 4:56am    
yes I m using image datatype but still i got same exception
Sid_Joshi 24-Jan-15 5:07am    
where you got error?
at crystal report or showing image at pictureBox2?
Member 11335460 24-Jan-15 5:14am    
I get error while showing the image "pictureBox2.Image = Image.FromStream(ms);" on this line.

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