Click here to Skip to main content
14,690,515 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to retrieve images from a table.Here the picture boxes (bu1) are created dynamically.
But this code retrieve only the last image of the table.how can it retrieve all the images in the table.what is wrong in this code??
please help me!!!


try
{
   //string dbcnstr = "Data Source=MLBSTDSVR;Initial Catalog=DBMSI;Integrated Security=True";
   string dbcnstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\compaq\Desktop\project\E-Voting System\Database\VotingDB.mdf;Integrated Security=True;User Instance=True";
   SqlConnection con = new SqlConnection(dbcnstr);
   con.Open();

   //Retrieve BLOB from database into DataSet.
   SqlCommand cmd = new SqlCommand("SELECT Image FROM Pictures", con);
   SqlDataAdapter da = new SqlDataAdapter(cmd);
   DataSet ds = new DataSet();
   da.Fill(ds, "Pictures");
   int c = ds.Tables["Pictures"].Rows.Count;

   for( i=0;i<;c;i++)
   {
      Byte[] byteProfilePIC = new Byte[0];
      byteProfilePIC = (Byte[])(ds.Tables["Pictures"].Rows[c - 1]["Image"]);
      MemoryStream stmProfilePIC = new MemoryStream(byteProfilePIC);
      bu1[i].BackgroundImage = Image.FromStream(stmProfilePIC);
      bu1[i].BackgroundImageLayout = ImageLayout.Stretch;                    
   }
                
   cmd.Cancel();
   cmd.Dispose();
   da.Dispose();
   con.Close();
   con.Dispose();
}
Posted
Updated 30-May-11 6:10am
v4

1 solution

Try changing this:
byteProfilePIC = (Byte[])(ds.Tables["Pictures"].Rows[c - 1]["Image"]);
To use the loop varariable:
byteProfilePIC = (Byte[])(ds.Tables["Pictures"].Rows[i]["Image"]);
At present the same image is pulled out each time...

BTW:
for( i=0;i<;c;i++)
Won't compile:
for( i=0;i<c;i++)>
Will.
   
Comments
Member 7779792 30-May-11 14:42pm
   
thank you!! it works perfectly!!
but still it gives a run time error! it appears the this message (which is in catch block) "object reference not set to an instance of an object" and then appears the list of images in the form.
why is that?can some one help me to solve it!!
OriginalGriff 30-May-11 14:44pm
   
Which line is it complaining about? The exception will tell you all the details you need, just put a breakpoint in the catch block, and have a look at the exception.
Member 7779792 31-May-11 13:36pm
   
following is the catch block that i have place at the bottom of the above try block. it runs the exception saying that "object reference not set to an instance of an object" images are appered in the form soon after this message is run.

catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
OriginalGriff 31-May-11 14:15pm
   
Put a break-point on the MessageBox and examine "ee" more closely. It will give you a stack trace, which should include the line.
If you can't find it from there, go to "Debug...Exceptions" on the VS menu, and tick every box. Run it again, and it should break at the line throwing the exception.
Member 7779792 31-May-11 14:34pm
   
exception is running on the following line,

bu1[i].BackgroundImage = Image.FromStream(stmProfilePIC);
OriginalGriff 31-May-11 14:52pm
   
So, it implies that the problem is one of two things:
1) stmProfilePIC is null (No, it isn't), or that an empty stream causes this - unlikely.
2) bu1 is not big enough.
Since the latter is the most likely, that's where I would look. How many elements did you create in bu1? How big is "c" going to get?
Member 7779792 31-May-11 15:03pm
   
private PictureBox [] bu1=new PictureBox[10];

and c will be getting 5(there are 5 rows in pic table)
OriginalGriff 31-May-11 15:07pm
   
Have you checked? Put a breakpoint on the
int c = ds.Tables["Pictures"].Rows.Count;
line, and single step. Check the size of bu1, and the value of c.
If they look right, single step through the loop, and check every part of the line that throws the exception before you execute it.

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