469,919 Members | 2,135 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,919 developers. It's quick & easy.

Display image on C# web page through ms-access

3
Hi everyone!!
I m trying to display jpg image from ms-access database on C# web page. Image is stored as OLE object under "img_stream" variable in database. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. String connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("App_Data/db1.mdb");
  2.  
  3.         // Object created for Oledb Connection
  4.         OleDbConnection myAccessConnection;
  5.  
  6.         protected void openAccessConnection()
  7.         {
  8.             // If condition that can be used to check the access database connection
  9.             // whether it is already open or not.
  10.             if (myAccessConnection.State == ConnectionState.Closed)
  11.             {
  12.                 myAccessConnection.Open();
  13.             }
  14.         }
  15.  
  16.         protected void closeAccessConnection()
  17.         {
  18.             // If condition to check the access database connection state
  19.             // If it is open then close it.
  20.             if (myAccessConnection.State == ConnectionState.Open)
  21.             {
  22.                 myAccessConnection.Close();
  23.             }
  24.  
  25.         }
  26.  
  27.    private void Page_Load(object sender, System.EventArgs e)
  28.    {
  29.         // Put user code to initialize the page here
  30.        myAccessConnection = new OleDbConnection(connStr);
  31.        openAccessConnection();
  32.        OleDbCommand myCommand = new OleDbCommand("select    img_stream from tblImg where img_id=4", myAccessConnection);
  33.       //OleDbParameter img_id = new OleDbParameter("4", OleDbType.Integer);
  34.      //myCommand.Parameters.Add(img_id);
  35.       OleDbDataReader reader = myCommand.ExecuteReader();
  36.       if(reader.Read())
  37.       {
  38.          Response.ContentType = "image/jpeg";
  39.          byte[] imageContent = (byte[])(reader["img_stream"]);
  40.          ms.Write(imageContent, 0, imageContent.Length);
  41.          Bitmap bmp = null;
  42.          bmp = new Bitmap(ms);
  43.          bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
  44.          ms.Close();
  45.          closeAccessConnection();
  46.      }
  47.      else
  48.      {
  49.         closeAccessConnection();
  50.      }
  51. }
On executing code i get the following error:
"Invalid Parameter Used"
Error is at: bmp = new Bitmap(ms);
Please help me to resolve this problem. I will highly obliged as i have passed so much time in finding and resolving this error but no way out :-(
Aug 7 '08 #1
3 2858
Curtis Rutland
3,256 Expert 2GB
Where did you define ms?
Aug 7 '08 #2
nudma
3
Where did you define ms?
I'm sorry that got deleted by me, by mistake...Here it is:
if(reader.Read())
{
MemoryStream ms = new MemoryStream();
Response.ContentType = "image/jpeg";
byte[] imageContent = (byte[])(reader["img_stream"]);
ms.Write(imageContent, 0, imageContent.Length);
Bitmap bmp;
bmp = new Bitmap(ms);
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
ms.Close();
closeAccessConnection();
}
else
{
closeAccessConnection();
}
Please check it out and help me out to resolve my error...
Aug 8 '08 #3
nudma
3
I'm sorry that got deleted by me, by mistake...Here it is:
if(reader.Read())
{
MemoryStream ms = new MemoryStream();
Response.ContentType = "image/jpeg";
byte[] imageContent = (byte[])(reader["img_stream"]);
ms.Write(imageContent, 0, imageContent.Length);
Bitmap bmp;
bmp = new Bitmap(ms);
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
ms.Close();
closeAccessConnection();
}
else
{
closeAccessConnection();
}
Please check it out and help me out to resolve my error...
Dear All!!
I m still solution less..Please help me...
Aug 12 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Peter Lapic | last post: by
3 posts views Thread by den 2005 | last post: by
3 posts views Thread by c676228 | last post: by
1 post views Thread by pkamil83 | last post: by
10 posts views Thread by =?Utf-8?B?UmludSBHb3BhbGFrcmlzaG5hIFBpbGxhaQ==?= | last post: by
17 posts views Thread by seajay | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.