2 Replies - 4232 Views - Last Post: 25 December 2012 - 10:49 AM Rate Topic: -----

#1 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

trying to retrieve image from the database

Posted 25 December 2012 - 08:19 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: trying to retrieve image from the database

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5498
  • View blog
  • Posts: 11,804
  • Joined: 02-June 10

Re: trying to retrieve image from the database

Posted 25 December 2012 - 09:34 AM

Quote

Connection property has not been initialized.


While many errors are cryptic, such as the "out of memory" error that raises when you try to open a graphic that is corrupt - this error is pretty plain.

If you place a breakpoint at line 3 and look at the values as you walk through the execution you'll see the .Connection property has some issue such as not being open or is null.

ray1234: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated DEC 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 teyrebaz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-January 12

Re: trying to retrieve image from the database

Posted 25 December 2012 - 10:49 AM

and after connection issue;

MemoryStream ms = new MemoryStream(imagearray);

System.Drawing.Bitmap a = new System.Drawing.Bitmap(ms);//no need here , you can directly get from stream.

pictureBox1.Image = Image.FromStream(ms);
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1