Click here to Skip to main content
15,798,508 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.

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));
Updated 4-May-12 10:11am

1 solution

using (SqlConnection con = new SqlConnection(strConnect))
    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
aliprogrammer 4-May-12 15:20pm    
OriginalGriff 4-May-12 15:23pm    
You're welcome!
Monjurul Habib 8-May-12 16:38pm    

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