locked
Created on field Value RRS feed

  • Question

  • User1979860870 posted

    Hi

      I have 2 classes . How i can  save Created on field value in Locationdb class.

    public class Location
        {
            public Location()
            {
                CreatedOn = DateTime.Now;
                //CreatedBy = DateTime.Now;
            }
            [Key]
            public int Id { get; set; }
            [DataType(DataType.Text)]
            [RegularExpression(@"^[a-zA-Z'.\s]{1,25}$", ErrorMessage = "Special Characters not allowed")]
            [Required(ErrorMessage = "Please enter Location"), MaxLength(25)]
            [Display(Name = "Location Name")]
            public string Description { get; set; }
            public string IsActive { get; set; }
    
            [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
            public DateTime CreatedOn { get; set; }
            public DateTime CreatedBy { get; set; }
        }
    
    public class LocationDB
        {
            string cs = ConfigurationManager.ConnectionStrings["Cnn"].ConnectionString;
    
            //Return list 
            public List<Location> GetLocation()
            {
                List<Location> objLocation = new List<Location>();
                using (SqlConnection con = new SqlConnection(cs))
                {
                    con.Open();
                    SqlCommand com = new SqlCommand("Sp_Location", con);
                    com.CommandType = CommandType.StoredProcedure;
                    com.Parameters.AddWithValue("@Action", "R");
                    SqlDataReader rdr = com.ExecuteReader();
                    while (rdr.Read())
                    {
                        objLocation.Add(new Location
                        {
                            Description = rdr["Description"].ToString(),
                        });
                    }
                    return objLocation;
                }
            }
    }

    Thanks

    Tuesday, May 25, 2021 11:12 AM

Answers

  • User475983607 posted

    Hi

       

    public Location()
            {
                CreatedOn = DateTime.Now;
                //CreatedBy = DateTime.Now;
            }

    Thanks

    You set the DateTime in the constructor.  The next step is to pass the Location object to a Save method.  The Save method saves the properties in a table.  It looks like you are using ADO.NET and stored procedures so just create a stored procedure that accepts the CreatedOn date and INSERTs a new record.  Do not create one stored procedure that does 4 CRUD operations.  Doing so only leads to maintenance and troubleshooting issues.  Plus the client code requires dependencies that developer using your code need to know about.

    Any beginning level ADO.NET tutorial will illustrate standard programming patterns.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 25, 2021 1:46 PM

All replies

  • User475983607 posted

    You did not share any relevant code whatsoever.  It seems like you posted random code blocks. 

    Where and when is the CreatedOn field populated?  In C# code?  In a stored procedure that you did not include???  Or maybe a table default value???

    Can you explain how your design is intended to function? 

    Tuesday, May 25, 2021 11:36 AM
  • User1979860870 posted

    Hi

       

    public Location()
            {
                CreatedOn = DateTime.Now;
                //CreatedBy = DateTime.Now;
            }

    Thanks

    Tuesday, May 25, 2021 12:53 PM
  • User475983607 posted

    Hi

       

    public Location()
            {
                CreatedOn = DateTime.Now;
                //CreatedBy = DateTime.Now;
            }

    Thanks

    You set the DateTime in the constructor.  The next step is to pass the Location object to a Save method.  The Save method saves the properties in a table.  It looks like you are using ADO.NET and stored procedures so just create a stored procedure that accepts the CreatedOn date and INSERTs a new record.  Do not create one stored procedure that does 4 CRUD operations.  Doing so only leads to maintenance and troubleshooting issues.  Plus the client code requires dependencies that developer using your code need to know about.

    Any beginning level ADO.NET tutorial will illustrate standard programming patterns.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 25, 2021 1:46 PM