locked
Classes RRS feed

  • Question

  • User1979860870 posted

    Hi

      I have below 2 classes & can these 2 be combined & created one Single Class file

    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;
            public List<Location> GetAllLocation()
            {
                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;
                }
            }
    
            public int Add(Location objLoc)
            {
                int i;
                using (SqlConnection con = new SqlConnection(cs))
                {
                    con.Open();
                    SqlCommand com = new SqlCommand("sp_Location", con);
                    com.CommandType = CommandType.StoredProcedure;
                    //com.Parameters.AddWithValue("@Id", objLoc.Id);
                    com.Parameters.AddWithValue("@Description", objLoc.Description);
                    com.Parameters.AddWithValue("@Action", "C");
                    i = com.ExecuteNonQuery();
                }
                return i;
            }
        }

    Thanks

    Tuesday, May 25, 2021 11:27 AM

Answers

  • User475983607 posted

    I have both classes in Models folder

    Fantastic.  Do you have a follow up question?  Or are you explaining your final design?

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

All replies

  • User475983607 posted

    I have below 2 classes & can these 2 be combined & created one Single Class file

    You can but it is a poor design.  The location class should be within a shared model library.   The LocationDb is your data access layer.  The location class is passed to the data access layer method(s).  

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

    Hi mgebhard

       I have both classes in Models folder

    Thanks

    Tuesday, May 25, 2021 11:49 AM
  • User475983607 posted

    I have both classes in Models folder

    Fantastic.  Do you have a follow up question?  Or are you explaining your final design?

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