Adding a "entered by" field to crud operations - c#

I have a simple CRUD app in .net core with an Admin role and the rest of the users are normal users with no role. I wanted to make the Index of the crud data have the user next to each input that submitted the data while signed in. So say user1#app.com entered data into the database - I want their email to show up next to that data entry in the Index view. That way every user can have their own data tracked.
I have been searching questions and googling forever and maybe I just don't know how to word it correctly. If someone could point me in the right direction I would appreciate it. Something like below..
Food item
Price
Entered by
Apple
1.00
user1#app.com
Pear
2.00
user2#app.com

When registering with a record with the following code, you can obtain and save the user email.
System.Web.HttpContext.Current.User.Identity.Name

Related

Automatically tagging the data with, so as to opened by, the current user

Any walkthrough / sample code for ASP.Net (MVC Model) reflecting the way to record the current user's id in a table, and later on accessing only reflecting the said data?
For example a simple model for a SQL table recording sales being made by more than 1 selling officer at the same time, with fields like:
SalesDate
SalesAmount
SalesLocation
SalesOfficer (Automatically recording the user's id or relevant data)
Thus, the fourth field in the sample model above recording the user's id in the background so as to retrieve the same relevant data, when either logged-in by the same user or the administrator next time.
Hopefully, the question is simple and clear enough.
Looking forward for your expert advice, guidance and references in this regard.
Thanks in advance.
First of all what you can do is get data of SalesDate,
SalesAmount , SalesLocation in a model from a view. Now you need to know the Id of current logged in user.if you are using Default authentication in mvc you can get the currently logged in user by:
string id=System.Web.HttpContext.Current.User.Identity.GetUserId();
if not use appropriate method to get currently logged in user. After that you can add all the records in your database in your ActionMethod.
[HttpPost]
public ActionResult Create(SalesModel model)
{
tblSales sales=new tblSales();
sales.SalesDate=model.SalesDate;
sales.SalesAmount =model.SalesAmount;
sales.SalesLocation =model.SalesLocation;
sales.SalesOfficer =id; //This is the id of currently logged in user
_db.tblSales.Add(sales);
}

Access the same data by more than 1 client on almost the same time

Good day all,
I am work on C# .net 4.0 framework, and database is microsoft SQL server 2008 R2.
There is a system to let user do gift redemption. When User click on the "redeem" button, system will :
check on the gift inventory first before update customer_gift table,
and update the gift_inventory table for the particular gift.
If the inventory is less than 0, then it will prompt error message to tell user that the gift is already finish, and then cancel the redemption process.
There is a problem happen in the following condition :
Gift A only have 1 quantity in inventory now.
Customer Jack click on "redeem" button at 2013-11-11 09:22:27.780.
Customer Jess click on "redeem" button at 2013-11-11 09:22:27.497.
The different time they click on "redeem" button is only 0.0283 seconds.
Thus, maybe the different time is too short, both of them was successful redeem the gift because, before the system (first customer) update the gift_inventory table, the system (second customer) already get the data from inventory (Gift A), thus, system still let second customer to proceed because the inventory is still equal to 1.
I have think out of a way to solve this problem, which is describe as follow :
Create a new column for the gift_inventory table, name lock, with data type Boolean . When customer clicked on the redeem button, and before the system check the gift inventory, set the Boolean to true. Thus, if the second customer, try to access to the gift inventory, and found that the lock = true, then system will wait for 1 seconds and then try to access again, until the lock = false, then only access and get the inventory data.
However, I dun think this is a good solution. I think this may cause the database become slow.
Any idea and suggestion?
I think you can achieve this just by doing each update in a transaction and by using row locking in database.
Read more here:
Transactions
http://technet.microsoft.com/en-us/library/jj856598.aspx
Locking
http://technet.microsoft.com/en-us/library/aa213039(v=sql.80).aspx

Add unique ID to a session

Hello and thanks for reading this, could really use some help ;)
On my site, you play a puzzle after you entered your information and when you either complete the game before the time runs out or not your score will be posted into the gridview below.
I cant post a image but you can check the site if you want a good view about what I'm talking about Website
everything is working right now. you can play it and as you can see the time is showed. in the database that the information is stored in you every row has a unique ID.
here is my question - when someone hits "start spillet" and adds the information to the database, how can i get the unique ID that when that just been created and store it into a session so i can use it later.
(here is a row from my Database)
ID NAME EMAIL COLLEGE CLASS/TEAM TIME
114 Carsten TESTUSER#mediacollege.dk Technology h0dt100413 54
public string generateID()
{
return Guid.NewGuid().ToString("N");
}
using this function you will get a unique id and you can user it as session.
it will different every time

Advice needed on design of parent-child data entry system

I have recently inherited an ASP.Net 4.0/C# webform system. Much of the app was broken, before I fixed it, and the design is not the best. I am trying to get some advise on the best design for a portion of the app. I have searched the web but I am not even quite sure the terms to search for. So I am coming here for your help.
The portion I am talking about asks the user for some demographic information(i.e. name, address, a bunch of other textboxes) on a basic webform. Once this info has been entered the user has the option of creating 2 types of records(A and B for sake of argument) connected to this demographic information. Record type A and B have their own webform screens and collect a whole host of info, which the user can get to from a button on the demographic screen. The rule is that the user can have 0 to n of each record type but at minimum must have at least one record of type A or B. Once the user has entered the demographic info and then entered any records of type A or B, the demographic data is saved in one table and any type A records are saved in Table A and any type B records are saved in Table B. The type A and B records are linked to the demographic record with the primary key of the demographic data table.
I am not even sure where to begin.
What would be a good screen design for this?
How about the code behind logic? I was thinking about using 2 collections(A and B) to store the records A and B. Then I was going to loop through them to insert into the db.
Could you provide me a place to start on the design of this, both the front end and the middle tier logic?
Basically all it needs to do is collect parent record info and then collect as many child records as entered and save them all to the db. Conceptually I know what needs to be done, but I am new to asp.net and have no clue about the best architecture or design. Thanks!
For your middle tier, Object-relational Mapping (ORM) may be a good fit here.
http://msdn.microsoft.com/en-us/data/ef.aspx
As for the screen design -- what are you asking us for, we're just coders ;) but seriously there is a ux.stackexchange.com site and if you ask there they'll be all over it. I'd say structure it logically,
demographic
Native American? Y
records
[if no records exist]
Please add at least one record...
[if records exist]
A
Drink water? Y
B
Drink coffee? Y
etc.

MS Access two users query record at the same time - one should lock (from c# website)

I have a MS Access table which has a column called "recordLocked". Think of it as an editor. The contents is stored in the table and I have two users who want to edit the same content.
The system I have automatically "gives" the content to users to edit. So They log in and I have a timer which will ask the server (at random intervals) for content to edit.
Technically, two users logged on should NEVER request content from the server at the same time... yet they do. To the second. If I view logs, they say
"11:03:06 user 12 asked for content"
"11:03:06 user 77 asked for content"
When user 12 asks for the content, the c# code will update the record in the database to "locked" - so technically when user 77 asks for the content, it shouldn't give it to them (as it's locked).
But as they are asking at the same time, it's giving it to both of them.
Is there a better way to "lock" a record in this way? It's a YES/NO field which I query.
Thanks for any info!
You will want to look at record locking, which will lock a record (row) whilst it is being accessed/edited.
You can find how to implement this here: http://www.databasedev.co.uk/multi-user-application-record-locking.html

Categories

Resources