Hi,
I have a list of products in with images stored in a SQL Server 2000 DB. I
need to be able to retrieve the images when a product is chose. I use the
code below to get the image from the DB and assign it to the picture box.
It works fine the first time but subsequent times it gives me an error
saying that the file is in use by another process. What process? If I go to
the folder and try to delete the file in windows I get the same error. Can
anyone help me here? What am I doing wrong? I am closing the filestream so
what is still open?
Thanks
Tim
try
{
//Look Up the Image
SqlCommand cmdSelect=new SqlCommand("SEL ECT logo_image FROM
t_store_informa tion WHERE store_id =1" , DBConnection.DB Conn.Connection );
DBConnection.DB Conn.Connection .Open();
byte[] barrImg=(byte[])cmdSelect.Exec uteScalar();
DBConnection.DB Conn.Connection .Close();
string strfn=Convert.T oString(DateTim e.Now.ToFileTim e());
FileStream fs = new FileStream(@"C: \Program Files\MM\Temp\t humb.jpg",
FileMode.OpenOr Create, FileAccess.Writ e);
fs.Write(barrIm g,0,barrImg.Len gth);
fs.Close();
strfn = "";
this.pboxThumb. Image = Image.FromFile( @"C:\Program
Files\MM\Temp\t humb.jpg");
}
catch(Exception ex)
{
#if DEBUG
MessageBox.Show ("Image Error!\r\n\r\n" + ex.Message);
#endif
}
finally
{
DBConnection.DB Conn.Connection .Close();
} 9 1893
Hi Tim,
If I were you I wouldn't use a FileStream but a MemoryStream. That way
you avoid all those stick file issues and it will work a goof deal
faster for you.
For the record what might be happeneing is that the FromFile functions
implementation may not be releasnig the unmanaged FileHandle fast
enough but i can't be sure.
If you want to see what process is holding the file handle try using
Process Explorer from www.sysinternals.com.
Good Luck,
Jan
Thanks Jan,
I don't suppose you have an example of MemoryStream do you? Also I thought
that pictureboxes had to have a file that actually existed on the hard
drive.
Tim
"Jan" <ja***********@ gmail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. . Hi Tim,
If I were you I wouldn't use a FileStream but a MemoryStream. That way you avoid all those stick file issues and it will work a goof deal faster for you.
For the record what might be happeneing is that the FromFile functions implementation may not be releasnig the unmanaged FileHandle fast enough but i can't be sure.
If you want to see what process is holding the file handle try using Process Explorer from www.sysinternals.com.
Good Luck, Jan
Hi Tim,
Here's an example:
using (System.IO.Memo ryStream mem = new
System.IO.Memor yStream(bytes,f alse))
{
System.Drawing. Bitmap bitmap = new Bitmap(mem,fals e);
pictureBox1.Ima ge = Bitmap;
}
And no PictureBoxes don't need a file. They just and Image (which is
the superclass of Bitmap).
Hope that helps,
Jan
Tim <ti*@home.com > wrote: I have a list of products in with images stored in a SQL Server 2000 DB. I need to be able to retrieve the images when a product is chose. I use the code below to get the image from the DB and assign it to the picture box.
It works fine the first time but subsequent times it gives me an error saying that the file is in use by another process. What process? If I go to the folder and try to delete the file in windows I get the same error. Can anyone help me here? What am I doing wrong? I am closing the filestream so what is still open?
It doesn't really mean another process - it's a misleading error
message.
Although you're closing the FileStream, you're then using
Image.FromFile, which opens another FileStream and keeps it open until
the image is disposed.
Using a MemoryStream as Jan suggested should solve the problem, but you
shouldn't use a using block - the image "owns" the stream you pass into
it, and expects you to leave it open. From the Bitmap constructor docs:
<quote>
Remarks
You must keep the stream open for the lifetime of the Bitmap object.
</quote>
You should, however, definitely use a using statement for your database
connection :)
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Thanks for your reply Jon, I really appreciated it. What do you mean by "You
should, however, definitely use a using statement for your database
connection"?
Thanks
Tim
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** @msnews.microso ft.com... Tim <ti*@home.com > wrote: I have a list of products in with images stored in a SQL Server 2000 DB. I need to be able to retrieve the images when a product is chose. I use the code below to get the image from the DB and assign it to the picture box.
It works fine the first time but subsequent times it gives me an error saying that the file is in use by another process. What process? If I go to the folder and try to delete the file in windows I get the same error. Can anyone help me here? What am I doing wrong? I am closing the filestream so what is still open?
It doesn't really mean another process - it's a misleading error message.
Although you're closing the FileStream, you're then using Image.FromFile, which opens another FileStream and keeps it open until the image is disposed.
Using a MemoryStream as Jan suggested should solve the problem, but you shouldn't use a using block - the image "owns" the stream you pass into it, and expects you to leave it open. From the Bitmap constructor docs:
<quote> Remarks You must keep the stream open for the lifetime of the Bitmap object. </quote>
You should, however, definitely use a using statement for your database connection :)
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Wow, Jan thank you. I have been struggling with this problem for a while and
this works like a charm.
Tim
"Jan Bannister" <ja***********@ gmail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. . Hi Tim,
Here's an example:
using (System.IO.Memo ryStream mem = new System.IO.Memor yStream(bytes,f alse)) { System.Drawing. Bitmap bitmap = new Bitmap(mem,fals e); pictureBox1.Ima ge = Bitmap; }
And no PictureBoxes don't need a file. They just and Image (which is the superclass of Bitmap).
Hope that helps, Jan
Tim <ti*@home.com > wrote: Thanks for your reply Jon, I really appreciated it. What do you mean by "You should, however, definitely use a using statement for your database connection"?
You're currently manually calling Close on your database connection,
which means that if an exception is thrown, you're failing to close it.
(You were doing the same with the FileStream too.)
You should use a using statement (like the one Jan used for the
MemoryStream) which is the equivalent of try/finally, with Dispose
being called in the finally block.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Thanks Jon, I have adjusted my code accordingly.
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Tim <ti*@home.com > wrote: Thanks for your reply Jon, I really appreciated it. What do you mean by "You should, however, definitely use a using statement for your database connection"?
You're currently manually calling Close on your database connection, which means that if an exception is thrown, you're failing to close it. (You were doing the same with the FileStream too.)
You should use a using statement (like the one Jan used for the MemoryStream) which is the equivalent of try/finally, with Dispose being called in the finally block.
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: antsays |
last post by:
I am trying to serialize a collection to disk using the code provided.
This works just fine but when I try do copy and past the xml file to
another location or sometimes even just click on the file it gives me
the error: The instruction at "0x635e0719" referenced memory at
"0x00000000". The momory could not be "read.
Could anyone please help out with this
|
by: Patrick Sannes |
last post by:
Hi,
At this moment I have one large sourcecode file and want to seperate it into
multiple files. When I do so, the app crash after he created 6389 (156794880
bytes total) source files with the error:
Filestrem will not open Win32 devices such as disk partitions and tape
drives. Don't use \ \\\.\\\ in the path.
After I put in an exception handler and an breakpoint in it, i discovered
that the filename was a correct one. (just like the...
|
by: Dorsa |
last post by:
HI, Could you please tell me the error in here. I am trying to open an
XML file from a link.
Response.Clear()
Response.Expires = 0
Response.BufferOutput = False
Response.ContentType = "text/xml"
Response.AddHeader("Content-Disposition",
"filename=test.XML")
|
by: lh |
last post by:
The following method only works when i give the ASP.net account full
permissions on the directory.
It doesn't work when i give the directory Modify, Read &Execute, List Folder
Contents, Read, and Write permissions (all at the same time)
I'm only trying to READ the file. Why do i have to give it full
permissions?
public static string ReadFileContents(string filePath)
{
|
by: Nathan Sokalski |
last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving, the other in which I close the FileStream afterwards, although both return the same error. Here are the two versions of the code and the errors they each return (NOTE: I rebooted immediately before running each of these versions so that I knew they...
| |
by: Tim_Mac |
last post by:
hi,
i'm not sure if i have chosen the best approach, but it seemed quite
good to me.
i have a collection class, containing business objects. the collection
class is static and remains in-memory all the time the app is running.
it is persisted to an Xml file when necessary. i keep a static
filestream on the xml file. the advantage of this is that i can lock
the filestream object to prevent concurrent reading/writing.
the code works...
|
by: ewingate |
last post by:
The following code which is supposed to dynamically create files with
incrementing names is throwing an exception due to the inclusion of the
DateTime.Now component of the sReportSave string:
string sReportSave = ( DateTime.Now + "Report.Dat" );
FileStream fs = new FileStream( sReportSave , FileMode.Create );
Is my approach completely wrong or is there different way to modify the
FileStream so that .NETclients used by multiple users...
|
by: =?Utf-8?B?TWlzY2hh?= |
last post by:
It is my understanding that I can create an XmlTextReader from a variety of
sources, including a stream object. I am developing a web application that
reads some data from an Xml file. Everything works fine if I use the
XmlTextReader to open the file (i.e. pass it the file name):
Dim reader As XmlTextReader = New XmlTextReader("<file_name>")
But I get an access error if I try to open the file using a FileStream
object and then passing...
|
by: Andreas Zimmermann |
last post by:
Hi,
we just set up a SQL Server 2008 on Windows Server 2008 (configured as file
/ webserver, 16 GB memory, 4 dual core processors, 6 internal disks + 15
disc ISCSI array with overall almost 3 TB space) to use filestream and ran
into the following problem:
We extracted binaries from our 'original' database and imported it into the
new 08 database using filestream - so far, so good.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |