How i can display image on C# web form through ms-access

Hi All!!

I'm using following code to display my image on web page. Image is stored in database as OLE object unser "img_stream" variable.

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);                  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;                        bmp = new Bitmap(ms);                        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);                        ms.Close();                        closeAccessConnection();                  }                  else                  {                        closeAccessConnection();                  }            }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();                  }             }

I get the following error: "Invalid Parameter Used"

Error is at line: "bmp = new Bitmap(ms);"

Please help me to resolve this error...i will be highly obliged...

 

 

0
saher
8/7/2008 12:52:06 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

14 Replies
643 Views

Similar Articles

[PageSpeed] 20

It's quite tricky to figure out what's going on- could you post the code properly formatted?

For example, what is the 'ms' variable. I assume it's a memory stream, but where is it declared and instantiated?


My Blog
0
booler
8/7/2008 2:10:56 PM

Here is correct code:

            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);                  MemoryStream ms = new MemoryStream();                  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;                        bmp = new Bitmap(ms);                        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);                        ms.Close();                        closeAccessConnection();                  }                  else                  {                        closeAccessConnection();                  }

            }

Please check it out and solve my problem...Thankyou!!!

0
saher
8/7/2008 6:31:40 PM

 I think the problem is you are writing to the memory stream, and then trying to read from it without resetting the sreeam position. Try this in your page load:

 

            myAccessConnection = new OleDbConnection(connStr);
openAccessConnection();
OleDbCommand myCommand = new OleDbCommand("select img_stream from tblImg where img_id=4",
myAccessConnection);
MemoryStream ms = null;
OleDbDataReader reader = myCommand.ExecuteReader();
if (reader.Read())
{
Response.ContentType = "image/jpeg";
byte[] imageContent = (byte[])(reader["img_stream"]);
ms = new MemoryStream(imageContent);

Bitmap bmp;
bmp = new Bitmap(ms);
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
ms.Close();
closeAccessConnection();
}
else
{
closeAccessConnection();
}
This instantiates a new memorystream, passing in the byte array. The stream position should therefore still be at zero when you pass it to the bitmap constructor. HTH. 
  
My Blog
0
booler
8/8/2008 9:01:26 AM

My problem is still unsolved...I get same error even after following your instruction..please help me out!!!
0
saher
8/12/2008 1:11:42 PM

 Can you check the contents of the byte array when it is returned, and ensure it is greater than zero?


My Blog
0
booler
8/13/2008 8:45:14 AM

Yes i have counted the length of byte array by using "int image_length = imageContent.Length" and it gives me answer greater than 0
0
saher
8/13/2008 2:57:28 PM

 Have you tried writing the memory stream contents to disk, to check what you have in there is actually a valid bitmap?


My Blog
0
booler
8/13/2008 4:07:33 PM
yes i have tried writing memory stream to .jpeg file but actual image is not created. No preview of the image is created, just .jpeg file is created.please help!!!
0
saher
8/14/2008 2:56:27 PM

 If that's the case, then perhaps the content you are retrieving was saved incorrectly. If so, then perhaps it is the method which originally saves the image to the DB which is at fault? Or possibly a problem with the database field type- perhaps it is constraining the number of bytes and therefore truncating the content?


My Blog
0
booler
8/15/2008 2:25:13 PM

 

Yes u'r right then u please guide me how can i display image from database..either storing it directly or through storing its path in database.. please guide me..its been 2 weeks and i have no solution..i will be highly thankful to you!!!
0
saher
8/16/2008 5:21:52 AM

 What is the type of your database field- the one which holds the image binary contents?


My Blog
0
booler
8/18/2008 10:15:34 AM

 

Image is stored in ms-access. The type of database field is OLE DB.
0
saher
8/18/2008 3:49:12 PM

 Hmm... I don't use Access very often, but I read that when you use an OLE Object field, Access appends a bunch of header information to the byte array before storing it. So, in this case, you would need to strip out this junk before returning the contents.

This article may be of some use, but I'm afraid the code is in VB6, so it's going to take a bit of tweaking.


My Blog
0
booler
8/18/2008 5:20:54 PM

ok thanku so much...i really appreciate it!!!

i will try it...and then i will tell you..

0
saher
8/18/2008 6:20:41 PM
Reply:

Similar Artilces:

For interrupting.web form Resized by position of the div : if div can't dispaly all (such as only top part , then web form heithten the heght),resize the web form
Web Form Resized by position of the div : if div can't dispaly all (such as only top part , then web form heithten the heght),resize the web form ! Because of my pool English, if you can't see , I will explain more ! ThanksGambler is the real life I guess you mean when form grows it also grow parent div element. This is normal. If you want div to stay fixed size and rather clip the form and maybe show a scrollbar, look at overflow property in CSS.Thanks------------------------------------------------------------This posting is provided "AS IS" with no warranties, and confers no ...

C#.NET WEB APPLICATION
hei guys, i need help on my application, i'm using C#.NET as front-ed and SQL as back-end, aside from a datagrid, i want to display my data using textboxes and not a datagrid, i can do it using Visual Basic 6 but since i'm a newbee at C#.NET i don't know how to do it, specially the codes, can anyone help me with it? thank you in advance! If you are doing windows development, i would suggest using 'DataForm wizard' (Use Solution Explorer-->Rightclick project-->Add new Item-->New DataForm). It will guide you through steps. If you are doing web apps. Use quickstart to learn them. h...

Strange output in web form after web form is displayed.
   I have been using a web user control for over a week now with no problems. I am now getting strange output in my web form after my controls display on the page. Below is the strange output. Can anyone tell me what is wrong here? Note I did not include it all because it is quite large.   Request Details Session Id: l5b5vkazucfatba4oliaw1vi Request Type: POST Time of Request: 9/10/2007 4:59:59 PM Status Code: 200 Request Encoding: Unicode (UTF-8) Response Encoding: Unicode (UTF-8) Trace Information Category Message From First(s) F...

Accessing two web forms in a single web form
  Hi. I am developing a website in VS 2005 with Ajax & SQL Server. I am having 5 forms in my application. 1) Login Form 2) Main form 3) Form "A" 4) Form "B" I need to show the main form after the User login. I need to show all the contents of form A or form B (Ex: Form "A" user or form "B" user) in the Main Page. Can I show a single page having Main Form controls as well as Form "A" and Form "B" controls? I should access all the controls including Form A/B in the Main Page. Thanks ...

How to access a control on a web form from anoter web form
hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it possible to access lbl1 from form2, change its text permenantly? thanks.   refer: http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspxSSNPlease remember to click "Mark as Answer" on the post that helps you. MinaSamy: hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it p...

Implementing .Net Form into an APS.net web form, can this be done
I am new here, but have been searching for a while, and may not have the correct lingo to find what I am looking for.  I am tasked with implementing a .exe application that was writen vb6 then converted to .net into a new website my team is developing. The idea is to put each of the 3 different forms in this .exe application of 3 different .aspx pages. I have attempted multiple things to get this into the page and even started to just rewrite it as an ASP.net web form, but even then I can't reuse any of the code since the System.Web.UI.Page doesn't inherit the sa...

can someone one give me difference between web form and mobile web form and which or where or when i'm using the forms?
thanks Marc Hi Marc, Based on my understanding, you want to know the differences between the ASP.NET web application and ASP.NET mobile web application. If I have misunderstood you, please feel free to let me know. The ASP.NET forms are used to develop the ASP.NET pages for desktop browsers and The ASP.NET mobile forms are used to develop the ASP.NET pages for mobile device browsers. When we create the ASP.NET mobile web forms pages, we can use nearly all the features of ASP.NET. But there are a couple of compatibility issues: Error Handling and Reporting. Tracing. Using Redi...

Can my web page with a form submit to other web pages forms?
I was wondering if I created an aspx page that had a form on it that I could use the submit button to submit this form's contents to other web pages forms(servers accepting those form's data). I am thinking this would be a great way to update data on particular web pages without having to actually visit those similar web pages. Any ideas, examples would be much appreciated. Thanks in advance.  Yes you can.  It's called Cross Page Posting: http://msdn2.microsoft.com/en-us/library/ms178139.aspxAlso: http://msdn2.microsoft.com/en-us/library/ms178140.aspx might ...

How to Make Web Forms from other Web Forms
In putting together an ASP.NET application for a biz application, we have come to the point where there are three different types of basic forms which have, for the most part, been perfected. Now its is a matter of cloning those forms for various uses and modifying or adding code to suit the particular need. Copy and paste works OK, but there is a lot of changing of certain words, etc. Is there a way in ASP.NET to take these basic forms, turn them into classes or templates, create forms from them, then add or overwrite code? ? (Anybody who is a Vis Fox Pro programmer would know imme...

Web forms and mobile web forms
Hey everyone, I am looking to develope a web site that depending on the device connecting to it, it will display a certain page. In this case im looking to create a web and wap version of the same project. I have the Web forms version finished using ASP.NET and C# and I am wondering how to create the mobile web form and incorporate it in the project. In the project folder, can I just add a mobile web??? But what about displaying device specific pages?? Cheers -Dudach Yes, you can add mobile web forms and controls to your Web Application project. No problem. For device/br...

Web Form embedded in another Web Form
Hi,  I have programmed a Web Form which acts like a scientific calculator. I would like to embedd this calculator in the upper left hand corner of another accounting Web Form. I don't want to host the calculator on the same server as the accounting page. I.e. I want them to have distinct URLs. Classically, I would do this using frames, but is there a better way using ASP? Is there some sort of Panel which can display the webpage at a specific URL which I can just drag onto my accounting Web Form? Thanks,  P1000   Frames are the only way to embed a webform inside ano...

Web Form vs. Web Content Form
If you don't mind, I would like to know what is the difference between those two items, when I go to add a new item I see both of those and I don't know what the difference is.  A content form is for use with a masterpage. A web form is a standalone web page. If you select web content form, you will then have to specify a masterpage for it. and what is a master page used for?  Hi, a master page is used to give a constant look and theme to a site, without having to duplicate code. So basically you can put your header, menu, nav and footer on a master page. Then a...

Include web form inside web form
I tried to include an aspx file in a asp.net web form. However, all the web controls (textbox, button) in the included page are not displayed when called. <Form id="Form1" method="post" runat="server"> <%   Response.writeFile("subPage.aspx"); %> <asp:button id="btnPrev" runat="server" text="Previous"></asp:button> </Form> There are some <asp:textbox> controls in the subPage.aspx file, the file itself works fine, but when it is included in the main page, all the controls are not displayed. Please assist. Thanks   u should u...

How to fire events in other web form from other web form ?
Suppose that I have A and B web form. I want to trigger B web form's Page_Load event from A web form. How can I do that ? Thanks in advanced WilbertWilbert Bujn Create a public method in B that call the load method : public void Load(object sender, System.EventArgs e) { Page_Load(sender, e) ; } In the page_load of A, instanciate a B object and then call the function B myB = new B() ; myB.Load() ; Quicky Thank you quicky, it works fine. WilbertWilbert Bujn...

Web resources about - How i can display image on C# web form through ms-access - asp.net.web-forms

Resources last updated: 11/28/2015 9:58:27 PM