Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

trying to retrieve image from the database

$
0
0
hi,

i'm trying to retrieve my image from the database, as its stored as an array of bytes. and display in my picture control in winform. I'm currently getting an:


invalid operationunhandled excepetion. ExecuteScalar: Connection property has not been initialized.

byte[] imagearray = (byte[])cmd.ExecuteScalar();

here the rest of my code:


using (SqlConnection sql = new SqlConnection(@"data source=.\SQLEXPRESS;Integrated Security=SSPI;User Instance=true"))
            {
                SqlCommand cmd = new SqlCommand("SELECT Image FROM Items WHERE (Image = @image");
                cmd.Parameters.Add("image", SqlDbType.Image);
                cmd.Parameters[0].Value = textBox1.Text;

                sql.Open();
                byte[] imagearray = (byte[])cmd.ExecuteScalar();

                MemoryStream ms = new MemoryStream(imagearray);

                System.Drawing.Bitmap a = new System.Drawing.Bitmap(ms);


                pictureBox1.Image = a;
            }




so where am i going wrong? also a solution to this would be nice.

thanks in advance

ray

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>