i am using Programming language C# asp.net 4.0. I have a situation where i upload an excel. Save it on hard drive using code then using SqlBulkCopy i dump all the content of this excel in to database. the code is working fine.
Problem arises if there is some problem any where in the program like i have less/Extra column in my excel file as specified in db. the exception is fired. i have handled the exception.
now if i again use the same file for upload it shows file is used by another process and code breaks.
Moreover even if file is dumped successfully. i cant upload same file due to same reason.
how can i release or clean up the code or free this file for other processing after my exception occurs or my code runs successfully
You should close the file which you have opened for reading/writing. I guess you are doing that in simple scenario but when exception occurs you have not closed the file.make sure that you close the file in exception as well.
Related
When reading and following log files on a system where logrotate is installed, it happens at certain times that the existing log file is renamed and a new file with the same name is created. The application will then write new log entries into the new file. When I'm still reading the old file and waiting for new data to be appended there, I'll have to know when the file with that name was replaced so that I can stop reading at the end of the file and restart reading the new file.
My log reader is written in C# (.NET Core 3.1) and will run on Linux. I can use native functions through the Mono.Posix.NETStandard package. But I'm not sure how to do that properly.
Should I fetch the inode number from the file name before I start reading? Or should I compare on other data like the size or time? What's the most robust approach to detecting when the file I've currently opened for reading is replaced?
The solution should ideally still work if the log file was replaced exactly at or around the moment when I opened the file for reading, and also if the old file was very small and the new file will be big from the start (because suddenly a lot happens). I couldn't find any information about this topic at all.
I open a CSV file from within my C# application for writing with a
FileShare.Read
flag. I only ever append to the file - I never attempt random access. If I open the file from Excel while my app is still running, Excel gives me an expected "open read-only/notify" prompt. If I chose 'Notify', occasionally my app fails while attempting to write a new line to the file with a
0x21 error - "The process cannot access the file because another process has locked a portion of the file."
I assumed allowing other processes to read the file was safe, but apparently not. Is there anyway to share the file for reading while at the same time prevent another app from locking it (I'm assuming Excel tries to copy the file causing the lock)? I'm also assuming a lock would only affect the bytes at the time of the copy - so why would appending to the file affect that?
When you access the file there's a flag to do a pessimistic lock versus an optimistic lock.
I have a requirement to read and write to a shared excel(xlsx) file using open xml sdk in C#.
I have updated the shared mode setting using the answer to this question on stack overflow and the setting is updated in the created excel file.
I have wrote a small program to insert data to this generated Excel file based on this.
I have tested this with 3 different users trying to write data at the same time over LAN.
I initially got an exception during open at the below statement.
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(excelStream, true))
At this point of time only one user can write to the shared excel file even though the shareworkbook settings were enabled.
Later I changed the above statement to use Stream as below
using (FileStream excelStream = new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite))
{
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(excelStream, true))
After this change the frequency of the initial exception is reduced but still comes sometimes. But, when this succeeds multiple users are able to write to the excel file.
I have observed two important behavior when multiple users write to a shared excel file.
Even though the Users write the data concurrently, all the rows written by a user in one session are arranged in a sequence.
When
multiple users try to write to the same shared excel file, the open
xml seem to have writing the data user after user in a sequence.
This I have verified by inserting a timestamp while writing into the
excel file for each user. Writing for user2 starts after end of
user1.
Can anyone please guide me in finding the right approach to eliminate the exception during open and also do a concurrent write to the excel file using OpenXML Sdk.
Thanks in advance.
You probably should consider using a database if you need multi-user access and expect it to work well. Even Access would be better than Excel. If you need the output to be in Excel, a report that can export to Excel could function for that.
If an exception is occurring while opening the file, maybe try handling that exception, and adding retry logic? You can use something like, https://github.com/App-vNext/Polly to make doing that easy.
The user sequencing is probably just how it's handling writes. I wouldn't expect this to be something you can work around since it's Excel, not a database.
i can comfortably read excel file via ADO.net and ExcelReader but i have to read an Excel file in which data is streaming(by some other application) and that data is not not getting saved on Hard disk
Problem is filestream reads content of file which is saved in hard disk :( but here data is not saved on hard disk
Task at Glance
1) Some exe named abc.exe (3rd party exe, i have no control over it) writing data to excel named temp.xls in every 1 second,and this excel file is open. We can see this data in excel but that data is not getting saved on hard disk. abc.exe is using excel just to display data where we can merely see data.
2) Now I am trying to read data from excel, since that data is not getting saved on hard disk so we can not read with help of file stream class.
3) I am looking for technique by which we can read data in C# from this opened excel file directly from its memory not from hard disk.
Is it possible to read ?
Please help me out
There is approch you can follow:
open temp.xls with readonly flag with standard way: Workbooks.Open Method
Perform your traitement on data. Peraphs copying content to another Workbook.
detect changes on temp.xls using FileSystemWatcher Class and reopen it as 1st point
Another thing can be useful is to detect end of process from abc.exe. Maybe it is only by detect non-existance of temp.xls or non-existance of abc.exe in table of process (via ManagementEventWatcher Class).
Anyway at this point you should sniff everything passing thru temp.xls.
IF you really want "live access" to those changes you can do this:
Write an Excel-Addin (basically a PlugIn for Excel)
It runs inside Excel and can receive events (like workbook/cell changed, file opened/closed etc.)...
this AddIn then communicates all information needed via IPC (for example MMF) to your EXE
Another option might be to use Interop to communicate directly with Excel - whether this works robustly enought depends on several aspects (how the other EXE communicates with Excel etc.).
I was using the Microsoft.Office.Interop.Excel in C# to create a custom .xlsx file.
In doing so I created a Workbook object. Due to the nature of complex SQL queries to grab the data, process it, and apply via Interop the custom styles and formatting the code is very lengthy. Not to mention the very careful process of avoiding memory leaks from the Interop itself, and ensuring that Excel actually closes properly after running.
I originally was testing it out as a console application, and got it working to my satisfaction. What it does is save the end result to the filesystem using the SaveAs member.
However, my next goal was to instead redirect the output as an output stream to asp.net similar to this question here. I've done some rudimentary research and I cannot seem to find an approach that does not involve first saving the Workbook to the server's file system. This may cause conflicts if several users are accessing at the same time, etc.
So my question is, is there an easy way to set the asp.net ContentType for .xlsx and stream out the Workbook object without saving it to the file system? If not, is there a way asp.net can save temporary files automatically without conflicts, serve the temp file, and then delete the temp file after it's been served?
I agree with the comments that you should avoid using Excel Interop server-side, and the third party libraries I've used (EPPlus, Aspose) all support streaming the output. However, if you want to save temporary files without conflict you can use Path.GetTempFileName.
If your ASP.NET app is running under an account without a profile, you may need to give it write access to %WINDIR%\Temp or whatever temporary directory it uses.