Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
while im giving input of username, login password I need to five the timestamp of creating along with username to it.
anyone know how I can!!

I need to change the below code at least to give username + default sys time as input to DB automatically.

What I have tried:

public static DateTime Now { get; set;}

public int Id { get; set; }
        [Required(ErrorMessage = "First Name is required")]
        [StringLength(50, MinimumLength = 3)]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

  //[Required(ErrorMessage = "Last Name is required")]
        [StringLength(50, MinimumLength = 0)]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

     [Required(ErrorMessage = "Age is required")]
        [Range(typeof(int), "18", "60", ErrorMessage = "Age must be between 18 and 40")]
        public int Age { get; set; }



       [Display(Name = "Email Address")]
        [Required(ErrorMessage = "Email Address is required")]
        [StringLength(60)]
        [DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email Address")]
        public string Email { get; set; }


       [Display(Name = "Mobile Number")]
        [StringLength(10)]
        [Required(ErrorMessage = "Mobile Number is required")]
        [RegularExpression("[6-9][0-9]{9}", ErrorMessage = "Mobile Number must 
        begin with 6,7,8 or 9")]
        public string MobileNumber { get; set; }

        public DateTime DateOfBirth { get; set; }



       [StringLength(20)]
        public string PlaceOfBirth { get; set; }
        [StringLength(20)]
        public string Nationality { get; set; }

    [Required(ErrorMessage = "Password is required")]
        [StringLength(10)]
        [DataType(DataType.Password)]
        public string Password { get; set; }



       [Required(ErrorMessage = "Confirm Password is required")]
        [DataType(DataType.Password)]
        [StringLength(10)]
        [Display(Name = "Re-Enter Password")]
        [Compare("Password")]
        public string ConfirmPassword { get; set; }
Posted
Updated 16-Aug-22 0:02am
v2
Comments
Richard MacCutchan 16-Aug-22 6:30am    
There is no point in asking for age since it is not a constant. If you want the person's age then use the Date of Birth to calculate it. Also you have not indicated where you want to add the registration timestamp.

See DateTime.Now Property (System) | Microsoft Docs[^]. You should use the DateTime.UtcNow Property (System) | Microsoft Docs[^] and store it in your database as a DateTime type.
 
Share this answer
 
That's not code. That's a single line that defines a property called Now. That's it. There's really nothing to "modify" and there's nothing really there to modify.

NOTHING happens "automatically." You have to write the code to do everything you want the user to perceive as "automatic." If you want to save the username and the current datetime, you have to write the code to set those properties and write the code to save that data to whatever datastore you're using.

Nobody can do that for you as you and you haven't described anything useful that can be used to write the code. Where are you storing this data? Whatever are you storing? What are the requirements for the data you're storing? Because you mention "username", that implies a password to go with it. Is that correct? Are you storing login attempts? ...
 
Share this answer
 
Comments
Randomuser787 16-Aug-22 5:58am    
Yes your correct i want to give a time stamp for the registration and update of a student in MVC
I want to figure out how to do that
1. when registering username+ timestamp
2. when updating username+ timestamp +previous value along with present value with changed time

I'm trying this out from scrath to implement on student application of MVC
I tried given db approach and now I'm trying for code first approach

I'm trying this below code which I'm adding above now on what I have tried

Dave Kreskowiak 16-Aug-22 9:58am    
Given such limited and confused "requirements", you could do this in a single table. You already said what the fields in the table are, CurrentUsername, PreviousUsername, LastModifiedDateTime.

The database will not update these values for you. You have to write the code to get the current record for the username (if it exists!), set the previous username, set the new current username, and set the last modified datetime. After everything is set, you tell EF the record is dirty and call savechanges on the context.
Dave Kreskowiak 16-Aug-22 10:32am    
I just saw the code you added to the original question.

Why would store the "confirm password" value? Nobody does that!

You're putting all kinds of validation attributes on your data storage objects. Those classes should not be used to pass data to your view, nor should they be used for UI validation. You should be creating view model classes that handle user input and validation, separate from your data storage classes.

Also, you're storing passwords in clear text strings. NEVER DO THAT!! Password values should be salted and hashed before being stored in the database.
Randomuser787 23-Aug-22 12:56pm    
thanks i figured rather directly giving time stamp from here i gave trigger for after insert.

now im trying to do is that in this same application when a user logged inand creates/edits the records i need that current logged in username to be passed into db how can i do that any reference or links with yt videos would help
Dave Kreskowiak 23-Aug-22 13:36pm    
Create what records? If you're talking about data the application handles (outside of login data), then you DO NOT "pass the username" into the database. The username is already there, or else the user would not be logged in, correct?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900