Click here to Skip to main content
15,886,772 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i'm trying to load images from databes to the picturebox. i use these following codes in order to load them to my picture. i wrote some codes but i don't know what should i do for continuing this codes.
Any help will be appreciated.
Thanks in advance.

C#
private void button1_Click(object sender, EventArgs e)
    {
        sql = new SqlConnection(@"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True");
        cmd = new SqlCommand();
        cmd.Connection = sql;
        cmd.CommandText = ("select Image from Entry where EntryID =@EntryID");
        cmd.Parameters.AddWithValue("@EntryID", Convert.ToInt32(textBox1.Text));
    }
Posted
Updated 4-May-12 9:11am
v2

1 solution

C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT Image FROM Entry WHERE EntryID =@EntryID", con))
       {
       cmd.Parameters.AddWithValue("@EntryID", Convert.ToInt32(textBox1.Text));
       using (SqlDataReader reader = com.ExecuteReader())
           {
           if (reader.Read())
               {
               byte[] bytes = (byte[]) reader["Image"];
               MemoryStream ms = new MemoryStream(bytes);
               Image img = Image.FromStream(ms);
               myPictureBox.Image = img;
               }
           }
       }
   }
 
Share this answer
 
Comments
aliprogrammer 4-May-12 15:20pm    
Thanks
OriginalGriff 4-May-12 15:23pm    
You're welcome!
Monjurul Habib 8-May-12 16:38pm    
5!

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