Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
private void GetImage()
{            
   SQLiteConnection sqlite_con = 
      new SQLiteConnection("Data Source=|DataDirectory|" + 
      "dbasedict.s3db;Version=3;New=False;Compress=True;");
   String querry2 = "select id, word, iimages from" + 
          " dictionario where word = '" + searchBox.Text + "'";
   SQLiteDataAdapter adap3 = new SQLiteDataAdapter(querry2, sqlite_con);
   DataSet set = new DataSet();
   adap3.Fill(set, "dictionario");
   DataTable dataTable = new DataTable();
   dataTable = (DataTable)set.Tables[0];
   MemoryStream ms = null;
   foreach (DataRow row in dataTable.Rows)            
   {          
       byte[] data = (byte[])row["iimages"];
       ms = new MemoryStream(data);
       Bitmap bmp = new Bitmap(ms);
       pictureBox1.Image = bmp;                           
    }            
}


When I try this code, it ends up getting an error saying: Exception is Unhandled MESSAGE: Exception. Can anyone help me in debugging this code please?

I cannot use image.fromstream because I am running this code in PocketPC 5 SDK, so I use Bitmap as an alternative to FromStream.

If anyone can debug it for me is much appreciated. All i want is to read images(.jpg) from the database and output it in a picturebox.

@abhinav: the only advantage of try and catch is that it didn't crash your system at runtime but still has an error. i tried it yesterday.

@pete: sir, what do you mean by dispose? sorry but im just a newbie. can u extend your sample code sir? i can more rely on sample code than explanation because i cannot understand english very well. :(
Posted
Updated 7-Feb-10 17:14pm
v3

Put your code in a try catch block.

If you can post the error you get someone may be able to help you better.
 
Share this answer
 
A few things you need to sort out in your code. First of all, it's wide open for a SQL injection attack - take a look at query2, you should use parameters in place of searchBox.Text instead.

You also need to release some resources in there. You need to Dispose of the memory stream and bitmap at the least, as well as the SQLiteConnection. Wrap them in using statements, e.g.:
using (ms = new MemoryStream(data))
{
  // Do some stuff.
}
 
Share this answer
 
use try catch statement
it will definetly helps u
 
Share this answer
 

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