Running number with multiple instance C# program - c#

I would like to check with you all regarding running multi-instances of same program (in different PC) with a running number (non-duplicate) field on it.
Program outline
- Program will get running number from DB (Eg: LoanID from LoanRef table)
- User input info and click on Save.
- Program will save the information to DB (eg: Loan table)
Currently, the program will retrieve the latest running number from DB (SQL Server 2012) and put it in read only text filed on the program.
During my development testing, I only tested with one program on one PC and it worked fine.
When I tested with multiple PC, access (almost) at the same time, the running number become a problem.
Is there anyway to prevent such kind of running number issue (while allowing multiple instances of the same program).
I think of generate the number at the end of 'Save' process but the problem is user wants to see the number before save. So, I need to get it when form load occurs.
And I also can't disable multiple instances with Mutex because more than one person needs to use this program.
Any idea on what functions or features that I should use or check?

Related

Traversing all files in windows pc and create report due to creation date in an efficient way in C#

My client wanted me to write a simple desktop app such that :
User will select a date. Then the program will travel all files in the windows pc and then create an excel report for all files which are created before the selected date.
Actually it seems really simple application. However, my client told me he has petabytes of data which made me think the app would run for hours even days. I need a really smart solution for that. For instance despite the possibility of a crash or unexpected error etc, I am planning to divide the report into parts and create a new excel file for each 50000 records. In addition, the application should continue from where it left off. In another words, if the application is closed, the application should not start scanning all over again. What is the most logical way for the program to work efficiently and continue from where it left off? What comes to my mind is to put a tick on the folders so that the already looked folders are not looked at again. What is the best way to implement this logic in C#? Is it efficient to create a separate list for the visited folders and then first look if the folder is alreay in list? Which data structures would you use for this problem and how would you implement this?
Thanks.

How to trigger VBA code in Access when underlying SQL table is updated?

Background
We have a legacy MS Access application that uses MS SQL tables as the backend, and prints a report (product label) on demand based on a user action. This works as expected, and we'd prefer not to change this part.
I have a new standalone (C# Windows Forms) application running on a different PC that performs some mostly unrelated actions, but requires the user to print the above mentioned label towards the end of the process.
Question
Instead of having the user switch to the legacy Access app on a different PC (which may be at a different location), how can I trigger the Access VBA that runs the report when data is updated from the standalone C# application?
One opproach
I was thinking I could create a table RequestedLabels with columns LabelId, PrinterId. The standalone app would insert a row to request a label, and the MS Access app would query the table at small intervals to see if there are any new labels to be printed for this instance.
However, there are several things I don't like about this approach:
The label should be printed immediately after the request so my query interval would have to be quite small (1-2s max). Even at 2s the delay would be a nuisance. At higher rates I suspect this could interfere with the MS Access application's responsiveness.
This would be running on 10-20 machines at at time so the number of queries to the SQL server would be around 20/sec or more.
The user is only printing labels every few minutes, so most of the processing/network capacity required to do this is wasted.
A better approach
I was hoping for some way to have changes to the underlying data trigger a Form Event in MS Access, or trigger some VBA function that could print the labels on demand.
Caveats
The computers are across two subnets separated by a strict firewall and run mixed OSs both on and off the domain. RPC between PCs has proven to be tricky in the past due to the non-uniform setup.
I would prefer a solution that relies on updating the data on the SQL server, or some other MS SQL feature that both the C# and MS Access applications can take advantage of.
UPDATE:
Since there have been two votes to close, I have refined the wording to focus more clearly on triggering the VBA when the underlying data changes.
You can build a macro in MS Access to send the report to the printer. The report should already have the data you want to print.
Let's say your macro's name is mcrPrintReport
Then you can execute MS Access from the command line to open the Access DB file and run the macro like this:
PathToAccessExe PathToAccessFile /x MacroName
C:\Program Files\Microsoft Office\Office16\MSACCESS.EXE C:\AccessDB.accdb /x mcrPrintReport
You can then schedule this to run on an interval, maybe every 5 minutes, using the Task Scheduler on a dedicated PC.
See this related question also: Running Macro from .bat with another Access db open

Make last change to database when program has crashed/been terminated?

I have an app where user can work on a file downloaded from database. When file is downloaded, in database query is run setting "owner" value in corresponding table row. As a result other users see that file is owned by somebody else and can only read from it, not modify it. After work session user saves file to database and sets "owner" to none allowing other users access file. This works fine till app crashes or there is power shortage or some error in system that terminates app leaving database entries "reserved". In case of crash I can follow this approach :
WPF global exception handler
and try to connect to database to "free" all files reserved by user, but in other cases as my research has shown there is no way how to know that program failure has occured and I should "free" database so other users can access previously reserved files. Is this way how to show other users that something in database is not awailable for editing is in general bad design or there are solutions to this type of a problem?

Saving config file safely on the disk - Windows

I am having one situation related to security and need your valuable suggestions or solutions.
So the story goes like this:
There is a Windows (consider Win 7 and above. This is an end user machine and not any server) machine and it is having 2 desktop applications. One is written in C++ and the other is written in C#.
One application emits some data as configurations which I need to save on this machine (on disk), later when another application wakes up, it will pick the configuration file and do whatever it is supposed to do.
Both the applications are 2 different application. There is no relationship between them except that they are installed on the same machine.
Now, from the security and consistency point of view, I do not want that user access this file and play with this/update it or delete it.
So my question here is what is the best way (or safest place) to secure the file on the Windows machine so that user will not be able to temper it? The amount of config data is very small - consider 3 string fields like first name, last name and age in json format.
Thanks.

saving multiple user input variables to be used later in scheduled task

I am trying to figure out the best way to run my scheduled task. My program finds a database backup file and restores it to a different location. But I can only restore backups after 5pm daily. I need the first part of my program (which is a web form with user input from a dropdown) to save a few variables to be used later when the scheduled task is triggered (after 5pm everyday).
The second part of the program takes the user inputted variables from first part and runs the second part of the program (a database restore). The problem I am having is figuring out where and how to store the variables from the first part of program? * This first form may be sent multiple times. Meaning I would need to retrieve the variables saved from first part to use in second part.
Also, trying to figure out how to run the second part of program until all the saved variables are used up, meaning when all databases selected from first part are restored. (It's a database restore so I can't loop the whole process. Some databases are very large).
First part of program, variables needing to be saved
-dbName(chosen from dropdown)
-backUpLocation(where the backup file is located)
-serverLocation(Server that user wants to restore to)
What second part of program does
After 5pm daily, copies files and unzips it to server that is designated in first part. Then restores it. Thanks for any help!
I like to use SQLite or just a regular old XML file using LINQ to XML for this sort of state persistence. Do you have any special requirements out of how/where you store these values?

Categories