473,729 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Filestream Error...

Tim
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();

}
Nov 17 '05 #1
9 1893
Jan
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

Nov 17 '05 #2
Tim
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

Nov 17 '05 #3
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

Nov 17 '05 #4
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
Nov 17 '05 #5
Tim
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

Nov 17 '05 #6
Tim
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

Nov 17 '05 #7
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
Nov 17 '05 #8
Tim
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

Nov 17 '05 #9
Cool,

Glad to help,
Jan

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2137
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
5
8981
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...
11
4044
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")
0
1532
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) {
7
2310
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...
9
2668
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...
2
1918
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...
2
4077
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...
8
2851
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.
0
8921
marktang
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...
0
9427
Oralloy
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...
0
9284
jinu1996
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...
0
9148
tracyyun
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...
1
6722
isladogs
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...
0
6022
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();...
0
4528
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...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2165
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.