Re: Filestream Error...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1d8fc61a8c4a7a298c6cd@xxxxxxxxxxxxxxxxxxxxxxx
> Tim <tim@xxxxxxxx> 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 - <skeet@xxxxxxxxx>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too


.



Relevant Pages

  • Re: Concurrent database access in SQL 2005 Mobile
    ... opening one connection for the lifetime of the app and keeping it open seems ... What version of SQL CE are you using? ... Are you accessing the database from an app written in C++? ... then opens his work forms and it's in those ...
    (microsoft.public.sqlserver.ce)
  • Re: Making a connection timeout?
    ... >> Perhaps I am not understanding your question, but a MSAccess database is ... > The class that returns the actual dao.database object opens the db and ... > I'd rather have the db that Data Accessor gets from the other class (the ... implement a "Connection Object". ...
    (microsoft.public.vb.general.discussion)
  • Re: Concurrent database access in SQL 2005 Mobile
    ... What version of SQL CE are you using? ... Are you accessing the database from an app written in C++? ... then opens his work forms and it's in those ... It wouldn't seem to me that you'd need a new connection to ...
    (microsoft.public.sqlserver.ce)
  • Re: Sql Server 2008 Developer Edition
    ... connection to the database engine and not Analysis Services works correctly. ... what type of query window I try to open I only get an MSAS connection popup. ... Assuming Object Explorer opens up correctly, ...
    (microsoft.public.sqlserver.setup)
  • Re: Management Studio: DB Context: Script Opens a new connection - How do I get script to reuse
    ... > When I was opening up a script to apply to a database, ... > always opening a new connection to the Master Database. ... > happening was each script opens a new connection to the master DB. ...
    (microsoft.public.sqlserver.tools)