Ejemplo n.º 1
0
 public int InsertAd(DTO.Ad ad)
 {
     try
     {
         using (var con = new SqlConnection(conString))
         {
             con.Open();
             var cmd = new SqlCommand("InsertAd", con);
             cmd.CommandType = CommandType.StoredProcedure;
             cmd.Parameters.AddWithValue("Title", ad.Title);
             cmd.Parameters.AddWithValue("Description", ad.Description);
             cmd.Parameters.AddWithValue("City", ad.City);
             cmd.Parameters.AddWithValue("Locality", ad.Locality);
             cmd.Parameters.AddWithValue("Price", ad.Price);
             cmd.Parameters.AddWithValue("ValidTill", ad.ValidTill);
             cmd.Parameters.AddWithValue("CategoryId", ad.CategoryId);
             cmd.Parameters.AddWithValue("UserId", ad.UserId);
             decimal newAdId = (decimal)cmd.ExecuteScalar();
             return(Convert.ToInt32(newAdId));
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
Ejemplo n.º 2
0
        public Ad GetAdById(int adId)
        {
            try
            {
                DTO.Ad ad = null;;
                using (var con = new SqlConnection(conString))
                {
                    con.Open();
                    var cmd = new SqlCommand("GetAdById", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("AdId", adId);
                    var rdr = cmd.ExecuteReader();

                    while (rdr.Read())
                    {
                        ad             = new Ad();
                        ad.AdId        = (int)rdr["AdId"];
                        ad.CategoryId  = (int)rdr["CategoryId"];
                        ad.City        = (string)rdr["City"];
                        ad.Description = (string)rdr["Description"];
                        ad.Locality    = (string)rdr["Locality"];
                        ad.PostedDate  = (DateTime)rdr["PostedDate"];
                        ad.Price       = (int)rdr["Price"];
                        ad.Title       = (string)rdr["Title"];
                        ad.UserId      = (int)rdr["UserId"];
                        ad.ValidTill   = (DateTime)rdr["ValidTill"];
                        ad.AdImages    = GetAdImages(adId);
                    }
                }
                return(ad);
            }
            catch (Exception)
            {
                throw;
            }
        }