Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to display image in PictureBox from database. I'm working .NET window application. I wrote code for this problem but I got error.
Please solve my problem or tell me other solution.

C#
        private void button4_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd = new SqlCommand("select * from tblImage where ID=2", con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                byte[] b = new byte[0];
                b = (Byte[])(dr["ImgUrl"]);
                MemoryStream ms = new MemoryStream(b);
                pictureBox1.Image = Image.FromStream(ms);
                //pictureBox1.Image = Convert.ToSByte(dr["Url"].ToString());

}
            con.Close();
        }


I took two column in table.firstone path type nvarchar(max) and second one ImgUrl type image.
Posted
Updated 12-Jan-11 22:10pm
v4
Comments
Umair Feroze 12-Jan-11 4:46am    
Please also post your error. It does not seem to be any error in the code

The column name in your table seems to say "Hey I'm the url to an image that's why they called me ImgUrl". Looking at the code on the other hand seems to imply that the image itself is stored in the database and not some url to an image.

So what type is column ImgUrl?
And as Umair already said please post the error message in your question.

Regards,
Manfred
 
Share this answer
 
v2
Comments
Dalek Dave 13-Jan-11 4:33am    
You tell him, Manfred.
 
Share this answer
 
Comments
Dalek Dave 13-Jan-11 4:33am    
Good Links.
justinonday 17-Jan-11 4:52am    
Thanks Dalek Dave
C#
OleDbConnection conn = new OleDbConnection(connectionString);
           OleDbCommand SQLCommand = new OleDbCommand();
           conn.Open();
           string getpwd = textBox3.Text.ToString();
           string query2 = "select * from Table1 where Filename= '" + getpwd + "'";
           Console.Write(query2);
           OleDbCommand s = new OleDbCommand(query2, conn);
           OleDbDataReader bReader = s.ExecuteReader();
           if (textBox3.Text == "")
           {
               MessageBox.Show("Please Enter the Image name to retrive!!");
            }

           else if (bReader.Read())
           {
               byte[] b = new byte[0];
               b = (Byte[])(bReader["imgname"]);
               MemoryStream ms = new MemoryStream(b);
               pictureBox1.Image = Image.FromStream(ms);
               //pictureBox1.Image = Convert.ToSByte(dr["Url"].ToString());

           }


           else
           {
               MessageBox.Show("Please Enter Valid Image name!!!");
           }


           conn.Close();
 
Share this answer
 
v2

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