|
one last question please..
I have a dashboard form and details form.
I am viewing the photo on the dashboard form and when the user clicks edit a details form will be shown with the photo too then user can update the photo from the details form.
I have no problem viewing the photo on the dashboard form.
but when the user clicks edit I get the following error:
The process cannot access the file c:\temp\tmpimage.png because it's being used by another process
How can I avoid this?
Technology News @ www.JassimRahma.com
|
|
|
|
|
Using Image.FromFile locks the file until the application ends. This is a known bug that's been around since .NET 1.0; the workaround is to use the Image.FromStream method instead.
In this case, since you're reading the image from the database, you don't need to store it in a file. Just create a MemoryStream over the byte array returned from the database:
byte[] rawData = (byte[])sql_reader["photo"];
using (var memoryStream = new MemoryStream(rawData))
{
pictureEmployee.Image = Image.FromStream(memoryStream);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
It's not a bug. It's a very unwanted "feature".
The file stays locked for the lifetime of the Image object, not the application. Dispose the Image and the lock goes away.
|
|
|
|
|
Strange - I'm sure it used to keep the image locked until the process ended, but I've just tested in .NET 4.0 and 3.5, and disposing the image does release the lock.
I guess they must have fixed that bug earlier than I thought.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Great
Thank you so much
Technology News @ www.JassimRahma.com
|
|
|
|
|
Hi,
i have an sql table which stores times of a machine's 'work start' and 'idle start' times, which is like ;
2014-09-03 12:50:15 start
2014-09-03 13:45:25 idle
2014-09-03 13:56:06 Start
2014-09-03 16:30:35 idle
.
.
and goes like that.
i need to calculate the time between first start and first idle time after the first start, then, time between second start and first idle after the second start, and it goes like that and there are too many lines. at the and i need total work time and total idle time.
Can anyone help me with this?
Thank you.
|
|
|
|
|
The easiest way would be to read all the times into a dataset and then go through them calculating the individual differences. See http://msdn.microsoft.com/en-us/library/1905yhe2(v=vs.110).aspx[^] for how to get the TimeSpan value between two DateTime objects. I assume that you are storing these values as DateTime types.
|
|
|
|
|
thank you for your response, yes i store these values as DateTime types, and also i am aware of TimeSpan. but the fact is, it is easy for just two values, but i have too many values and my real struggle is this, i couldn't create a logic to calculate for all that values.
|
|
|
|
|
It is just a matter of reading all the values one by one.
- Read the first time value, that is the starting time of work.
- Read the next time, that is the ending time of work.
- The difference between those two is added to the total work time.
- Set the end time as the new start time, that is the starting time of idle.
- Read the next time, that is the ending time of idle (and the next starting time of work).
- The difference between start and end should be added to idle time.
- Set the end time as the new start time (of work).
- Repeat the process until all times have been dealt with.
In cases such as this you should forget about programming and coding issues, and think about the logical steps needed to get your answer. Write down those steps and go through them a few times to ensure they make sense. Then when you are happy that your algorithm is correct you can write and test the actual code needed for your application.
|
|
|
|
|
thank you for your advice.
|
|
|
|
|
Are you trying to do this in C# or SQL?
|
|
|
|
|
C# but if you can offer a solution for SQL it is OK too
|
|
|
|
|
To do this in SQL, CROSS APPLY might help:
DECLARE @Data TABLE
(
TimeLogged datetime2(0) NOT NULL,
Action varchar(5) NOT NULL
);
INSERT INTO @Data
VALUES
('20140903 12:50:15', 'start'),
('20140903 13:45:25', 'idle'),
('20140903 13:56:06', 'Start'),
('20140903 16:30:35', 'idle')
;
SELECT
D1.Action,
Sum(DateDiff(second, D1.TimeLogged, D2.TimeLogged)) As DurationInSeconds
FROM
@Data As D1
CROSS APPLY
(
SELECT TOP 1
TimeLogged
FROM
@Data As D2
WHERE
D2.Action != D1.Action
And
D2.TimeLogged >= D1.TimeLogged
ORDER BY
TimeLogged
) As D2
GROUP BY
D1.Action
;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
this seems very useful but i am not very good at sql. how can i use my table in this query?
|
|
|
|
|
Since you didn't post any details about the structure of your table, I had to make up an example. You'll want just the SELECT block; replace @Data with the name of your table, and replace TimeLogged and Action with your column names.
SELECT
D1.NameOfYourActionColumn,
Sum(DateDiff(second, D1.NameOfYourTimeLoggedColumn, D2.NameOfYourTimeLoggedColumn)) As DurationInSeconds
FROM
NameOfYourTable As D1
CROSS APPLY
(
SELECT TOP 1
NameOfYourTimeLoggedColumn
FROM
NameOfYourTable As D2
WHERE
D2.NameOfYourActionColumn != D1.NameOfYourActionColumn
And
D2.NameOfYourTimeLoggedColumn>= D1.NameOfYourTimeLoggedColumn
ORDER BY
NameOfYourTimeLoggedColumn
) As D2
GROUP BY
D1.NameOfYourActionColumn
;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
it seems to work,
thank you very much
|
|
|
|
|
|
Who's zip file is it?
Give up, you don't stand a chance.
Regards,
Rob Philpott.
|
|
|
|
|
|
Jennifer Lawrence's
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
There is a tool I've heard about, but have not yet been able to verify if it really works.
It's called Ultimate CrIP Zacker, or something.
Cheers!
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
Sorry but you're NEVER going to get help with this in any legitimate forum such as CP.
We have no idea who this ZIP file belongs to and we're not going to post any information that can be used to crack it as we're not going to be party to stealing someones information, even if it isn't you that's doing it. It could be some other schmuck who's Googling around trying to find information on how to do it themselves.
|
|
|
|
|
With that argumentation, we should REALLY stop explaining how sql-injection works.
Ban the bobby tables comic!
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
There's a significant difference between giving a detailed explanation of how to pick a lock, and explaining to someone that if they leave their door open, someone will come in and steal their stuff.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Glad you agree the comic should be banned.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|