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
- String connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("App_Data/db1.mdb");
- // Object created for Oledb Connection
- OleDbConnection myAccessConnection;
- protected void openAccessConnection()
- {
- // If condition that can be used to check the access database connection
- // whether it is already open or not.
- if (myAccessConnection.State == ConnectionState.Closed)
- {
- myAccessConnection.Open();
- }
- }
- protected void closeAccessConnection()
- {
- // If condition to check the access database connection state
- // If it is open then close it.
- if (myAccessConnection.State == ConnectionState.Open)
- {
- myAccessConnection.Close();
- }
- }
- private void Page_Load(object sender, System.EventArgs e)
- {
- // Put user code to initialize the page here
- myAccessConnection = new OleDbConnection(connStr);
- openAccessConnection();
- OleDbCommand myCommand = new OleDbCommand("select img_stream from tblImg where img_id=4", myAccessConnection);
- //OleDbParameter img_id = new OleDbParameter("4", OleDbType.Integer);
- //myCommand.Parameters.Add(img_id);
- OleDbDataReader reader = myCommand.ExecuteReader();
- if(reader.Read())
- {
- Response.ContentType = "image/jpeg";
- byte[] imageContent = (byte[])(reader["img_stream"]);
- ms.Write(imageContent, 0, imageContent.Length);
- Bitmap bmp = null;
- bmp = new Bitmap(ms);
- bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
- ms.Close();
- closeAccessConnection();
- }
- else
- {
- closeAccessConnection();
- }
- }
"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 :-(