Click here to Skip to main content
15,867,686 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 5: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.
 
Share this answer
 
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);

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